管理员
论坛积分
分
威望 点
贡献值 个
金币 枚
|
大家好,感谢你的回复。% @5 c) r* V6 ?: c8 b
我用c 写了一个socket通信的小程序,写好了服务端和客户端,可遇到个问题,就是每次启动客户端只能发送第一条消息,之后服务端就不能再收到消息了。
9 W& g# F0 i7 i0 w2 G4 x- gTalk is cheak , show me the code.) X4 }3 g. P( h* b' j: x/ L* }
服务端代码:- J7 D% m6 P8 }1 ?1 f& p6 _- e
- #include<stdlib.h>. \$ B, O" p$ `0 s/ ]
- #include<WinSock2.h>
: A( j _; M9 P% B4 X - #pragma comment(lib,"ws2_32.lib")( w. `; l: O' Z: z
- int main(){' z+ M3 N Z- G) t* [* i! E* u
- WSADATA wsd;
! J1 A; t# F8 Z' t$ T - SOCKET sockServer;
0 V$ |) X M- d7 f5 v - SOCKADDR_IN serveraddr;// 服务端套接字 该结构中包含了要结合的地址和端口号
8 w( n0 {7 k3 U1 o2 K3 b8 @ - SOCKET sockClient;
! r: |3 G4 L2 _* N1 n- U; \ - SOCKADDR_IN clientaddr;
. |) p# Y" `# i; b' M0 t6 S) x- R - WSAStartup(MAKEWORD(2,2),&wsd);//初始化网络接口
) {2 N! E% c, o- o - sockServer = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);//创建服务器socket
& h3 _4 M: G1 e3 x+ ?+ V& X n& J
' z# [5 r! `0 `$ ~; ~- 4 y! u; c- v8 e$ ~% c) s( G8 w! n
- if (sockServer != -1 && sockServer != INVALID_SOCKET){
- V2 K& u# ?& {+ g9 q - printf("Socket has been created :%d", sockServer);
- O R! |( G% s7 E. _1 e - }7 ~! P6 V/ [& F6 \" o
- else{
' `; Y, F! V1 i) o: Z( ? - printf("Socket create failed.");
' J! X) W3 y! q- c0 r: U, |$ w - exit(0);
# ^! D$ D' o7 L$ E* }" d - }& p% k0 O- h: p7 o0 ~! O+ W
- serveraddr.sin_addr.S_un.S_addr = htonl(INADDR_ANY);
& Q& Z( S2 ]; s/ M4 X& j4 }; |9 i7 j - serveraddr.sin_family = AF_INET;
1 l" P" c: G( }9 {( \ - serveraddr.sin_port = htons(6000);//绑定端口60009 P7 d: q& n9 X( i
- int bindInfo = bind(sockServer, (SOCKADDR*)&serveraddr, sizeof(SOCKADDR));9 y8 ]8 M9 D J6 @; c% G
- char recvBuf[100];
4 e2 Y+ Q5 S% B+ S$ Q& V - int len = sizeof(SOCKADDR);# L1 f# ?8 }; j6 [0 W% X5 k1 b
- listen(sockServer, 5);//5为等待连接数
& t5 H6 m2 z) v. L0 B/ k! f - while (1){+ l, o0 o; S( P9 I) ?! H
- sockClient = accept(sockServer, (SOCKADDR*)&clientaddr, &len);
: }( }, |8 F# n; t9 C E. y4 G - //接收客户端数据2 g% B) o" l0 ~: |# [
- recv(sockClient, recvBuf, strlen(recvBuf)+1, MSG_PEEK);
2 X# s; ?: H' S - printf("to me:%s\n",recvBuf);+ M! K" t9 ?5 x" i2 T
- memset(recvBuf, 0, 100);8 ?( e7 q7 d& f0 L' W! @& \2 t
- }+ \% W3 ?8 Y4 C2 Q
- closesocket(sockClient);) {6 h k: E1 a* ~( Z0 ]. x
- system("pause");
: c4 w. b1 V( i C, [6 R - return 0;
2 r( j. M4 W+ u7 m0 V" M" J - }
复制代码 客户端代码:
9 W- s8 C5 M4 C4 s2 g- #include<winsock2.h>
2 U! L. B* F6 r" N8 b3 A - #include<stdio.h>
$ J2 H7 M- y) D; L5 Q - #pragma comment(lib, "ws2_32.lib")" ~, c! t5 \5 T# a5 H T
- void main()6 ?! v! m5 y, f& I4 J
- {3 ^8 [/ H F4 `2 [. f
- WSADATA wsaData;+ j( g0 j8 c8 \# E( b
- SOCKET sockClient;//客户端Socket* _$ Y: a8 R2 K! w
- SOCKADDR_IN addrServer;//服务端地址9 T- j& K& n. s- A+ V8 k& ?
- WSAStartup(MAKEWORD(2, 2), &wsaData);
$ y( b5 r6 ]. f* j( z$ g - char message[20] = "HelloSocket!";
& v4 E8 t2 B. k8 n - //定义要连接的服务端地址
: I. O& C7 l9 y! u - addrServer.sin_addr.S_un.S_addr = inet_addr("127.0.0.1");//目标IP(127.0.0.1是回送地址)
+ T0 n, Y% X! L# u7 g: m - addrServer.sin_family = AF_INET;/ s: E- L$ F0 s* Z: \- k) q
- addrServer.sin_port = htons(6000);//连接端口6000) ]$ K" H/ M6 s$ ?2 j! d0 k
- //新建客户端socket5 F2 ` d1 y9 M0 i6 j
- sockClient = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
* \. M& N0 G+ j0 d. E2 {' \ - int s = 0;; O% p* b+ q3 u0 |
- s = connect(sockClient, (SOCKADDR*)&addrServer, sizeof(SOCKADDR));8 g; A, E) w1 ~ V
0 m m' n' H, M0 R5 E& ]- while (1){
" o0 t, V2 F' _2 z6 @/ q- d( Y; Z - //发送数据
" N5 S6 F' `* @ - int a = send(sockClient, message, strlen(message) + 1, 0);
. G* t9 {; u+ M9 ]" d: T% w7 C - printf("to server:");( d5 t# n- o* Y# g! I: P: F
- scanf("%s", message);
6 y8 b% V+ c* Q2 [3 K1 l
7 Q6 W- {0 l" R7 X3 h- }2 T2 A2 ]4 F+ V( G
- //关闭socket
6 j1 |& C- o' {- U - closesocket(sockClient);
* j. }9 \( V3 S$ ^+ `* a0 T - WSACleanup();
5 h! S4 R- R1 d - }
复制代码 求懂的高手来解答一下。
0 G( p7 P$ |4 I+ ? U" [谢谢了!
- y2 M4 g3 N1 N6 T" w' J; {- |6 Z3 R4 ^+ k
( z& W6 v1 q8 a) b
; E. a: n; `( ~9 R" o6 H8 c
|
|