管理员
论坛积分
分
威望 点
贡献值 个
金币 枚
|
大家好,感谢你的回复。" p7 Z2 U* ^) Y) a0 }0 [
我用c 写了一个socket通信的小程序,写好了服务端和客户端,可遇到个问题,就是每次启动客户端只能发送第一条消息,之后服务端就不能再收到消息了。' F# N1 K# ?7 y1 y
Talk is cheak , show me the code.
; Z T k' o$ V# N# T1 h- G服务端代码:
( u; w/ q8 v; F) M+ H3 m- #include<stdlib.h>1 ~% z0 j: @# b' z+ K: q2 G
- #include<WinSock2.h>" X. K9 [; W2 q7 P5 y
- #pragma comment(lib,"ws2_32.lib")( _9 J9 s1 B( h$ C
- int main(){5 ]6 z- n4 |, t) b
- WSADATA wsd;* |0 g/ Z3 ^6 B
- SOCKET sockServer;" p7 x3 V- V, f- i' M3 W
- SOCKADDR_IN serveraddr;// 服务端套接字 该结构中包含了要结合的地址和端口号
# t8 i( g* E# W) {, | - SOCKET sockClient;! d, n* p P- U9 }$ u4 [+ k% w/ ~
- SOCKADDR_IN clientaddr;( d" A5 ^' @) G8 Q
- WSAStartup(MAKEWORD(2,2),&wsd);//初始化网络接口
9 _, L0 n1 S5 C% E( o3 L - sockServer = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);//创建服务器socket
2 y/ i# L$ Q$ a1 i0 o
4 l' E# @, f _- + R$ E% a& z$ Y0 d" [
- if (sockServer != -1 && sockServer != INVALID_SOCKET){
! ^) U( J3 }+ }* ~4 G* F - printf("Socket has been created :%d", sockServer);! w$ D$ ]. r9 q) L* ]% p
- }$ }; o) B1 Y" [6 {6 C9 s9 }
- else{
7 b3 @( P3 t7 R$ T e. G - printf("Socket create failed.");6 y+ x9 ]8 [& h. ]+ {6 H8 f |2 v& A
- exit(0);9 |6 u" |+ d) }' [- S& E7 X
- }
: L, w7 c& w+ s8 | - serveraddr.sin_addr.S_un.S_addr = htonl(INADDR_ANY);! z* P+ [+ z! D- p5 W8 c
- serveraddr.sin_family = AF_INET;
; ^8 n9 E2 J3 F' ~& p - serveraddr.sin_port = htons(6000);//绑定端口6000( |4 j0 m& C1 {. F' [! ]9 |
- int bindInfo = bind(sockServer, (SOCKADDR*)&serveraddr, sizeof(SOCKADDR));
0 c5 F' e# u# o+ ?: G1 i; g - char recvBuf[100];
- {- r" _3 }5 S) m+ g! r - int len = sizeof(SOCKADDR); t9 Z7 U; R! s {
- listen(sockServer, 5);//5为等待连接数
* k3 U' H4 g1 J - while (1){
1 H6 O4 H. q; _9 q: z: n - sockClient = accept(sockServer, (SOCKADDR*)&clientaddr, &len);" {# R7 |6 S) u
- //接收客户端数据
' c' H3 p7 j5 t - recv(sockClient, recvBuf, strlen(recvBuf)+1, MSG_PEEK);
9 m# n% a1 D! v; O/ n/ A - printf("to me:%s\n",recvBuf);
" R+ ?3 F' a, O$ Y8 n. f$ B8 N7 A - memset(recvBuf, 0, 100);
* s3 d" v' u7 N4 T3 d - }
" r+ @: t! ^9 G7 ?$ G5 H2 q; ^4 h - closesocket(sockClient);
" w5 n( S. |+ a$ W2 ^ I - system("pause");2 s% P3 @5 M$ P2 T
- return 0;- V3 W+ H0 L' j* H1 w
- }
复制代码 客户端代码:
2 J* [. T5 F. ~$ j' z- #include<winsock2.h>
/ i4 M7 M: |+ l& V8 W0 [; G - #include<stdio.h>
! a0 [/ g( U$ J+ ]1 E7 J) ^ - #pragma comment(lib, "ws2_32.lib")& c) K* o6 {9 u' m+ U
- void main()8 A3 F& |: z3 `+ W. z
- {
/ k/ f4 ]$ d# @$ j - WSADATA wsaData;
0 G4 [5 Y1 m. |. r' O - SOCKET sockClient;//客户端Socket
1 q9 L7 w5 P% q) _ - SOCKADDR_IN addrServer;//服务端地址 E/ X8 h9 f" p4 f' e6 r
- WSAStartup(MAKEWORD(2, 2), &wsaData);
/ {: w4 X% z* v$ Q( u - char message[20] = "HelloSocket!";
& X* U# u6 K# O8 u2 W7 e! B - //定义要连接的服务端地址
1 N$ C1 K2 v4 n& D; k) K$ K2 p# o - addrServer.sin_addr.S_un.S_addr = inet_addr("127.0.0.1");//目标IP(127.0.0.1是回送地址)
9 N# u( }" F1 } - addrServer.sin_family = AF_INET;* c" y5 f. J. O' H n
- addrServer.sin_port = htons(6000);//连接端口6000
. d% O* y$ s" `) I - //新建客户端socket1 { S2 `& G* l3 u/ d$ ?. b% K
- sockClient = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);; c( }( d' }; ?! J8 x; E n& \
- int s = 0;& Z3 Z' u" }3 c* ^2 {
- s = connect(sockClient, (SOCKADDR*)&addrServer, sizeof(SOCKADDR));# M3 }- G$ O: s7 M8 p
- 3 z, H" q; S( M& o8 e& [
- while (1){/ r$ ~% ^* j1 F5 L2 v
- //发送数据- c$ p/ F. Y2 Q2 A
- int a = send(sockClient, message, strlen(message) + 1, 0);. G1 e3 o: o3 z: G& L
- printf("to server:");
6 ]3 J0 U) c9 C n! O& D - scanf("%s", message);
' p/ \, X3 Z8 X9 ^+ {7 I - ) W/ L( a3 r& f! w8 n/ i
- }' ~9 Z6 r' q! t8 q& e7 ]
- //关闭socket
. G1 I4 T5 w3 D6 r2 _1 J - closesocket(sockClient);
6 {1 Y/ {2 i0 T6 }7 F - WSACleanup();
8 w% _0 b( p4 u1 `4 K - }
复制代码 求懂的高手来解答一下。
" F8 ^/ G% m9 R" p* R9 |6 U谢谢了!( z7 o) a0 h. F R) f3 C
5 e u6 K, {* v3 G: B/ N0 @8 W3 C# z/ M) Z$ m$ O
1 K4 o7 j9 O4 Z0 D g: w
|
|