0% found this document useful (0 votes)
36 views1 page

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

This C code defines a client for a UDP socket-based messaging application. It includes header files for sockets, networking, time functions. The main function initializes a socket, connects to a server, prompts the user for input messages, sends each message to the server along with a timestamp, receives and prints the server's response with another timestamp. The client continuously loops to allow sending multiple messages.

Uploaded by

Srikanth Vasist
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
36 views1 page

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

This C code defines a client for a UDP socket-based messaging application. It includes header files for sockets, networking, time functions. The main function initializes a socket, connects to a server, prompts the user for input messages, sends each message to the server along with a timestamp, receives and prints the server's response with another timestamp. The client continuously loops to allow sending multiple messages.

Uploaded by

Srikanth Vasist
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 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