File: /home/srikanth/assignments/csp/samples/example1/client.c Page 1 of 1

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 1

File: /home/srikanth/assignments/csp/samples/example1/client.

c Page 1 of 1
#include <sys/socket.h>
#include <netinet/in.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
int main(int argc, char**argv)
{
int sockfd,n;
struct sockaddr_in servaddr,cliaddr;
char sendline[100];
char recvline[100];
time_t timer;
char buffer[25];
struct tm* tm_info;
if (argc != 2)
{
printf("usage: client <IP address>\n");
exit(1);
}
sockfd=socket(AF_INET,SOCK_DGRAM,0);
bzero(&servaddr,sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr=inet_addr(argv[1]);
servaddr.sin_port=htons(50000);
printf("Enter a message to send to the server:");
while (fgets(sendline, 100,stdin) != NULL)
{
time(&timer);
tm_info = localtime(&timer);
strftime(buffer, 25, "%Y:%m:%d %H:%M:%S", tm_info);
printf("sending %s to server at %s\n",sendline,buffer);
sendto(sockfd,sendline,strlen(sendline),0,
(struct sockaddr *)&servaddr,sizeof(servaddr));
n=recvfrom(sockfd,recvline,100,0,NULL,NULL);
recvline[n]=0;
time(&timer);
tm_info = localtime(&timer);
strftime(buffer, 25, "%Y:%m:%d %H:%M:%S", tm_info);
printf("server response: %s @ %s\n",recvline,buffer);
printf("Enter a message to send to the server:");
}
}

You might also like