-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.c
More file actions
78 lines (60 loc) · 1.31 KB
/
client.c
File metadata and controls
78 lines (60 loc) · 1.31 KB
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
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <stdlib.h>
#include <string.h>
#include <pthread.h>
#include <unistd.h>
#define oops(msg) { perror(msg); exit(1); }
int sock_id;
void *receive();
int main(int ac, char *av[])
{
struct sockaddr_in servadd;
struct hostent *hp;
char message[BUFSIZ];
int messlen;
pthread_t t;
int res;
char str[BUFSIZ];
if(ac != 3)
{
fprintf(stderr, "usage: %s ip port", av[0]);
exit(1);
}
sock_id = socket(PF_INET, SOCK_STREAM, 0);
if(sock_id == -1)
oops("socket");
memset(&servadd, 0, sizeof(servadd));
servadd.sin_family = AF_INET;
servadd.sin_addr.s_addr = inet_addr(av[1]); // ip
servadd.sin_port = htons(atoi(av[2])); // portnum
if(connect(sock_id, (struct sockaddr *)&servadd, sizeof(servadd)) != 0)
oops("connect");
pthread_create(&t, NULL, receive, NULL);
while(1)
{
gets(str);
if(strcmp(str,"break")==0)
break;
write(sock_id, str, strlen(str)+1);
//read(sock_id, message, sizeof(message));
//printf("read: %s\n", message);
}
pthread_join(t, NULL);
close(sock_id);
return 0;
}
void *receive()
{
while(1)
{
char message[BUFSIZ];
recv(sock_id, message, sizeof(message), 0);
if(strcmp(message, "break") == 0)
break;
printf("receive: %s\n", message);
}
}