1 /////client
2
3
4
5 #include<stdio.h>
6 #include<stdlib.h>
7 #include<
string.h>
8 #include<sys/types.h>
9 #include<sys/socket.h>
10 #include<netinet/
in.h>
11 #include<arpa/inet.h>
12 #include<unistd.h>
13 #include<pthread.h>
14
15 void* Sendmsg(
void*
socket)
16 {
17 int *sock = (
int*
)socket;
18 while(
1) {
19 char sendmsg[
1024];
20 memset(sendmsg,
0,
sizeof(sendmsg));
21 scanf(
"%s",sendmsg);
22 size_t outputlength =
strlen(sendmsg);
23 if(
0==
outputlength)
24 {
25 return;
26 }
27 size_t bytecount = send(*sock,sendmsg,outputlength,
0);
28 if(bytecount<
0)
29 {
30 printf(
"send failed\n");
31 }
32 if(strcmp(sendmsg,
"exit")==
0)
33 {
34 exit(
0);
35 }
36 }
37
38 }
39 void* Receivemsg(
void*
socket)
40 {
41 int *sock =(
int*
)socket;
42 while(
1){
43 char msg[
1024];
44 memset(msg,
0,
sizeof(msg));
45 ssize_t bytecount = recv(*sock, msg,
sizeof(msg),
0);
//接收消息
46 if (bytecount <=
0) {
47 return;
48 }
else if(strcmp(msg,
"exit")==
0)
49 {
50 printf(
"Server Exit\n");
51 exit(
0);
52 }
53 else {
54 printf(
"Server>>:%s\n",msg);
55 }
56 }
57 }
58
59
60 int main(
int argc,
char*
argv[])
61 {
62
63 pthread_t sendPthread;
64 pthread_t recvPthread;
65 char ipaddr[
14]=
"127.0.0.1";
66 int port =
8888;
67 int sock =
socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
68
69 if (sock <
0)
70 {
71 printf(
"socket error!\n");
72 exit(
0);
73 }
74
75 struct sockaddr_in servAddr;
76 memset(&servAddr,
0,
sizeof(
struct sockaddr_in));
77 servAddr.sin_family =
AF_INET;
78
79 int rtn = inet_pton(AF_INET, ipaddr, &
servAddr.sin_addr.s_addr);
80
81 if (rtn ==
0) {
82 printf(
"address wrong,inet_pton failed!\n");
83 }
else if (rtn <
0) {
84 printf(
"inet_pton error!\n");
85 }
86
87 servAddr.sin_port =
htons(port);
88
89 if(connect(sock, (
struct sockaddr*)&servAddr,
sizeof(servAddr)) <
0)
//连接到服务器
90 {
91 printf(
"connect error!\n");
92 exit(
0);
93 }
94
95
96 pthread_create(&sendPthread,NULL,Sendmsg,&
sock);
97 pthread_create(&recvPthread,NULL,Receivemsg,&
sock);
98 pthread_join(sendPthread,
0);
99 pthread_join(recvPthread,
0);
100
101
102 close(sock);
103
104 return 0;
105 }
106
107
108
109 /////server
110
111 #include<stdio.h>
112 #include<stdlib.h>
113 #include<
string.h>
114 #include<sys/types.h>
115 #include<sys/socket.h>
116 #include<netinet/
in.h>
117 #include<arpa/inet.h>
118 #include<unistd.h>
119 #include<pthread.h>
120
121
122
123 void* Sendmsg(
void*
socket)
124 {
125 int *sock = (
int*
)socket;
126 while(
1)
127 {
128 char sendmsg[
1024];
129 memset(sendmsg,
0,
sizeof(sendmsg));
130 scanf(
"%s",sendmsg);
131 size_t outputlength =
strlen(sendmsg);
132 if(
0==
outputlength)
133 {
134 return;
135 }
136 size_t bytecount = send(*sock,sendmsg,outputlength,
0);
137 if(bytecount<
0)
138 {
139 printf(
"send failed\n");
140 }
141 if(strcmp(sendmsg,
"exit")==
0)
142 {
143 exit(
0);
144 }
145
146 }
147 }
148
149 void* Receivemsg(
void*
socket)
150 {
151 int *sock = (
int*
)socket;
152 while(
1){
153 char recvmsg[
1024];
154 memset(recvmsg,
0,
sizeof(recvmsg));
155 ssize_t bytecount = recv(*sock, recvmsg,
sizeof(recvmsg),
0);
//接收消息
156 if (bytecount <=
0) {
157 return;
158 }
else if(strcmp(recvmsg,
"exit")==
0)
159 {
160 printf(
"Client Exit\n");
161 exit(
0);
162 }
else {
163 printf(
"Client>>:%s\n",recvmsg);
164 }
165 }
166
167 }
168
169
170
171 int main(
int argc,
char*
argv[])
172 {
173
174 pthread_t sendPthread;
175 pthread_t recvPthread;
176 in_port_t servport =
8888;
177 int servSock;
178
179 if((servSock = socket(AF_INET,SOCK_STREAM,IPPROTO_TCP))<
0)
180 {
181 printf(
"socket error!\n");
182 }
183
184 struct sockaddr_in servAddr;
185 memset(&servAddr,
0,
sizeof(
struct sockaddr_in));
186 servAddr.sin_family =
AF_INET;
187 servAddr.sin_addr.s_addr=
htonl(INADDR_ANY);
188 servAddr.sin_port=
htons(servport);
189
190 if(bind(servSock,(
struct sockaddr*)&servAddr,
sizeof(servAddr))<
0)
191 {
192 printf(
"bind error!\n");
193 exit(
0);
194 }
195 if(listen(servSock,
5)<
0)
196 {
197 printf(
"listen filed!\n");
198 exit(
0);
199 }
200
201 struct sockaddr_in clntAddr;
202 socklen_t clntAddrLen =
sizeof(clntAddr);
203 int clntSock = accept(servSock,(
struct sockaddr*)&clntAddr,&
clntAddrLen);
204
205 if(clntSock <
0)
206 {
207 printf(
"accept error!\n");
208 }
209
210 char clntName[
1024];
211 if(inet_ntop(AF_INET,&clntAddr.sin_addr.s_addr,clntName,
sizeof(clntName))!=
NULL)
212 {
213 printf(
"Handling client: %s port :%d \n",clntName,ntohs(clntAddr.sin_port));
214 }
215 else
216 {
217 printf(
"client address get failed!\n");
218 }
219
220
221 pthread_create(&sendPthread,NULL,Sendmsg,&
clntSock);
222 pthread_create(&recvPthread,NULL,Receivemsg,&
clntSock);
223 pthread_join(sendPthread,
0);
224 pthread_join(recvPthread,
0);
225
226
227
228
229 close(servSock);
230 close(clntSock);
231
232 return 0;
233
234 }
转载于:https://www.cnblogs.com/fmonlyg/p/9207140.html