管理员
论坛积分
分
威望 点
贡献值 个
金币 枚
|
大家好,感谢你的回复。
3 e7 a/ k& g$ h0 P7 A我用c 写了一个socket通信的小程序,写好了服务端和客户端,可遇到个问题,就是每次启动客户端只能发送第一条消息,之后服务端就不能再收到消息了。
c- v% B4 m1 S1 KTalk is cheak , show me the code.
: m2 n3 c& e2 x9 _* V! k' }+ d服务端代码:$ H3 @7 w% A2 T0 L8 W
- #include<stdlib.h>5 Y8 k" \6 D4 X) Y5 B& ]
- #include<WinSock2.h># W: }! X9 |* Z& G
- #pragma comment(lib,"ws2_32.lib")
" L9 R1 b( e m* A2 Y9 m - int main(){6 z$ G: a: K- H, C5 ^
- WSADATA wsd;7 g _, w; n+ g7 I$ l8 l; g
- SOCKET sockServer;- t8 l& E. G& ]1 ]. R! Y$ W! B
- SOCKADDR_IN serveraddr;// 服务端套接字 该结构中包含了要结合的地址和端口号6 Q) |: N5 }0 Z0 e) b
- SOCKET sockClient;
$ P! @7 T: c: V - SOCKADDR_IN clientaddr;5 f: P. J7 i$ v- V, H
- WSAStartup(MAKEWORD(2,2),&wsd);//初始化网络接口+ Y8 P0 b9 L9 C! B' o* }" _* t9 z
- sockServer = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);//创建服务器socket) N1 z* c a) i" r* ]
- ) \" L# _2 e0 c @6 I/ E
- ) U, q( l) b" r" [1 L
- if (sockServer != -1 && sockServer != INVALID_SOCKET){
5 M9 h; `2 f# k0 \( c - printf("Socket has been created :%d", sockServer);
- [5 G& c0 H/ S5 P ]/ ^; J' ?9 @ - }, f J/ X Z3 Y3 }; S, O
- else{
/ {7 @2 K! V. v* V/ a4 I9 V; j - printf("Socket create failed.");, p b5 Y# k$ Y5 {
- exit(0);
+ p' m: ]% c! } - }6 T1 Q3 L% T; `( ?9 ]
- serveraddr.sin_addr.S_un.S_addr = htonl(INADDR_ANY);4 V( a$ M9 ^0 |, L
- serveraddr.sin_family = AF_INET;
3 z+ i2 T- W1 d: l - serveraddr.sin_port = htons(6000);//绑定端口6000
/ s h& f& T+ Q n: }2 _ - int bindInfo = bind(sockServer, (SOCKADDR*)&serveraddr, sizeof(SOCKADDR));& U$ U! x5 [& [
- char recvBuf[100];
, I- ]7 u A: Z0 F) D - int len = sizeof(SOCKADDR);
, R1 u6 e! K' n/ K3 P - listen(sockServer, 5);//5为等待连接数
+ Y1 u3 T( Z3 j5 i - while (1){
, u/ R0 z. e3 g, Z8 M! T- b4 L - sockClient = accept(sockServer, (SOCKADDR*)&clientaddr, &len);
" ?/ W7 O L9 Z+ z* n9 m. \. c - //接收客户端数据
7 C; V7 N. w# @6 F" i8 u - recv(sockClient, recvBuf, strlen(recvBuf)+1, MSG_PEEK);
3 I, o3 R" t' }; o4 F - printf("to me:%s\n",recvBuf);; f0 d `9 W2 ]/ X0 P3 [
- memset(recvBuf, 0, 100);
Q* E0 Z2 h3 F- q5 v - }! \8 t; g+ D8 I
- closesocket(sockClient);1 y' ]4 v1 \2 F- ]4 J# D* [7 Y5 h0 T* j% C
- system("pause");3 |0 ?! V* q' G. M9 A4 F6 g
- return 0;
- w d# M/ V! \7 E2 C' Y1 W% A$ e - }
复制代码 客户端代码:
4 V1 T' d; ^. i" b7 ?8 C$ m- #include<winsock2.h>3 s1 k$ |1 U, O3 g! R7 P# q! o# o
- #include<stdio.h>
+ r. {3 s2 c; p- _" I - #pragma comment(lib, "ws2_32.lib")
7 n( M+ W4 o& l( X3 Q0 r - void main()3 d! Y) Z; y+ M/ b5 F2 R/ i
- {
1 A0 @. g- ^- r2 A0 K( {0 I - WSADATA wsaData;
4 O' c* ]+ K) s/ c4 j* V2 R$ `8 d - SOCKET sockClient;//客户端Socket: g4 b, Z' K3 h: _
- SOCKADDR_IN addrServer;//服务端地址
- R7 O) @1 y. S' k/ n; ` - WSAStartup(MAKEWORD(2, 2), &wsaData);
# A0 i( v9 R" A3 a+ I9 w - char message[20] = "HelloSocket!";
% i, y7 [ q& w. b% C0 f7 | - //定义要连接的服务端地址" }3 ^+ u" B( b2 f+ @3 N2 R
- addrServer.sin_addr.S_un.S_addr = inet_addr("127.0.0.1");//目标IP(127.0.0.1是回送地址)$ v4 c3 G1 S/ G4 l4 f
- addrServer.sin_family = AF_INET;8 \3 z* o2 A, P% I. `7 ^. U) K
- addrServer.sin_port = htons(6000);//连接端口6000
+ k( P6 r% ]4 m, c& }$ l9 j - //新建客户端socket
4 s7 l/ F! \) k1 ^ - sockClient = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);, V6 g5 |, r/ j! o' x
- int s = 0;
7 |$ M: D, v4 d C' I: U+ D1 M - s = connect(sockClient, (SOCKADDR*)&addrServer, sizeof(SOCKADDR));
7 A/ G8 E) a! R3 O - ; ^+ u' O* y, U3 \$ ]4 D8 t5 B; U
- while (1){1 t0 s& c. e: M6 D$ Q7 Q7 v+ h, }
- //发送数据+ `5 s6 T8 [: `' F
- int a = send(sockClient, message, strlen(message) + 1, 0);. Q( q, B7 y5 e1 {, S9 N9 G
- printf("to server:");5 F: `; }" |# z7 F: u5 ]1 f
- scanf("%s", message);2 I( D+ b4 b+ h& e F
- ) |1 {$ N ~& S2 V8 s; z
- }2 Z* b: A( S, P0 l5 K
- //关闭socket: d6 {6 w7 l" W0 P* `$ E
- closesocket(sockClient);
, j9 _' ?/ |$ Y# T2 C; o( z$ Q - WSACleanup();4 l; w3 B- X3 P: j
- }
复制代码 求懂的高手来解答一下。
# Z" p# w' b9 |1 {谢谢了!
3 X6 m' C" |$ X6 P3 p: X. V- @: f: ^( K J, V
4 g, G: x% {2 y3 |8 q$ T$ q# e5 Z5 j* b5 X. O# s
|
|