管理员
论坛积分
分
威望 点
贡献值 个
金币 枚
|
大家好,感谢你的回复。7 ^0 j3 L9 E/ T) P0 W" P
我用c 写了一个socket通信的小程序,写好了服务端和客户端,可遇到个问题,就是每次启动客户端只能发送第一条消息,之后服务端就不能再收到消息了。$ {4 v. \# i% `5 H6 J$ W
Talk is cheak , show me the code.
6 q- V1 ]1 K- `" P9 W( o/ [服务端代码:# G4 Y5 c. @3 z. k$ k! h
- #include<stdlib.h>
, u- o" q5 q7 O7 M - #include<WinSock2.h>
0 l, ]8 `* y! L5 G3 L - #pragma comment(lib,"ws2_32.lib")+ T X+ s4 q( s1 B/ g
- int main(){
8 h* W$ ?- b. H( f- [& ~$ v - WSADATA wsd;+ I( g5 Z. {: k' y
- SOCKET sockServer;
' o# ]1 m w7 _) f: c - SOCKADDR_IN serveraddr;// 服务端套接字 该结构中包含了要结合的地址和端口号+ {. V7 N+ Y4 L4 ~' E4 l: R1 P
- SOCKET sockClient;/ X0 t% \2 B n7 K8 I
- SOCKADDR_IN clientaddr;
8 F! u- G+ d8 b& P. `1 z! y - WSAStartup(MAKEWORD(2,2),&wsd);//初始化网络接口! n- F; j+ I; t" _4 Z" N+ F# E
- sockServer = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);//创建服务器socket* O1 X8 a* X1 R w; ^
- , B. f7 v6 ^4 h! Z
# f' w+ @- j2 k2 }3 z- if (sockServer != -1 && sockServer != INVALID_SOCKET){
% @9 i, |+ t7 Z9 ^' q( f9 K - printf("Socket has been created :%d", sockServer);
d2 L' q! Y% H( R1 U1 X - }" w0 N! k$ V+ D! g
- else{
# t" N+ k# {- N0 ^8 | - printf("Socket create failed.");
; A/ A7 P6 j) J8 d8 q3 [$ ? - exit(0);+ l% S' v8 l4 @5 ^/ J; w
- }! n) o5 m; j. D. Z; C' X6 r- C
- serveraddr.sin_addr.S_un.S_addr = htonl(INADDR_ANY);0 \# m! G0 K" C3 d0 K' i
- serveraddr.sin_family = AF_INET;& J& A$ g7 q, U. r; O
- serveraddr.sin_port = htons(6000);//绑定端口6000
1 P: }0 t6 a' N, W( W7 }8 X - int bindInfo = bind(sockServer, (SOCKADDR*)&serveraddr, sizeof(SOCKADDR));
) p! _ r9 M4 s' X* R - char recvBuf[100];
/ f2 k) {$ [- `5 Z: q, p1 u& h - int len = sizeof(SOCKADDR);
; \: m [! A0 G2 L2 o, ? ?$ { - listen(sockServer, 5);//5为等待连接数
6 S/ P* ?) a1 [+ D - while (1){, o" `6 m5 h; w2 s9 r4 L; R: G1 x
- sockClient = accept(sockServer, (SOCKADDR*)&clientaddr, &len);
; A( Z9 H: b' B3 i) P - //接收客户端数据: S1 L2 O$ A3 x8 m3 n5 t5 \. n$ E
- recv(sockClient, recvBuf, strlen(recvBuf)+1, MSG_PEEK);" ^9 _5 A3 c: ]7 d$ R% O
- printf("to me:%s\n",recvBuf);' v/ d( l# ]8 P7 p" H3 t+ X8 B
- memset(recvBuf, 0, 100);$ d0 o9 n1 \' G4 y( C6 {2 b' e4 y
- }. [9 p$ p2 H& l, x0 A
- closesocket(sockClient); ^7 U5 Q( v! n! T, V; c: X1 @
- system("pause");4 j( ^8 y1 S& t3 Q- @ L
- return 0;
! f& }+ T/ }0 J, } - }
复制代码 客户端代码:
# Z5 K/ }) V3 d3 h& S$ ~/ s% A2 e- #include<winsock2.h>% O& X; s; w: d
- #include<stdio.h>
" w- J; E+ \ _. u4 j( q0 z - #pragma comment(lib, "ws2_32.lib")+ c% f) m6 P! R. t
- void main()$ f+ n$ @: B9 p$ y& g7 `/ [3 y. u
- {: E6 q9 b8 Z8 H7 W9 j, N
- WSADATA wsaData;6 `9 @/ z% S8 c4 b9 h5 g! n
- SOCKET sockClient;//客户端Socket( `; v, f. n5 q+ }7 o5 ~* x- D. r
- SOCKADDR_IN addrServer;//服务端地址
5 c# D$ Y0 z7 Z; y9 I# x! Z - WSAStartup(MAKEWORD(2, 2), &wsaData);+ K) t6 D: V: x9 g
- char message[20] = "HelloSocket!";$ U+ E$ r& p+ `* I
- //定义要连接的服务端地址
) t5 |! m, M/ ^6 l2 F - addrServer.sin_addr.S_un.S_addr = inet_addr("127.0.0.1");//目标IP(127.0.0.1是回送地址)- K5 I% s3 o" j: f w5 d
- addrServer.sin_family = AF_INET;# {$ N9 ^0 K2 a7 B/ @5 N
- addrServer.sin_port = htons(6000);//连接端口6000
" Y, w+ U! f$ n0 w - //新建客户端socket% s9 ]; e4 v: B* [* ~
- sockClient = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
# `% s0 d/ {: m% S) m - int s = 0;; B, \' |4 r+ O5 O) c: \
- s = connect(sockClient, (SOCKADDR*)&addrServer, sizeof(SOCKADDR));
$ L! Q. n1 a% d2 V& m2 C+ y
0 H/ J2 U5 b) I! G; A: K+ C7 J- while (1){
2 q5 O$ d2 t3 | - //发送数据& R e7 y( \" X- L# w% p
- int a = send(sockClient, message, strlen(message) + 1, 0);
5 X0 e+ v% q$ Q& p! e - printf("to server:");
0 p8 `! I& A3 q7 Y% S* E4 d; d, N - scanf("%s", message);
* E. J. B; i9 v9 l7 S" p2 ]. T - # Z/ ?* N! ?# }" e
- }. `3 |" u% Y# q0 i
- //关闭socket
( t, e# j" k, _: {: }- ?& y - closesocket(sockClient);% X$ m. n9 C2 e8 U, \! O( W7 R
- WSACleanup();7 h6 {; L; l/ n( `) y4 { h2 E$ a
- }
复制代码 求懂的高手来解答一下。; E( S. a# {" c* c* d8 s$ ^4 _
谢谢了!9 [! Q% H0 ]" w" K
" Z: T$ ?# F( A! s1 u: \/ I* N% s; B( K
4 ~4 U+ [# o1 E: L3 |
! W3 Q4 R* M4 M9 ?/ z- u; L5 z |
|