-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMultiTimezoneClient.c
153 lines (123 loc) · 3.44 KB
/
MultiTimezoneClient.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
//
// MultiTimezoneClient
//
// Created by <insert name>
// Username: <inser username>
//
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <netdb.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>
#include <pthread.h>
#include <sys/socket.h>
#include <time.h>
#define kDAYTIMEPORT 1313
#define kMULTITIMEZONEPORT 1414
#define bufferSize 12
int main(int argc, const char * argv[])
{
int sockFD;
int con;
char buffer[bufferSize+1];
char line[512];
char temp[512];
ssize_t n;
int count;
int more;
int pos=2;
struct sockaddr_in adstr;
struct hostent *hp;
sockFD=socket(PF_INET,SOCK_STREAM,IPPROTO_TCP);
if (sockFD>-1)
{ /* create connection*/
memset(&adstr, 0,sizeof(adstr));
adstr.sin_family = AF_INET;
adstr.sin_port=htons(kMULTITIMEZONEPORT);
hp=gethostbyname(argv[1]);
memcpy(&adstr.sin_addr,hp->h_addr,hp->h_length);
con=connect(sockFD,(struct sockaddr *)&adstr ,sizeof(adstr));
if (con>-1)
{
pos=2;
//printf("findinging time\n");
/*read in data*/
do
{
do
{
// printf("pos = %d\n",pos);
more = 1;
//printf("reading\n");
n = read(sockFD, buffer, bufferSize);
//printf("finished reading\n");
buffer[n] = '\0';
if(n <= 0)
break;
for(count = 0; count < n; count++)
{
if(buffer[count] == 10)
{
more = 0;
break;
}
}
if(strcmp(buffer,"BYE\r\n")!=0)
printf("%s", buffer);
} while(more);
/*if(n <= 0)
{
break;
}*/
// printf("start of loop\n");
// printf("[[error check]] argc===== %d \n",argc);
if (pos< argc)
{
// printf("looping");
strcpy(temp,argv[pos]);
strcat(temp,"\r\n");
printf("\n");
//printf("[temp = %s\n]",temp);
write(sockFD, temp, strlen(temp));
}
else if(pos==argc)
{
if(strcasecmp(temp,"CLOSE\r\n")!=0)
{
strcpy(temp,"CLOSE");
strcat(temp,"\r\n");
printf("\n");
// printf("[!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!temp = %s]\n",temp);
write(sockFD, temp, strlen(temp));
}
}
// printf("here\n");
pos++;
// printf("pos == %d\n",pos);
/*for(pos=1;pos++;pos==argc)
{
printf("looping");
strcpy(temp,argv[pos]);
strcat(temp,"\r\n");
printf("\n");
write(sockFD, temp, strlen(temp));
}*/
} while(n > 0);
close(sockFD);
return 0;
}
else
{
printf("ERROR NETWORK CONNECTION FAIL 1");
}
}
else
{
printf("ERROR socket defention FAIL 2");
}
return 0;
}