管理员
论坛积分
分
威望 点
贡献值 个
金币 枚
|
大家好,感谢你的回复。
$ r. t8 M# Q! F! i: r, w' ~6 P q. R我用c 写了一个socket通信的小程序,写好了服务端和客户端,可遇到个问题,就是每次启动客户端只能发送第一条消息,之后服务端就不能再收到消息了。* g/ T* s4 a' D% K, K; w
Talk is cheak , show me the code.
; ?9 }9 z& `, \服务端代码:& l+ g7 n" A! d9 [; Z" n, v
- #include<stdlib.h>
" Y( ]% X( H7 T1 d1 b# I9 ] - #include<WinSock2.h>
7 ^% }; S m" S& z5 E3 \* J - #pragma comment(lib,"ws2_32.lib")8 r& U3 M/ t7 ~" Y1 F! E7 Z
- int main(){7 W* G: ?% @+ t6 M
- WSADATA wsd;
( U# ~# w. Y; V" C - SOCKET sockServer;# F& r. |/ I1 @5 y7 x% z
- SOCKADDR_IN serveraddr;// 服务端套接字 该结构中包含了要结合的地址和端口号
% J$ f4 e. \' d& |# e - SOCKET sockClient;
( j/ M* Z+ h. O5 n3 M - SOCKADDR_IN clientaddr;/ d q6 d5 y7 }+ I- g: {9 I
- WSAStartup(MAKEWORD(2,2),&wsd);//初始化网络接口0 X6 ]+ ]# ~+ h7 ?8 I* P+ F( Z
- sockServer = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);//创建服务器socket
% B) R+ ` n! s9 S; T9 E - 8 o: Q$ [+ L& h; E" p# p
- * a: ~& r0 F# \, b# K: ^, D
- if (sockServer != -1 && sockServer != INVALID_SOCKET){' ^+ f3 o ?. Q% T$ }3 L
- printf("Socket has been created :%d", sockServer);
% Z7 m2 M& A! r9 z/ M - }
3 T/ b& c: G% f7 a9 i8 F2 v% o: r - else{
: Q6 O. D+ x4 e. T - printf("Socket create failed.");' H# M$ k, G" T$ K8 N) E3 q
- exit(0);
6 b" q+ o1 T& m. T( S1 |+ A - }
/ ~9 Q6 i$ S+ m# e - serveraddr.sin_addr.S_un.S_addr = htonl(INADDR_ANY);* a: G- g) n0 ~! z4 @
- serveraddr.sin_family = AF_INET;2 C' o0 e6 T( C/ s1 Y- n
- serveraddr.sin_port = htons(6000);//绑定端口6000- e) E4 d( E& n! j) l5 b4 @2 Z4 _
- int bindInfo = bind(sockServer, (SOCKADDR*)&serveraddr, sizeof(SOCKADDR));
) ~6 _3 r2 U3 ]* B - char recvBuf[100];4 |7 e8 R8 v4 P( n0 m/ M/ J% Z
- int len = sizeof(SOCKADDR);
) j, \, v3 h* b& d) \; I - listen(sockServer, 5);//5为等待连接数
9 [7 z1 ?; K. J: Q y9 M: F - while (1){
* v/ f7 ^/ o. p3 Q% L! Q3 U - sockClient = accept(sockServer, (SOCKADDR*)&clientaddr, &len);7 U' O- t; c& X" k% C2 Y) r
- //接收客户端数据5 k' N; b6 T1 p' E! g& B
- recv(sockClient, recvBuf, strlen(recvBuf)+1, MSG_PEEK);
2 `: E' s& H g5 { - printf("to me:%s\n",recvBuf);$ ?5 H2 t9 B+ I, E3 E2 l7 }" H
- memset(recvBuf, 0, 100);
3 h3 f: d' H3 l. |0 Z - }
) x2 E2 ~, g9 E- A$ { }6 F& l' U7 o - closesocket(sockClient);& H v% \9 n8 ^
- system("pause");" k q! P% G0 T: L# D
- return 0;, U5 |( |% p' F2 m8 ?
- }
复制代码 客户端代码:
* k9 S: C' E, A: M: j) z# v$ i+ v- #include<winsock2.h>
2 u6 t5 c# f" S - #include<stdio.h>" a9 e, S8 V0 J6 E% E
- #pragma comment(lib, "ws2_32.lib")
1 O* v: R7 P! Z2 d - void main()
1 u( m2 e9 q2 Y2 O - {
& \ }2 [0 P& E j6 D; r% F- G - WSADATA wsaData;$ P9 T0 c( [6 X+ Y: h( H1 m
- SOCKET sockClient;//客户端Socket
& @$ { `0 L5 W+ D" x6 s - SOCKADDR_IN addrServer;//服务端地址7 y0 k) Y1 a/ P2 o- d0 w2 C( g
- WSAStartup(MAKEWORD(2, 2), &wsaData);
! h4 ~* b7 m/ [( O% b: ` - char message[20] = "HelloSocket!";7 P( s1 `3 i. a" e( o1 D
- //定义要连接的服务端地址: M6 C2 S! o0 x& H/ p! q3 W! B! x
- addrServer.sin_addr.S_un.S_addr = inet_addr("127.0.0.1");//目标IP(127.0.0.1是回送地址)
8 g$ u( r! L3 ?% X - addrServer.sin_family = AF_INET;# q* V, J9 D* e6 G4 z' B. k
- addrServer.sin_port = htons(6000);//连接端口6000' h) H$ H8 b, k. E2 a7 t: Y
- //新建客户端socket
) }1 I& D3 z1 N: Z - sockClient = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
0 h1 N* u1 W e8 } - int s = 0;
9 x" v$ P5 l( |2 @- D3 {+ U - s = connect(sockClient, (SOCKADDR*)&addrServer, sizeof(SOCKADDR));
1 O3 {/ z$ z. h4 _+ \ - : l# Y' [* c7 X8 w0 P8 h3 c
- while (1){; B8 o! s2 v) {) Q
- //发送数据( {+ M0 U4 r8 {: [2 _
- int a = send(sockClient, message, strlen(message) + 1, 0);/ K+ u) u2 d) K" W; ?
- printf("to server:");, r. M3 m2 c2 c4 _, T
- scanf("%s", message);
' q. a7 h% x6 ]$ `
7 M4 ^+ x! r0 a4 o- }
! G0 h" K( m s- w9 } @# a - //关闭socket2 @ C' m* o% S- M+ y/ c' P
- closesocket(sockClient);
0 w5 m6 T/ C# a; t8 G - WSACleanup();" l' q+ o$ f8 E( g
- }
复制代码 求懂的高手来解答一下。
* p* t8 p1 ], M" W7 j谢谢了!, k& o7 v3 e4 z- n; K4 G4 ~# [
: N/ y I5 d- W. O
; g# N* y2 Q3 T+ v9 b! ?2 d
' L! W$ Y! D& I! L2 l |
|