管理员
论坛积分
分
威望 点
贡献值 个
金币 枚
|
大家好,感谢你的回复。3 b8 s& W u# _# b
我用c 写了一个socket通信的小程序,写好了服务端和客户端,可遇到个问题,就是每次启动客户端只能发送第一条消息,之后服务端就不能再收到消息了。6 Z1 Z/ \# X% ]0 c0 t+ P' D
Talk is cheak , show me the code.) |+ u O; C p9 a% U' r% q: k: z7 R& J' Q
服务端代码:
9 K7 ]" A/ _( e7 s9 u. E8 \- #include<stdlib.h>' _; u: w5 A1 Y7 l
- #include<WinSock2.h>
4 @/ r4 A) d, d5 y, X1 ]; ~ - #pragma comment(lib,"ws2_32.lib")0 a) n4 V/ K: J8 ?) t$ B) q
- int main(){
?: Q" y5 G) b - WSADATA wsd;' l& Y# @( F4 b$ k7 C+ j2 l1 E
- SOCKET sockServer;8 S7 x* a& k; _% W( B. l
- SOCKADDR_IN serveraddr;// 服务端套接字 该结构中包含了要结合的地址和端口号! l. u# t) |! t7 T+ }0 @
- SOCKET sockClient;& x" X. ~. g0 ?# O: C& V
- SOCKADDR_IN clientaddr;
% x3 o3 t$ M" X - WSAStartup(MAKEWORD(2,2),&wsd);//初始化网络接口8 d! l7 Q' }) b0 d
- sockServer = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);//创建服务器socket9 ^, H. } E4 R5 x6 x; @
- 2 w! c- J s/ X5 {3 ~- y( ]
# b1 y/ R( l2 c- if (sockServer != -1 && sockServer != INVALID_SOCKET){" G6 h% d$ U' m1 q0 e
- printf("Socket has been created :%d", sockServer);: q: O# G; d% Y0 i1 T+ L4 x1 L
- }
: L! M' X' m R8 f) X2 ^ - else{. K) y7 q R o/ X( v
- printf("Socket create failed.");* K+ o" j1 E4 R: x: r- d1 Y1 U
- exit(0); p9 j1 B3 \. p. E; C8 p; W; y
- }
6 k7 {% P! ]7 v! D+ {* X: r" T, j - serveraddr.sin_addr.S_un.S_addr = htonl(INADDR_ANY);
- R# I/ b+ ~/ q" k- Z. Y - serveraddr.sin_family = AF_INET;3 o4 H4 S4 X$ H
- serveraddr.sin_port = htons(6000);//绑定端口6000* k$ u* ~. X. n0 I3 H7 @9 [. G
- int bindInfo = bind(sockServer, (SOCKADDR*)&serveraddr, sizeof(SOCKADDR));& q* u" ?; ]! t% q
- char recvBuf[100];
8 _$ O* i: @( N, i7 {( L% C - int len = sizeof(SOCKADDR);
8 J# B5 k% @- `% _( S& W( j# g - listen(sockServer, 5);//5为等待连接数9 a# k6 d3 l5 ?$ m+ W7 C
- while (1){$ f7 J) Z! F+ e7 C# D$ D
- sockClient = accept(sockServer, (SOCKADDR*)&clientaddr, &len);! o! @0 K7 j. l
- //接收客户端数据/ a6 S1 Y9 F6 @5 I! M
- recv(sockClient, recvBuf, strlen(recvBuf)+1, MSG_PEEK);2 e/ m8 O k3 b3 o1 ^
- printf("to me:%s\n",recvBuf);& y {8 w* ` n; v8 t/ P0 w1 f
- memset(recvBuf, 0, 100);- d% Z3 n# s+ v7 I( O
- }% h1 ^' {& C2 u
- closesocket(sockClient);
( S% n j1 G1 ? - system("pause");
Y) R3 p, |( F7 {9 U) }4 H - return 0;
) b& u+ q( H m- ~7 W& F" N1 j - }
复制代码 客户端代码:
+ o+ h6 R6 Z' { p7 ]; h1 Y2 z$ K- #include<winsock2.h>3 e$ f+ w; s- b
- #include<stdio.h>
5 X9 f2 b8 _" p! I - #pragma comment(lib, "ws2_32.lib")/ n% ?) \& Y2 k
- void main()
5 t. T' `) M8 A+ o( x& g! W: f& } - {
* d; f# }3 f6 Y- { - WSADATA wsaData;" ?( \8 s3 V `7 w0 ]" {0 N
- SOCKET sockClient;//客户端Socket" r- E/ l \3 B' d% }2 @0 \- i3 O5 x' v
- SOCKADDR_IN addrServer;//服务端地址
; X- f& k) F3 @7 \ - WSAStartup(MAKEWORD(2, 2), &wsaData);
8 p, h3 C- H3 @' p# A0 A6 u" t - char message[20] = "HelloSocket!";5 ?. F7 L* e0 c
- //定义要连接的服务端地址% y' a2 L0 o: B, {: m
- addrServer.sin_addr.S_un.S_addr = inet_addr("127.0.0.1");//目标IP(127.0.0.1是回送地址)' j4 c' f! C: o( O
- addrServer.sin_family = AF_INET;1 u& `: x6 t4 ]) |
- addrServer.sin_port = htons(6000);//连接端口60003 N7 x, b0 p1 a) K+ Y' X& f
- //新建客户端socket
, Q6 i* e1 _1 o6 {# [( D - sockClient = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
. R2 W: X9 I! V7 L, b, h. X0 v - int s = 0;0 N# N) y/ D& p
- s = connect(sockClient, (SOCKADDR*)&addrServer, sizeof(SOCKADDR));
, l0 I1 }; m- c - # A' l+ x) h1 F0 H/ W+ L9 S9 D
- while (1){
) a* c! g* g6 p8 Y' U - //发送数据
, }$ r2 e9 Y' \" B; s - int a = send(sockClient, message, strlen(message) + 1, 0);
: x( J q4 Q, ]2 W9 T6 l: T - printf("to server:");) s; V. R: a( Q0 w
- scanf("%s", message);% x* @9 H/ b: q% g
- 4 V H3 I5 T0 H' X7 L
- }
# d" H8 C1 ^6 A8 B- s: e - //关闭socket
9 Z- f8 C& ` F# f7 S4 b - closesocket(sockClient);8 V# _& A& Y& B5 c( E: {. \
- WSACleanup();
! H; Y @/ Y) ^ k9 _ - }
复制代码 求懂的高手来解答一下。
6 J5 I: C1 w) K谢谢了!& G7 g5 Q( ]( T7 M( `' H& j
3 m8 L% {1 n3 x3 p* ]+ c
9 L) s" r4 K+ d | ^6 f7 w
# _9 P# |; c% t5 M" ~7 ` |
|