管理员
论坛积分
分
威望 点
贡献值 个
金币 枚
|
大家好,感谢你的回复。* s/ P% j& B! F- z$ Q
我用c 写了一个socket通信的小程序,写好了服务端和客户端,可遇到个问题,就是每次启动客户端只能发送第一条消息,之后服务端就不能再收到消息了。3 E0 |& F# [: q) [# A l3 M
Talk is cheak , show me the code.
% X1 B, l3 V/ X* D" B& e# X( e服务端代码:! t# P7 _% f* s3 f; d5 q: `& t
- #include<stdlib.h>
- T3 W( C1 k5 B - #include<WinSock2.h>
$ v" P! }. o6 Y+ n, W! Y* K - #pragma comment(lib,"ws2_32.lib")" I X- L* \% e
- int main(){$ N0 X5 }4 _ ~6 ?- r
- WSADATA wsd;8 I4 L7 s2 y7 V/ _, t5 d$ d
- SOCKET sockServer;
b1 y/ J, q8 B1 }2 o" C( { - SOCKADDR_IN serveraddr;// 服务端套接字 该结构中包含了要结合的地址和端口号! e9 J2 u1 q5 ?% s
- SOCKET sockClient;
* _# {7 k# f5 ?. S - SOCKADDR_IN clientaddr;
# w# j5 S$ L8 w4 j( w) R8 v4 z - WSAStartup(MAKEWORD(2,2),&wsd);//初始化网络接口
' o, O% B! F0 t - sockServer = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);//创建服务器socket
' l1 [& f# `6 ]2 l& a8 } - $ y% S0 y& x- e1 I# C
- ' k6 w' w7 w1 H' N) x
- if (sockServer != -1 && sockServer != INVALID_SOCKET){" M/ Q7 W E' Q- K; I
- printf("Socket has been created :%d", sockServer);
. T1 K: I) @1 {) h" v1 Q3 C - }+ F$ ?0 O' F9 m
- else{. P; ~' ^( q/ F+ \" j
- printf("Socket create failed.");2 \' G. p8 R/ u4 e! I
- exit(0);$ @+ L; m& K6 ~3 M& _ l4 H. `4 o7 `9 e' y
- }
3 ?7 P7 N( E# W - serveraddr.sin_addr.S_un.S_addr = htonl(INADDR_ANY);
# A) t9 _# W4 ` - serveraddr.sin_family = AF_INET;
, N( A' X$ P# x6 @ d - serveraddr.sin_port = htons(6000);//绑定端口6000: c! X1 z# R/ R' D
- int bindInfo = bind(sockServer, (SOCKADDR*)&serveraddr, sizeof(SOCKADDR));8 Y! J I+ X$ V' N+ A, k9 x) l
- char recvBuf[100];0 y5 l. l8 U/ d2 P4 P; o8 o: z
- int len = sizeof(SOCKADDR);
# J8 p5 ?5 x2 K1 F - listen(sockServer, 5);//5为等待连接数* X/ x# Y5 ?) x) l/ w x" w4 K+ e
- while (1){8 v1 p( u+ K2 W- S+ J& V
- sockClient = accept(sockServer, (SOCKADDR*)&clientaddr, &len);
S; t& J- p+ S8 N - //接收客户端数据! N2 @: {& t& g3 f' o. n2 Q- c, T$ i
- recv(sockClient, recvBuf, strlen(recvBuf)+1, MSG_PEEK); n0 {& x4 |, z
- printf("to me:%s\n",recvBuf);
& b4 {2 D' R, D7 G - memset(recvBuf, 0, 100);. s; b7 q9 U+ y8 S1 g, Z
- }
$ x% ]( `. s g/ ]' Q - closesocket(sockClient);- J0 K F/ ~8 l: r; C
- system("pause");+ r H: B% J/ L+ ~$ O+ c' K
- return 0;
1 b# q# P& D1 }' u; c% K: A - }
复制代码 客户端代码:, L& a4 }" j$ D' P0 A/ r* z
- #include<winsock2.h>
, K7 ^! t; g/ D) P: n2 f- q - #include<stdio.h>
K2 L/ d( W7 L5 \/ i2 Q3 v - #pragma comment(lib, "ws2_32.lib")
2 m9 v8 G; I/ e; F3 R' p0 K0 Q - void main()% T8 K4 n) s8 e+ v: l% x
- {3 X+ v! c% @; y% D' |
- WSADATA wsaData;( P+ h& z* F6 {+ i
- SOCKET sockClient;//客户端Socket* a* r, e8 q4 r- p3 Z" G9 R; Z
- SOCKADDR_IN addrServer;//服务端地址
* L: m6 E2 I- C V - WSAStartup(MAKEWORD(2, 2), &wsaData);4 @1 ]' z, a+ p$ e- L1 {. Y2 i
- char message[20] = "HelloSocket!";2 V( c8 d: @, p, U$ {3 l
- //定义要连接的服务端地址
! v: |; @ N H' R9 ^( K/ \ - addrServer.sin_addr.S_un.S_addr = inet_addr("127.0.0.1");//目标IP(127.0.0.1是回送地址)
3 t! u) [& l- X0 d: ^( J - addrServer.sin_family = AF_INET;3 c- ^ L e4 b9 `
- addrServer.sin_port = htons(6000);//连接端口6000
/ T5 b& i" V$ P5 r3 I# @! j) C - //新建客户端socket
4 V$ c6 z- m' p6 c' z9 U - sockClient = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);- A; e8 J5 ]' ~7 M
- int s = 0;
2 ]7 q `& `( c! ?( q; a - s = connect(sockClient, (SOCKADDR*)&addrServer, sizeof(SOCKADDR));
1 z' M1 }. Q: q; n
. g1 A; f) g8 n# Q" ?9 F4 t: ?# w- O- while (1){0 W8 V! b% i9 u) c5 \' R
- //发送数据$ i0 L5 c; | G$ @" r; `
- int a = send(sockClient, message, strlen(message) + 1, 0);7 q% u. h7 Y. M$ |& j# Q
- printf("to server:");
& L- M! \! i8 k* K" [1 p l - scanf("%s", message);
- R7 H- r/ A p. ~1 S6 }3 X i3 l" m
4 v4 p* P* ]8 t- }3 B) f; Z& q/ y7 k1 R w# ~" W* Q
- //关闭socket
* X- K5 w9 W0 z& @% y5 J9 ? - closesocket(sockClient);
1 C) P0 s- {. _. k) k# e& J H+ z) \9 S - WSACleanup();
% v/ U1 m7 o+ i/ R9 U - }
复制代码 求懂的高手来解答一下。
/ f0 c* @1 V2 I6 D3 [: G谢谢了!
) Q* J% K* X2 [: E: P* L0 A/ [
$ F0 U' ?7 N8 ?! C4 B* U v0 h9 S0 w
8 _. P/ {4 j, p, A4 G! y0 h3 | |
|