-
Notifications
You must be signed in to change notification settings - Fork 1
/
client.c
64 lines (51 loc) · 1.79 KB
/
client.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#define CHUNK 50 // max send/ recv size
// #define VERBOSE // **optional** print input and output
char * recvTCP(int sockfd) {
// returned string is dynamically allocated and must be freed
char *input = NULL;
char tmpbuff[CHUNK+1]={0};
size_t inputlen = 0, tmplen = 0;
do { // do receive calls until empty receive
tmplen = recv(sockfd, tmpbuff, CHUNK, 0);
input = realloc(input, inputlen + tmplen + 1);
strcpy(input + inputlen, tmpbuff);
inputlen += tmplen;
} while(tmplen>0 && tmpbuff[tmplen-1]!='\0' && tmpbuff[tmplen-1]!='\n');
return input;
}
int main(int argc, char **argv) {
int port; // check if port value entered by user
if(argc<2 || (port = atoi(argv[1]))==0) {
perror("Insufficent arguments! Please provide port no of server as command line arg, Exiting...");
exit(0);
}
int sockfd;
struct sockaddr_in serv_addr;
if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
perror("Unable to create socket\n");
exit(0);
}
serv_addr.sin_family = AF_INET;
inet_aton("127.0.0.1", &serv_addr.sin_addr);
serv_addr.sin_port = htons(port);
if (connect(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) <0) {
perror("Unable to connect to server\n");
exit(0);
}
// printf("connected to serv on %s:%d\n", inet_ntoa(serv_addr.sin_addr), port);
// Assumption that received time is already in readable
// date-time format and doesn't need to be processed
char *buff = recvTCP(sockfd);
printf("Received time:\t%s\n", buff);
free(buff);
close(sockfd);
return 0;
}