管理员
论坛积分
分
威望 点
贡献值 个
金币 枚
|
大家好,感谢你的回复。& m1 x `8 O i' B0 k8 o
我用c 写了一个socket通信的小程序,写好了服务端和客户端,可遇到个问题,就是每次启动客户端只能发送第一条消息,之后服务端就不能再收到消息了。
1 m5 t! \+ W/ GTalk is cheak , show me the code.
1 z0 S) {; p2 T* I! o* e服务端代码:
0 [6 w s8 \1 M: D/ i7 z- #include<stdlib.h>" f' @9 i2 u8 M% _% T+ x# ]$ @
- #include<WinSock2.h>
6 N( t0 F) U6 z) J: U) H% A - #pragma comment(lib,"ws2_32.lib")
4 }6 C3 d3 b: [ - int main(){( h( S: }7 {2 ^. P0 U8 u- i
- WSADATA wsd;/ H9 k% V. f! ~' q3 s7 m# S
- SOCKET sockServer;. F* _2 I, U. w8 u, h2 s5 ?
- SOCKADDR_IN serveraddr;// 服务端套接字 该结构中包含了要结合的地址和端口号8 M. l2 n, E3 |0 A t& k
- SOCKET sockClient;
* ~8 n0 |- Y9 E. F; j - SOCKADDR_IN clientaddr;
: R5 ?6 Y z1 ^9 H9 q - WSAStartup(MAKEWORD(2,2),&wsd);//初始化网络接口
& S/ V) a* I, l; f# K6 R2 Y - sockServer = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);//创建服务器socket) e: t/ k& z k1 q: T3 k
/ h- ?6 f5 ~+ N; U6 }- ]- 7 t9 `& y) T+ }9 O8 c% p1 b
- if (sockServer != -1 && sockServer != INVALID_SOCKET){) [0 k; E3 E2 L0 W4 b; p
- printf("Socket has been created :%d", sockServer);, p6 X4 ^- B& J( d" T( k( U/ r
- }6 T3 q( Q/ ^* b2 d- `* N& b
- else{
/ R/ \& ]2 V5 K& u! W; x$ V. v - printf("Socket create failed.");2 ^! W2 e" g! P" z# f
- exit(0);6 R: i8 M: i8 T+ z4 f0 O
- }4 V9 j' Q4 z3 M/ e! W2 m! O. K4 J
- serveraddr.sin_addr.S_un.S_addr = htonl(INADDR_ANY);
2 L# h. A+ f0 r8 a3 i, _3 h - serveraddr.sin_family = AF_INET;
$ V" |) j- j4 n$ z) A3 o& W" D$ y. ` - serveraddr.sin_port = htons(6000);//绑定端口6000
+ X# f) p, F$ Y - int bindInfo = bind(sockServer, (SOCKADDR*)&serveraddr, sizeof(SOCKADDR));8 u! k7 k$ Y6 b2 D8 j/ k
- char recvBuf[100];
# B, J2 E; M3 `, ` - int len = sizeof(SOCKADDR);
& W% `, I/ ^6 Z% P. k - listen(sockServer, 5);//5为等待连接数 f1 c* K9 n2 U; n1 z+ _& i
- while (1){! }6 \& T1 U: T4 C9 K
- sockClient = accept(sockServer, (SOCKADDR*)&clientaddr, &len);
; r8 r% M* H2 F; ~7 k8 X3 g - //接收客户端数据
( _+ o( a Y+ H - recv(sockClient, recvBuf, strlen(recvBuf)+1, MSG_PEEK);
8 e* O3 \5 _- U. r: W0 P2 p - printf("to me:%s\n",recvBuf);: @; w5 L$ q5 I% ^6 Q
- memset(recvBuf, 0, 100);
W1 ?: O5 v4 o3 f$ T - }
7 l" Y' w) L5 W/ ^# U* {7 t) Q - closesocket(sockClient);+ F. R4 L, t) L6 J
- system("pause");
: T; A5 x+ ]2 d" E4 e7 S5 |1 Q5 D, I - return 0;
# ~& w" m$ s( N3 Y+ i6 g - }
复制代码 客户端代码:- P: y/ S. A% n. O2 Y! Y
- #include<winsock2.h>; D/ s) ]5 W3 }6 G# ]
- #include<stdio.h>
1 f! r' t8 B) A# [1 W - #pragma comment(lib, "ws2_32.lib"). ?) w8 ], m) M* _* F& @, T+ c
- void main()
# E( b$ a0 Y$ b1 q! S - {
- r7 c" r* G4 n: _8 I' \7 g! _: t - WSADATA wsaData;' |8 ~9 M+ Y- X. ]6 w
- SOCKET sockClient;//客户端Socket, E, C- \1 _$ V
- SOCKADDR_IN addrServer;//服务端地址, ~) ~) ]1 P$ d4 j( R) J+ R8 l
- WSAStartup(MAKEWORD(2, 2), &wsaData);
+ a5 b: u3 c1 O& E - char message[20] = "HelloSocket!";3 k2 B1 ?4 q9 `
- //定义要连接的服务端地址) z# |( E0 k6 l2 B- a
- addrServer.sin_addr.S_un.S_addr = inet_addr("127.0.0.1");//目标IP(127.0.0.1是回送地址)
4 l$ @8 ~/ I! K& U' _7 @) e - addrServer.sin_family = AF_INET;4 ]- D$ d2 |+ O
- addrServer.sin_port = htons(6000);//连接端口60005 Q6 U- ^# y' Q9 I( X+ a) M
- //新建客户端socket
% _# V4 R3 u5 s# @, N- }: D - sockClient = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);5 W7 I. z) v5 S/ `0 U+ C
- int s = 0;& {& q( r& P% O3 ~
- s = connect(sockClient, (SOCKADDR*)&addrServer, sizeof(SOCKADDR));
! I: q) p" Z9 I# i - - |! y f; ?) y. \$ C$ @
- while (1){
4 W0 C! p8 z! ] - //发送数据 L: C! A: p' `
- int a = send(sockClient, message, strlen(message) + 1, 0);# w2 h/ u4 L! T) i
- printf("to server:");8 G- F; R2 g& U |7 B
- scanf("%s", message);
& Z4 \( F# N1 |! V4 c% O
$ ~; h* h; ?5 n, l- o9 n& Q- }# Y: b0 q7 X. |4 G+ h
- //关闭socket
" g' L3 _# }0 D, ^' r# O0 R* Q - closesocket(sockClient);
" f0 v. w9 f* p - WSACleanup();
6 W; J! E( U/ ], W8 F - }
复制代码 求懂的高手来解答一下。
8 [. i, x1 a. \4 J. P% B谢谢了!+ G% c. h% a9 J! }+ u$ \- K
1 q% D2 W5 D6 P; @3 t! i% j7 z2 T) b0 F9 }1 v
6 A. Z+ N2 R, Y
|
|