管理员
论坛积分
分
威望 点
贡献值 个
金币 枚
|
大家好,感谢你的回复。
. N; r* Y, \! s5 H+ |1 H我用c 写了一个socket通信的小程序,写好了服务端和客户端,可遇到个问题,就是每次启动客户端只能发送第一条消息,之后服务端就不能再收到消息了。& g8 |" z* x% g; B9 I: K. [
Talk is cheak , show me the code.
2 `" Q/ X. O* f; v5 {服务端代码:" ^' N% l8 E, O% A" p4 y: ?
- #include<stdlib.h>% m r/ u4 w1 e) i- w, k; R% D
- #include<WinSock2.h>
* n! u* A1 ]. e/ e* N# n. L/ b - #pragma comment(lib,"ws2_32.lib")) {/ Q" i2 {% u& e: D# }
- int main(){1 g6 i, A9 F+ ^- j1 h, {
- WSADATA wsd;& {% }/ b" g9 X2 B9 X U) D
- SOCKET sockServer;
0 [$ \ B" S% @" M. c0 O5 ^- y5 X - SOCKADDR_IN serveraddr;// 服务端套接字 该结构中包含了要结合的地址和端口号2 b, x& A, w* I6 z
- SOCKET sockClient;
8 T W9 ~; ~8 I2 V# p: j1 \# S) }; Z( [ - SOCKADDR_IN clientaddr;/ T" b' r" l6 G# h% Y. o& ~8 C
- WSAStartup(MAKEWORD(2,2),&wsd);//初始化网络接口" J2 M5 A0 h4 v/ k* L( j8 t. o0 {) h% }
- sockServer = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);//创建服务器socket, P. ^+ j, N9 h2 l$ N# E
# O' a, c+ b# ]+ ^- 6 g8 \/ J4 ?$ E z9 E& y# W
- if (sockServer != -1 && sockServer != INVALID_SOCKET){/ _9 i% H. @% I6 [7 Y
- printf("Socket has been created :%d", sockServer);
8 ~& u8 m0 d# q% J. x! c - }7 O) K( l) J z1 ]3 R
- else{
3 F% P. @1 \3 {) Z& r3 M - printf("Socket create failed.");
+ O4 A/ X6 F2 C$ F# {4 j - exit(0);
3 c6 S2 V% C; F: M# a% e. A - }
5 w* n7 p& H. G7 y2 W - serveraddr.sin_addr.S_un.S_addr = htonl(INADDR_ANY);
1 Z9 `! v2 T1 R _/ D9 f - serveraddr.sin_family = AF_INET;8 B* H% S9 _: t! D
- serveraddr.sin_port = htons(6000);//绑定端口6000
9 ~" y6 Y* E& d! L0 } - int bindInfo = bind(sockServer, (SOCKADDR*)&serveraddr, sizeof(SOCKADDR));
c: V. g, P$ i, W - char recvBuf[100];" T$ y. {- ?% @0 t/ ?" d3 i+ a
- int len = sizeof(SOCKADDR);
. r" L% E) q( A - listen(sockServer, 5);//5为等待连接数$ F3 }: v# A: H, k. @ @
- while (1){
4 N+ X+ w3 K4 O - sockClient = accept(sockServer, (SOCKADDR*)&clientaddr, &len);+ U6 N- j. a# e4 j, _, F# q
- //接收客户端数据
. ~% h# m3 |4 f - recv(sockClient, recvBuf, strlen(recvBuf)+1, MSG_PEEK);
$ ^! L" `4 Q- Q: r: ^6 k - printf("to me:%s\n",recvBuf);8 r3 b3 F" Z+ [
- memset(recvBuf, 0, 100);
8 y% V: @7 W3 G3 U {/ \7 f - }( A5 j% h. a! m6 t# {) x
- closesocket(sockClient);, i$ l3 C( n- J
- system("pause");
4 T/ ?6 J( w. x' g9 t, c/ I# J - return 0;
. x4 R5 k6 N- e - }
复制代码 客户端代码:- }3 o- W, U- J ^
- #include<winsock2.h>
( q4 E, m- a# R9 m - #include<stdio.h>
6 [: `+ h" Q7 V1 v- b - #pragma comment(lib, "ws2_32.lib")
5 b: e. E! ]/ |2 Q0 }- Y5 C - void main()1 a% O- [/ C+ V
- {
5 K) b, H' p& `8 b - WSADATA wsaData;
8 W% R0 c4 p; m2 Z) e" J |# g/ x x; l2 k - SOCKET sockClient;//客户端Socket0 y; f8 l- s% E$ s* W
- SOCKADDR_IN addrServer;//服务端地址6 ?) I k7 x, t$ h7 o! S0 q! t
- WSAStartup(MAKEWORD(2, 2), &wsaData);( V! J" t' E! O% u+ B2 @6 Z& h
- char message[20] = "HelloSocket!";
K! G s" `2 k/ P - //定义要连接的服务端地址
+ `# e+ M, U4 L0 N* l - addrServer.sin_addr.S_un.S_addr = inet_addr("127.0.0.1");//目标IP(127.0.0.1是回送地址)
, s9 a' N1 z; m( X. m# Z7 Y - addrServer.sin_family = AF_INET;
9 {$ j3 r- z9 t& j5 ~ - addrServer.sin_port = htons(6000);//连接端口6000
- v7 \/ H% {" o - //新建客户端socket
# p* G3 p1 M& G5 A( R' W - sockClient = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);: x2 N/ V6 Q* W/ _4 [
- int s = 0;2 C' W4 [( ?/ v i0 x1 b% K/ T
- s = connect(sockClient, (SOCKADDR*)&addrServer, sizeof(SOCKADDR));
0 U8 W% m1 C' {! E. v0 h6 ?# n$ Y# e
- |6 z) x0 {& a) _! D" V: a- while (1){8 q) o3 \0 k, {! t
- //发送数据& a: i8 `- O- O
- int a = send(sockClient, message, strlen(message) + 1, 0);
" C; n0 B D9 |' N6 O4 v3 p2 }4 E4 ` - printf("to server:");+ C1 D2 v+ H/ V6 O9 `( N
- scanf("%s", message);
9 P4 G, h/ {( [9 U. F* C+ B9 d. i" V9 M - 7 \( F4 z, L. @
- }
4 L: ?% e4 p8 ` - //关闭socket" O# ?0 t0 o3 O. W9 X/ D I* e+ f3 ]
- closesocket(sockClient);
4 q6 r. l, `7 p - WSACleanup();( F7 d {' x" f: m2 e5 R4 j
- }
复制代码 求懂的高手来解答一下。
0 M7 f, {7 Y' N) @: c谢谢了!/ ~8 H- t" [: K0 |: p# y
8 [+ p: J8 w/ ~, v$ x2 T X/ S# ^5 Y2 u
9 x* z/ v4 r+ d' ^/ _& { T
|
|