大家好,感谢你的回复。! f5 e( V) E1 y& J5 v2 g
我用c 写了一个socket通信的小程序,写好了服务端和客户端,可遇到个问题,就是每次启动客户端只能发送第一条消息,之后服务端就不能再收到消息了。1 b M! s1 W' ]( K Y* f- ^$ B
Talk is cheak , show me the code.
$ v7 d0 M# |: |: G' p服务端代码:
( t& e. M2 h6 |9 ]& l1 t. v- #include<stdlib.h>
' k8 I6 x# ` o8 c8 T& Q1 a+ d - #include<WinSock2.h>
8 b5 S) u3 w, L W* N- k \: O& q - #pragma comment(lib,"ws2_32.lib")% H6 E- ^) p$ n$ X
- int main(){
( p' A x8 z" g2 z( \ - WSADATA wsd;
! n5 I9 a3 o. F- j% V( G - SOCKET sockServer;
4 P4 [# v1 b' L' ^ - SOCKADDR_IN serveraddr;// 服务端套接字 该结构中包含了要结合的地址和端口号8 o. i. F# k2 t# p
- SOCKET sockClient;
9 i! t1 p" B! e" W& f5 m" { - SOCKADDR_IN clientaddr;
0 O, o$ |9 J1 O8 s) q: E) n, ^ - WSAStartup(MAKEWORD(2,2),&wsd);//初始化网络接口
. x6 D$ C- d* k# [1 i0 ?! | - sockServer = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);//创建服务器socket# R9 O4 t( _3 a2 B g* g
- 1 C& @/ c# }' h" x) }% x/ x
- # g1 h7 V* Z7 g3 n( H7 _
- if (sockServer != -1 && sockServer != INVALID_SOCKET){
! O: P' E3 w( z' }3 k3 ? k - printf("Socket has been created :%d", sockServer);9 t M& ]% ^- o2 M6 x! |0 I
- }
) i9 [0 U. ]- @ - else{8 A% t$ v: X& m. r* l
- printf("Socket create failed.");
1 W5 T- p' }* i: } - exit(0);
/ S& S5 Q. F8 c3 @2 v - }
0 [2 Y6 [ q, N( N; m# ~% Z - serveraddr.sin_addr.S_un.S_addr = htonl(INADDR_ANY);. ?; u! u+ J/ Y4 y2 l3 k! a/ o
- serveraddr.sin_family = AF_INET;& Y& D' a, Y9 s1 V: \/ g
- serveraddr.sin_port = htons(6000);//绑定端口60003 a7 Z X3 G# J7 C D
- int bindInfo = bind(sockServer, (SOCKADDR*)&serveraddr, sizeof(SOCKADDR));! s8 X: j9 ~+ y/ F
- char recvBuf[100];
! Q# m R! k" C. C! c - int len = sizeof(SOCKADDR);& D o. ^2 ^5 ?1 M2 [/ l P# W
- listen(sockServer, 5);//5为等待连接数
/ J' T- O5 B) `2 F/ j, l - while (1){
& @# x* h# F8 y% a - sockClient = accept(sockServer, (SOCKADDR*)&clientaddr, &len);
& ~1 m: [4 c% ~9 I" a6 m - //接收客户端数据
% v. R) X0 \6 t. j" Y+ {& e& K6 ]' k - recv(sockClient, recvBuf, strlen(recvBuf)+1, MSG_PEEK);- u; V8 K) e9 O4 b* v7 Y1 |
- printf("to me:%s\n",recvBuf);
% N0 `+ i/ Y8 }! m7 v - memset(recvBuf, 0, 100);" }: }; \; I0 I- y" {
- }
8 N! k6 p+ s& p2 ~2 ^ - closesocket(sockClient);1 B/ M! c3 O1 L, J
- system("pause");) k! \( P3 ?# Z" W3 [+ D
- return 0;3 W5 @$ |$ o, r; O1 q
- }
复制代码 客户端代码:& ^/ _8 J( F7 G. f' `2 g1 d( Y' n
- #include<winsock2.h>
# M; u' B) l) w" O- \ - #include<stdio.h>; s$ i) t; f; G& c( m3 A5 J
- #pragma comment(lib, "ws2_32.lib")2 j( j( U* m& S ]+ z4 X9 {
- void main()
3 e7 S4 q& B" k; M0 C - {
" b6 E, C' v) A6 W$ J - WSADATA wsaData;
' H1 l& R! j- F F/ ^8 S - SOCKET sockClient;//客户端Socket7 _2 |1 c3 I6 o) h! [9 t* r
- SOCKADDR_IN addrServer;//服务端地址
' y7 X' r5 D$ R* R* V2 o3 i - WSAStartup(MAKEWORD(2, 2), &wsaData);3 _; `4 x C M3 f. ?
- char message[20] = "HelloSocket!";
. D9 \6 [. Y$ s. v/ ^ - //定义要连接的服务端地址
7 h& H& R+ ?. B- V, C- m - addrServer.sin_addr.S_un.S_addr = inet_addr("127.0.0.1");//目标IP(127.0.0.1是回送地址)
. |( R b. G& @. G |: I4 @ - addrServer.sin_family = AF_INET;% O- }6 j, e, w1 r- ?( p- b7 c
- addrServer.sin_port = htons(6000);//连接端口6000
: h2 `. c# k9 C4 F - //新建客户端socket- g# D0 W4 e0 |2 p7 e+ b9 ]4 h# I' y1 X
- sockClient = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
- U: \" i; O- R8 d9 {" v8 D - int s = 0;
i9 {" x8 f: R - s = connect(sockClient, (SOCKADDR*)&addrServer, sizeof(SOCKADDR));
: d0 Z8 w. n* t' l L1 h
% d7 m% @1 F2 E4 c2 D- while (1){3 J: J: t/ |8 @) M+ a/ `+ _' l* ?
- //发送数据6 m, E: `, q& u, t1 z
- int a = send(sockClient, message, strlen(message) + 1, 0);- e A, |: t, i* I3 d9 @! Q
- printf("to server:");" L$ ?' O3 O: o/ V
- scanf("%s", message);
U% K) z: i8 l( W - 8 f# R2 y+ A0 L' ]$ v
- }: m5 h& m- e/ v5 p# v% q* }
- //关闭socket; k8 Q; f% O( u `/ t: c/ _ l4 F
- closesocket(sockClient);
; X, y+ d- L* X% s- X% W - WSACleanup();
# O! G" y3 q1 w" z; L7 @ - }
复制代码 求懂的高手来解答一下。$ h, \! h% x) g
谢谢了!8 s! N3 ]5 o) {: K
0 Z: _. T" _4 X9 ~6 t
$ e- E k @: l/ P2 ^0 _' Z4 h1 o- i" @) L7 W
|