管理员
论坛积分
分
威望 点
贡献值 个
金币 枚
|
大家好,感谢你的回复。( [. M& |0 r$ {# \, _' b* G. J: _
我用c 写了一个socket通信的小程序,写好了服务端和客户端,可遇到个问题,就是每次启动客户端只能发送第一条消息,之后服务端就不能再收到消息了。8 A5 I/ G y3 R1 h4 O/ A
Talk is cheak , show me the code.3 G: ?6 `6 Q4 Z% f$ V9 }
服务端代码:7 e! M" E7 W; p2 _0 q
- #include<stdlib.h>
1 R8 f9 I5 W/ ]7 N0 d7 M F - #include<WinSock2.h>! e& s1 \* S' G$ l+ k- }" D; O- f
- #pragma comment(lib,"ws2_32.lib")4 H/ l0 p- Y5 ?; S
- int main(){
6 r# e. a, q% l$ l' V - WSADATA wsd;
- M X( i: {0 G6 i2 Z5 Y - SOCKET sockServer;% K6 u0 G4 }6 M1 g4 ^2 M
- SOCKADDR_IN serveraddr;// 服务端套接字 该结构中包含了要结合的地址和端口号+ @$ j% @1 j5 a" ] X1 `
- SOCKET sockClient;
, c. F4 Y2 ?( f2 K; H& L0 x - SOCKADDR_IN clientaddr;
$ V Q1 d3 O( i/ j! S0 j - WSAStartup(MAKEWORD(2,2),&wsd);//初始化网络接口
/ X) x9 Q' L/ e6 |: p - sockServer = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);//创建服务器socket5 V& c3 B; D; b; b5 U
- + D- P( C! m8 z& G$ k
- * |9 @) W6 s+ y: O# T
- if (sockServer != -1 && sockServer != INVALID_SOCKET){
( K( F. b1 w- K4 K, F - printf("Socket has been created :%d", sockServer);% f) P7 R1 O* Y1 g. _
- }, V m& ~+ d: ^& r" p
- else{8 ^2 M4 V+ N) c) ]
- printf("Socket create failed.");
, H, `. E# `! v6 o2 C - exit(0); g' U- l' d. r
- }' d( ?2 x5 x* z/ H5 r q
- serveraddr.sin_addr.S_un.S_addr = htonl(INADDR_ANY);# _( |0 @6 E, A: h9 G" @$ x$ [
- serveraddr.sin_family = AF_INET;
% H- W/ q1 T" G9 F0 n3 y( Z$ g - serveraddr.sin_port = htons(6000);//绑定端口6000
* P2 _: H9 f1 j - int bindInfo = bind(sockServer, (SOCKADDR*)&serveraddr, sizeof(SOCKADDR));
6 c/ B- l+ y0 ~, j, J - char recvBuf[100];7 ^; @. c( l/ ?
- int len = sizeof(SOCKADDR);( a* V, v$ t; s! Q5 i
- listen(sockServer, 5);//5为等待连接数
. D3 @9 W3 F' I. g4 _ - while (1){
5 q: I: S: Q, k$ U( B+ { - sockClient = accept(sockServer, (SOCKADDR*)&clientaddr, &len);
2 C' x6 `6 x4 b3 X$ N - //接收客户端数据
3 E4 _' k2 y8 I& @( O0 O4 F* e7 v/ ~ - recv(sockClient, recvBuf, strlen(recvBuf)+1, MSG_PEEK);# V0 ]8 {8 `( z' a n1 j/ Y! P8 ^
- printf("to me:%s\n",recvBuf);
2 u5 B C! W1 \' C - memset(recvBuf, 0, 100);1 ~' i/ n5 ?& o6 G
- }
$ q7 Y2 k9 T- _. G - closesocket(sockClient);
2 m# O" M9 K. K4 e: X$ v - system("pause");; [2 ?) d: I. c9 J
- return 0;7 A% Y! _( m2 G" x
- }
复制代码 客户端代码:
5 j2 G, \, E6 \4 |" C2 }& r9 {- #include<winsock2.h>3 v: e r. Q( ~7 p# f
- #include<stdio.h>% C" p5 _' P6 t& q
- #pragma comment(lib, "ws2_32.lib")( j0 q; r2 D$ V: F1 u
- void main()
8 a) y/ e z# h& Q, U: R+ |' c - {: D' g) B2 k' j1 [) ~
- WSADATA wsaData;7 k9 ]! `: |( b9 `! N
- SOCKET sockClient;//客户端Socket
5 `" E/ Y) [7 k" z0 s. [! A" `2 b# P4 u5 x - SOCKADDR_IN addrServer;//服务端地址
4 M, X6 M: L5 n- ~5 ^7 D `1 s% Y1 V - WSAStartup(MAKEWORD(2, 2), &wsaData);
3 ~. j/ @+ _7 Y7 ^1 B - char message[20] = "HelloSocket!";! q0 j$ Z' r2 F4 A' V. b
- //定义要连接的服务端地址 b0 h" k% t2 V; N0 W5 v
- addrServer.sin_addr.S_un.S_addr = inet_addr("127.0.0.1");//目标IP(127.0.0.1是回送地址)
! u) P0 U, h# L) Z6 E - addrServer.sin_family = AF_INET;
, E! W" R6 M" k' Y - addrServer.sin_port = htons(6000);//连接端口6000 s6 B* [' y/ o; n, \, V
- //新建客户端socket
$ t" i. Q: Y3 L: @" _* m, o - sockClient = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);2 F% m' ?0 C7 F6 l# o* z( e3 S3 N0 Q
- int s = 0;- e" h0 O& S- R# U8 V5 [
- s = connect(sockClient, (SOCKADDR*)&addrServer, sizeof(SOCKADDR));( Y+ j& _8 v- \ k* x! z
- & r1 X6 ^7 E0 H* g% _) Z
- while (1){/ d, U7 P5 `! f7 m# F8 R' O
- //发送数据
. g6 p! S, o7 w6 }5 c0 Y - int a = send(sockClient, message, strlen(message) + 1, 0);
/ ~; D* B/ s5 P4 { - printf("to server:");
- U- f7 r) h E0 e4 g* H. b9 d - scanf("%s", message);
9 o0 n2 J9 {! I H, }& e6 X
9 X! v0 D( \1 J! e! x- B- }
( A) x7 E& p% Q* U" T% a8 y - //关闭socket% _5 y: k! B" g6 o+ {$ B6 K0 W
- closesocket(sockClient);/ I9 l% c! K; i/ x# u$ m
- WSACleanup();
6 Z2 h" U* M* D9 D% Z/ f0 `3 L - }
复制代码 求懂的高手来解答一下。- M- O" M$ k& O+ Q& t% J
谢谢了!- I( x" O! `6 a6 q
) v! ]2 l2 J9 e+ P1 a9 J$ h5 L& y8 Z2 I! P; q
: }' }5 n$ [# u+ B. V9 }+ K
|
|