-
Notifications
You must be signed in to change notification settings - Fork 0
/
Client.c
108 lines (63 loc) · 1.61 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
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <unistd.h>
#include <netdb.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <sys/types.h>
#include <arpa/inet.h>
#include <pthread.h>
#define MAXLINE 100
void *threadsend(void *vargp);
void *threadrecv(void *vargp);
void *threadsend(void *vargp)
{
int connfd = *((int *)vargp);
int idata;
char temp[100];
while(1){
fgets(temp, 100, stdin);
send(connfd, temp, 100, 0);
printf("<< Client Send OK >>\n");
}
printf("client has sended");
return NULL;
}
void *threadrecv(void *vargp)
{
char temp[100];
int connfd = *((int *)vargp);
while(1){
int idata = 0;
idata = recv(connfd, temp, 100, 0);
if(idata > 0){
printf("<Server>:\n%s\n", temp);
}
}
return NULL;
}
int main()
{
int *clientfdp;
clientfdp = (int *)malloc(sizeof(int));
*clientfdp = socket(AF_INET, SOCK_STREAM, 0);
struct sockaddr_in serveraddr;
struct hostent *hp;
bzero((char *)&serveraddr, sizeof(serveraddr));
serveraddr.sin_family = AF_INET;
serveraddr.sin_port = htons(15636);
serveraddr.sin_addr.s_addr = inet_addr("127.0.0.1");
if(connect(*clientfdp, (struct sockaddr *)&serveraddr, sizeof(serveraddr)) < 0){
printf("connect error\n");
exit(1);
}
pthread_t tid1, tid2;
printf("connected\n");
while(1){
pthread_create(&tid1, NULL, threadsend, clientfdp);
pthread_create(&tid2, NULL, threadrecv, clientfdp);
}
return EXIT_SUCCESS;
}