管理员
论坛积分
分
威望 点
贡献值 个
金币 枚
|
大家好,感谢你的回复。' v/ ]$ ~) \- p: Y, I) k
我用c 写了一个socket通信的小程序,写好了服务端和客户端,可遇到个问题,就是每次启动客户端只能发送第一条消息,之后服务端就不能再收到消息了。2 T$ D* K7 x5 o) y' Z' ]9 n* g
Talk is cheak , show me the code.
9 G: K/ E1 P9 O( [3 |) C" E' Z服务端代码:& }8 p/ I8 R( h& m
- #include<stdlib.h>% ^7 m/ {) \' _% d3 w3 J# F* M$ T
- #include<WinSock2.h>! `1 K+ i, _& @/ h- T
- #pragma comment(lib,"ws2_32.lib"). S& }, }: j! C
- int main(){
3 y$ w* |7 L" A9 o, A+ R" {2 ` - WSADATA wsd;
G! K: \3 N* V r - SOCKET sockServer;0 m) q" J* Q5 o9 E0 }6 i; }; m
- SOCKADDR_IN serveraddr;// 服务端套接字 该结构中包含了要结合的地址和端口号: \. W3 ~9 w+ b% `
- SOCKET sockClient;8 j6 p, o1 `* g5 \" @0 u
- SOCKADDR_IN clientaddr;1 Q2 T; `4 D2 E2 A( ?/ G' S( J
- WSAStartup(MAKEWORD(2,2),&wsd);//初始化网络接口
! _& @9 e6 ~/ U! A8 P0 r - sockServer = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);//创建服务器socket p" @9 K( n, o+ @# o
1 \9 c0 s2 f# Z$ {! `- * m; S" z D$ t+ i: Q3 z j$ z
- if (sockServer != -1 && sockServer != INVALID_SOCKET){+ [3 ?0 v, o9 q' [4 ?1 q' L8 Y2 `/ F
- printf("Socket has been created :%d", sockServer);. G) w) R" a& i( B' B, E7 @" U
- }% O4 ]* z, s$ [. I: y
- else{! M9 N/ i2 M0 ^
- printf("Socket create failed.");% C! T3 t) b8 y* G5 ~, u
- exit(0);2 @: l3 R3 O& ~' c% u4 Y
- }! s8 B( E+ u$ Y
- serveraddr.sin_addr.S_un.S_addr = htonl(INADDR_ANY);
0 O0 x* B2 u" E/ U; v - serveraddr.sin_family = AF_INET;
P' @$ b: E3 }: ?& ^ - serveraddr.sin_port = htons(6000);//绑定端口6000
5 r) n4 q5 f5 B6 z, `0 f& R9 M - int bindInfo = bind(sockServer, (SOCKADDR*)&serveraddr, sizeof(SOCKADDR));
' E, |' W- f# U6 M- G7 Q - char recvBuf[100];2 M" `0 j d/ e9 U
- int len = sizeof(SOCKADDR);9 X( Z# `5 O! G6 A3 H( V
- listen(sockServer, 5);//5为等待连接数 s6 W" [6 W: h1 U- ]( f4 Z7 }
- while (1){
$ X \! B6 V P& G+ y9 h9 w# z - sockClient = accept(sockServer, (SOCKADDR*)&clientaddr, &len);
+ h/ `. }+ g% R& {& A$ A U - //接收客户端数据
7 ?. S( \9 R: q( |4 Y8 h - recv(sockClient, recvBuf, strlen(recvBuf)+1, MSG_PEEK);
% m0 I0 }5 b* e1 A; _ - printf("to me:%s\n",recvBuf);8 E3 }! }* ]2 y
- memset(recvBuf, 0, 100); E" f) g7 L; Y
- }
% F& k6 J, z' U: a5 h& T& A/ v - closesocket(sockClient);7 L( x& }* z( x3 O/ q5 T6 s
- system("pause");
. w$ q c4 v$ n8 U+ |7 z - return 0;
4 u) \" M8 A6 p4 U. o" E - }
复制代码 客户端代码:
1 z4 o6 i+ p- K% z4 e- #include<winsock2.h>' p: I8 [% G9 |2 \
- #include<stdio.h>
* q) Y: E4 p: K' |# B a - #pragma comment(lib, "ws2_32.lib")
: @1 F3 G' m; [- ? - void main()
, d# M9 |, ?. B( g9 {! h - { Z7 z3 }3 |* m
- WSADATA wsaData;4 p6 I2 W8 V1 Y% o& N$ s& d
- SOCKET sockClient;//客户端Socket/ E0 I. C# |: y$ S F" Q
- SOCKADDR_IN addrServer;//服务端地址/ m) Z* ^2 P P$ @. d* F
- WSAStartup(MAKEWORD(2, 2), &wsaData);
5 T& |: Y6 X* }( U8 e- w+ m0 G& p - char message[20] = "HelloSocket!";
|, k7 ~* D# [6 n& k5 e - //定义要连接的服务端地址
* x7 u$ s6 ?. s: j - addrServer.sin_addr.S_un.S_addr = inet_addr("127.0.0.1");//目标IP(127.0.0.1是回送地址); P# M1 m+ N! t e0 ~
- addrServer.sin_family = AF_INET;: ` D6 M' T9 N" `# u! K5 w1 M
- addrServer.sin_port = htons(6000);//连接端口6000
+ S- q: g* w9 N# l - //新建客户端socket5 B; b: w- W: g# B$ ]
- sockClient = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);8 {; @* c' N C. A7 u% f3 s" i
- int s = 0;5 R0 o3 @! h( E9 ~! r' z
- s = connect(sockClient, (SOCKADDR*)&addrServer, sizeof(SOCKADDR));
0 b4 b1 M" g* {; I - - Z* v4 y0 _0 X |9 [
- while (1){8 o: |; Q6 G B0 q- W" C& J( n
- //发送数据
6 p- n% V: b! [8 v# B$ u& g* m( F. C - int a = send(sockClient, message, strlen(message) + 1, 0);: b% t! F$ r: d2 i, }. h, B7 \6 ]) c% s" ^
- printf("to server:");
. R" T2 r7 p$ ?3 ]1 H5 F - scanf("%s", message);: S F# r" {9 Y/ G: J/ d
: P! G4 F7 l6 F7 b- }
5 p! [ p* V" ~* ~1 \ - //关闭socket
6 ` a7 ~' |7 ]! R& Z9 t - closesocket(sockClient);! E6 W" q% |0 v) O0 x; v6 e
- WSACleanup();8 F, B, N( T2 ]% H
- }
复制代码 求懂的高手来解答一下。1 X1 ~# ]' O( ^% c
谢谢了! v- f. [* V3 D5 l( m) g
7 u/ U* T9 A+ i) S T$ t
: @4 ]; i4 p* x4 W+ M6 h! \# j
( N# H$ ]0 Q7 n |
|