-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.c
187 lines (135 loc) · 3.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
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
#include<stdio.h>
#include<unistd.h>
#include<stdlib.h>
#include<fcntl.h>
#include<sys/types.h>
#include<sys/wait.h>
#include<string.h>
#include<ctype.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<netdb.h>
#include<errno.h>
#include<sys/ioctl.h>
#include<strings.h>
#include<pthread.h>
struct writeThreadStruct{
int socket;
pthread_t thread;
};
#define WELCOME "Welcome\n"
void * readThread(void *ptr);
void * writeThread(void *ptr);
int main(int argc, char * argv[])
{
while(1)
{
int sock;
struct sockaddr_in server;
struct hostent *hp;
/*Create socker*/
sock = socket(AF_INET, SOCK_STREAM, 0);
if(sock < 0)
{
perror("socket creating");
exit(1);
}
/*connect socket using name specified by the command line*/
server.sin_family = AF_INET;
write(STDOUT_FILENO,WELCOME,strlen(WELCOME));//opening promopt
write(STDOUT_FILENO,"type connect IP and Port\n",strlen("type connect IP and Port\n"));
char openningBuff[100];
char * token;
int openningRead = read(STDIN_FILENO,openningBuff,100);
if(openningBuff[0] == '\n')
token = "a";
else token = strtok(openningBuff," \n");
if(strcasecmp("connect",token) == 0)
{
int arguments = 0;
char *argumentsArray[2];
token = strtok(NULL," ");
while(token != NULL)
{
if(arguments > 1)
break;
argumentsArray[arguments] = token;
arguments++;
token = strtok(NULL," ");
}
if(arguments == 2)
{ hp = gethostbyname(argumentsArray[0]);
if( hp == 0)
{
exit(2);
}
bcopy(hp -> h_addr, &server.sin_addr, hp->h_length);
server.sin_port = htons(atoi(argumentsArray[1]));
if(connect(sock, (struct sockaddr *) &server, sizeof(server)) < 0)
{
perror("connect to stream socket");
exit(0);
}
/*create threads*/
pthread_t thread1, thread2;
/*pthread_attr_t myattr;
pthread_attr_init(&myattr);
pthread_attr_setdetachstate(&myattr , PTHREAD_CREATE_DETACHED);*/
int iret1 = pthread_create(&thread1, NULL,readThread,(void *)&sock);
struct writeThreadStruct writeT;
writeT.socket = sock;
writeT.thread = thread1;
int iret2 = pthread_create(&thread2, NULL,writeThread,(void *)&writeT);
pthread_join(thread1,NULL);
pthread_join(thread2,NULL);
close(sock);
}
else write(STDOUT_FILENO,"Invalid input\n",strlen("Invalid input\n"));
}
else write(STDOUT_FILENO,"Invalid input\n",strlen("Invalid input\n"));
}
}
void * readThread(void *ptr)
{
int * sock = (int *)ptr;
pthread_setcancelstate(PTHREAD_CANCEL_ENABLE,NULL);
pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS,NULL);
while(1)
{
char writeBuff[1000];
int readFromScreen = read(STDIN_FILENO, writeBuff, 1000);
if(readFromScreen == -1)
{
perror("error");
}
int writeCount = write(*sock, writeBuff, readFromScreen);
if(writeCount == -1)
perror("error");
}
}
void * writeThread(void *ptr)
{
struct writeThreadStruct *writeT = (struct writeThreadStruct *)ptr;
while(1)
{
char readBuff[1000];
int readFromSocket = read(writeT->socket, readBuff, 1000);
if(readFromSocket == -1)
{
perror("error");
}
int writeCount = write(STDOUT_FILENO, readBuff, readFromSocket);
readBuff[writeCount] = '\0';
if(strcmp("Connection terminated\n",readBuff) == 0)
{
exit(EXIT_SUCCESS);
}
if(strcmp("Connection disconnected\n",readBuff) == 0)
{
pthread_cancel(writeT->thread);
pthread_exit(NULL);
}
if(readFromSocket == -1)
perror("error");
}
}