管理员
论坛积分
分
威望 点
贡献值 个
金币 枚
|
大家好,感谢你的回复。/ ?0 l/ T2 h4 F' H5 ?) ^- |
我用c 写了一个socket通信的小程序,写好了服务端和客户端,可遇到个问题,就是每次启动客户端只能发送第一条消息,之后服务端就不能再收到消息了。- K% ~! i) b( n' p/ h# h1 t1 e! G
Talk is cheak , show me the code.0 A, h O- I7 g0 S; [, f! Z
服务端代码:1 H# {8 O' t: Z B& o; G
- #include<stdlib.h>
3 \, M$ |( a8 Z4 r - #include<WinSock2.h>/ n: @) Q/ _5 p m2 h) V5 m
- #pragma comment(lib,"ws2_32.lib")5 Z. F0 q+ t' P
- int main(){
( h8 |6 {1 [& n( U. U' W - WSADATA wsd;' L: x' L# P/ b' T8 c4 t+ b6 b% d, Z) R
- SOCKET sockServer;
8 ~2 S' r7 p! Y8 W: ] - SOCKADDR_IN serveraddr;// 服务端套接字 该结构中包含了要结合的地址和端口号
3 O! F1 D% ~" p' O5 k - SOCKET sockClient;5 L( h+ P( P; P
- SOCKADDR_IN clientaddr;
) f( w# {& ?" W+ y7 ^1 {% F - WSAStartup(MAKEWORD(2,2),&wsd);//初始化网络接口
) o0 @7 O8 i; k" Z r( _4 X- Z - sockServer = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);//创建服务器socket: N2 E; o% b! _& F
9 V5 O7 i( R+ M! r8 [7 x
( q ?1 S$ Z+ d# ?1 P G- if (sockServer != -1 && sockServer != INVALID_SOCKET){
5 k0 k' z- V7 ^! c - printf("Socket has been created :%d", sockServer);
! J$ V0 @4 y' W* r$ S2 C/ W - }
! b3 ~7 u6 J7 q - else{
|/ f- z' ?- C' A3 j: H O7 I4 l3 T - printf("Socket create failed.");( w7 q" {0 m) m" `
- exit(0);
% M1 f3 ?) [( S' F - }
7 k4 }$ i* \2 T) }2 L - serveraddr.sin_addr.S_un.S_addr = htonl(INADDR_ANY);
3 e" F& K0 v' O' h8 V5 O - serveraddr.sin_family = AF_INET;# ~3 [8 T; b8 V+ ?1 i7 n
- serveraddr.sin_port = htons(6000);//绑定端口60001 ^1 u; H; D- O8 ^' q
- int bindInfo = bind(sockServer, (SOCKADDR*)&serveraddr, sizeof(SOCKADDR));
3 d) T- ]7 S% t - char recvBuf[100];* ~4 x% h U8 m6 m
- int len = sizeof(SOCKADDR);" m- `. i! ` R
- listen(sockServer, 5);//5为等待连接数
9 o. y& l2 w/ [! g: _ - while (1){
' p0 S( n0 X9 }7 r - sockClient = accept(sockServer, (SOCKADDR*)&clientaddr, &len);( v% d+ K: A$ d" }: \
- //接收客户端数据/ {, V- M4 F( F
- recv(sockClient, recvBuf, strlen(recvBuf)+1, MSG_PEEK);
: l, u4 j+ A- ^9 o$ H+ L2 ] - printf("to me:%s\n",recvBuf);# T' L( U% w; {$ ?% i$ L
- memset(recvBuf, 0, 100);
0 N) ~, g7 W+ Q - }) l4 x$ B2 p! z4 N" T3 T- [. _" \7 S
- closesocket(sockClient);
- i( g7 J2 p% c/ o( ~ - system("pause");
+ B% }8 B! I: q, ^8 e1 K" Q - return 0;5 q( u, {% ^. M% N$ T, f
- }
复制代码 客户端代码:/ A9 H c* i* k4 \
- #include<winsock2.h>2 I, } d; w1 K G- \7 t x2 M7 L
- #include<stdio.h>
7 Z8 H: ]! R6 A5 Y' x - #pragma comment(lib, "ws2_32.lib")1 @/ @, @ p0 V% D3 d2 [
- void main(): S& P0 V. ~9 W; A0 u+ |
- {
% n3 k. j; r# M$ s) P; A - WSADATA wsaData;
$ o% U5 e, g" i- G1 J9 h - SOCKET sockClient;//客户端Socket
: y' i0 o% H0 _3 q: U0 i - SOCKADDR_IN addrServer;//服务端地址9 x8 m4 O# F' h7 ~% p0 ~) ?8 a/ e$ C9 c
- WSAStartup(MAKEWORD(2, 2), &wsaData);
0 |7 T1 v7 _+ C( A5 k+ T- j - char message[20] = "HelloSocket!";
4 E p; i* d$ |1 u) I* o - //定义要连接的服务端地址
' O% `2 S6 {% h8 M/ u/ Q - addrServer.sin_addr.S_un.S_addr = inet_addr("127.0.0.1");//目标IP(127.0.0.1是回送地址)7 ?/ k+ d, N6 f0 \' }3 ]6 G
- addrServer.sin_family = AF_INET;
- ~% W" c( H+ L- i$ v - addrServer.sin_port = htons(6000);//连接端口6000
+ j5 S9 `; k b - //新建客户端socket4 w2 k6 c) F! [# N+ O' p0 T
- sockClient = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);& g& r/ x$ ~$ m" w5 w# a2 B
- int s = 0;5 T) h; s- V1 Y; E! W( v7 `
- s = connect(sockClient, (SOCKADDR*)&addrServer, sizeof(SOCKADDR));/ X; b2 v- O9 n/ ~& e
& ]+ x: o) q# b7 z( ?% @" B% L- while (1){" I8 t7 @; |' H
- //发送数据4 I" `! T1 K7 {' M2 T
- int a = send(sockClient, message, strlen(message) + 1, 0);5 R$ \0 A; \! b$ R4 `
- printf("to server:");( D9 W+ b" K7 ]! S% K! |
- scanf("%s", message);
k* v% X: F+ T$ E( p6 D* p m( W
' C5 N! x v- j: J% z- d! j6 y- }- q! N2 j/ z( l, N1 e) u& p. F, n
- //关闭socket
8 O3 O7 i; I4 p2 | - closesocket(sockClient);0 p; w$ R5 F$ l. s
- WSACleanup();
) _, ?2 W2 s+ [' z) U0 Q$ \6 M - }
复制代码 求懂的高手来解答一下。: ?! w: q, j+ v+ k" [, P5 o( Q
谢谢了!( K' k- q/ Z% S# d/ ~+ T
, z+ m2 Y- r) w6 Z# f+ U1 P f5 ~# F$ f' z j/ }1 v4 C/ @* v
, D* q P1 }0 H4 k |
|