-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.c
157 lines (127 loc) · 3.35 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
/* A simple client in the internet domain using localhost server
and sending and receiving messages asynchronously
The port number and localhost is passed as an argument
*/
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <stdlib.h>
#include <unistd.h>
#include <pthread.h>
#include <signal.h>
#define h_addr h_addr_list[0] /* for backward compatibility */
pthread_t listener, writer;
int serverSocket;
void error(char *msg)
{
perror(msg);
exit(0);
}
void init(int args,char **argv)
{
if (args < 3) {
fprintf(stderr,"usage %s hostname port\n", argv[0]);
exit(0);
}
}
int clientAssignSocket()
{
int sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd < 0){
error("ERROR opening socket");
}
return sockfd;
}
void clientConnectToServer(char **argv, int portno, int sockfd)
{
struct sockaddr_in serv_addr;
struct hostent *server;
server = gethostbyname(argv[1]);
if (server == NULL) {
fprintf(stderr,"ERROR, no such host\n");
exit(0);
}
bzero((char *) &serv_addr, sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
bcopy((char *)server->h_addr,
(char *)&serv_addr.sin_addr.s_addr,
server->h_length);
serv_addr.sin_port = htons(portno);
if (connect(sockfd,(struct sockaddr *)&serv_addr,sizeof(serv_addr)) < 0)
error("ERROR connecting");
}
void clientWrite(int sockfd,char buffer[])
{
int n;
bzero(buffer,256);
fgets(buffer,255,stdin);
n = write(sockfd,buffer, strlen(buffer)-1);
if (n < 0) error("ERROR reading from socket");
}
void clientRequireClose()
{
int n;
n = write(serverSocket,"bye",3);
if (n < 0) error("ERROR reading from socket");
pthread_cancel(listener);
pthread_cancel(writer);
close(serverSocket);
}
void clientRead(int sockfd,char buffer[])
{
int n;
bzero(buffer,256);
n = read(sockfd,buffer,255);
if (n < 0) error("ERROR reading from socket");
printf("%s\n",buffer);
}
void treatSignal(int sinal)
{
if(sinal==2 || sinal==15){
clientRequireClose();
}
}
void *clientListener(void *socket){
char buffer[256];
int sockfd;
sockfd = *(int *)socket;
bzero(buffer,256);
while (memcmp(buffer,"server close",strlen("server bye")) != 0) {
clientRead(sockfd,buffer);
}
pthread_cancel(writer);
pthread_cancel(listener);
return NULL;
}
void *clientWriter(void *socket){
char buffer[256];
int sockfd;
signal(2, treatSignal);
signal(15, treatSignal);
sockfd = *(int *)socket;
bzero(buffer,256);
while (memcmp(buffer,"bye",strlen("bye")) != 0 && memcmp(buffer,"server close",strlen("server bye")) != 0) {
clientWrite(sockfd,buffer);
}
pthread_cancel(writer);
pthread_cancel(listener);
return NULL;
}
int main(int argc, char *argv[])
{
int sockfd, portno;
init(argc,argv);
portno = atoi(argv[2]);
sockfd = clientAssignSocket();
serverSocket = sockfd;
clientConnectToServer(argv,portno,sockfd);
pthread_create(&listener, NULL, clientListener, &sockfd);
pthread_create(&writer, NULL, clientWriter, &sockfd);
pthread_join(listener, NULL);
pthread_join(writer, NULL);
close(sockfd);
return 0;
}