-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathclient_select.c
118 lines (88 loc) · 2.11 KB
/
client_select.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
#include <stdio.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <pthread.h>
pthread_t thread;
char msg[1024];
extern int errno;
int main(int argc, char *argv[]){
struct sockaddr_in server;
int sockFD;
int ok = 1;
fd_set readfds;
fd_set actfds;
int nfds;
int comment;
if ((sockFD = socket(AF_INET,SOCK_STREAM,0)) == -1)
{
perror("err while socketing\n");
return 0;
}
bzero(&server,sizeof(server));
int port = atoi(argv[2]);
server.sin_family = AF_INET;
server.sin_addr.s_addr = inet_addr(argv[1]);
server.sin_port = htons(port);
if (connect(sockFD,(struct sockaddr*) &server,sizeof(server)) == -1)
{
perror("err while connecting\n");
return 0;
}
char msg[1024];
bzero(msg,1024);
FD_ZERO (&actfds);
FD_SET (sockFD, &actfds);
FD_SET (0, &actfds);
nfds = sockFD;
while (strcmp(msg,"quit") != 0){
bcopy ((char *) &actfds, (char*) &readfds, sizeof(readfds));
if( select( nfds+1, &readfds, NULL, NULL, NULL) <0 ){
perror("eroare la select");
return errno;
}
int k = 0;
bzero(msg,1024);
if( FD_ISSET(sockFD, &readfds) ){
if (read(sockFD,msg,1024) <= 0)
{
perror("err while reading from server\n");
return 0;
}
printf("\n%s\n",msg);
if(strncmp(msg,"Ai fost delogat cu succes",25) == 0) strcpy(msg,"quit");
if(strncmp(msg,"Have a nice one :)",18) == 0) strcpy(msg,"quit");
if(strncmp(msg,"Comentariu:",11) == 0) comment = 1;
else comment = 0;
}
if( FD_ISSET(0, &readfds) ){
if(strcmp(msg,"quit") != 0){
if(comment == 1){
int k = 0;
char msg1[10000];
strcpy(msg1,"");
int terminated = 0;
while(!terminated){
fgets(msg,1024,stdin);
strcat(msg1,msg);
if(strcmp(msg,"\n") == 0) ++k;
if(strcmp(msg,"\n") == 0 && k == 1) terminated = 1;
if(strcmp(msg,"\n") != 0 && k == 1) k = 0;
}
msg1[strlen(msg1)-2] = '\0';
if (write(sockFD,msg1,strlen(msg1)) <= 0){
perror("err while writing to server\n");
return 0;
}
}
else {
fgets(msg,1024,stdin);
if (write(sockFD,msg,sizeof(msg)) <= 0){
perror("err while writing to server\n");
return 0;
}
}
}
}
}
return 0;
}