管理员
论坛积分
分
威望 点
贡献值 个
金币 枚
|
大家好,感谢你的回复。4 @6 ^- ?2 i3 q. g2 b
我用c 写了一个socket通信的小程序,写好了服务端和客户端,可遇到个问题,就是每次启动客户端只能发送第一条消息,之后服务端就不能再收到消息了。
% z" |+ Z) N9 B: |( zTalk is cheak , show me the code.
5 w3 p0 R- r- I+ j) i服务端代码:4 V9 ^" ?1 h1 Q9 R( L5 |
- #include<stdlib.h>
! o |+ N* b3 m2 F' [6 @3 X, V: ] - #include<WinSock2.h>/ U) o0 x1 e3 D" }0 Y6 Y
- #pragma comment(lib,"ws2_32.lib")$ M: N! i( A9 G( Z5 l" K
- int main(){/ V/ K H' V* s: h& H. v' m
- WSADATA wsd;
5 b: R- ]' p, W1 U) G0 l - SOCKET sockServer;0 B4 z% o7 E$ s' X
- SOCKADDR_IN serveraddr;// 服务端套接字 该结构中包含了要结合的地址和端口号% o; _( B% ~+ a
- SOCKET sockClient;
3 r2 M. Z/ I: M0 T+ ? - SOCKADDR_IN clientaddr;
- G5 n; G, T7 | - WSAStartup(MAKEWORD(2,2),&wsd);//初始化网络接口. g) B) L3 ^& f# O. U8 D! i
- sockServer = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);//创建服务器socket7 d' _' l) n+ v' H; R
' X" l6 r$ ^, G
" }" w( y) J0 D8 J' E- if (sockServer != -1 && sockServer != INVALID_SOCKET){0 c/ e7 v5 U" D* ]
- printf("Socket has been created :%d", sockServer);
4 B1 f: ^6 o" h9 o2 W - }
' U% L$ p% l7 F* a+ t' _7 T - else{
* C+ W: y" y% n) C% h3 M - printf("Socket create failed.");
' |3 F2 E* t% k3 ?* @ - exit(0);( A! u; M2 r- [- x3 n/ }
- }
) E* E. X4 E1 a! ?" I" a) K - serveraddr.sin_addr.S_un.S_addr = htonl(INADDR_ANY);
$ d8 P: t4 l1 f5 G- s" k - serveraddr.sin_family = AF_INET;
% y# K! Y# b' q. ^5 y - serveraddr.sin_port = htons(6000);//绑定端口6000
* v9 B& _/ [1 v \7 x6 L i - int bindInfo = bind(sockServer, (SOCKADDR*)&serveraddr, sizeof(SOCKADDR));$ t0 u }- a! S7 O
- char recvBuf[100];
5 U( [0 I5 o" R5 n; S, Q - int len = sizeof(SOCKADDR);; x' ?* P2 ], k1 M& ?
- listen(sockServer, 5);//5为等待连接数
; {+ c9 X/ j! _) g. M) t1 u0 P - while (1){1 E4 L( n' I0 Q' G- b1 z2 `
- sockClient = accept(sockServer, (SOCKADDR*)&clientaddr, &len);
4 r( I& |# m: C- E' s; L - //接收客户端数据
% y. x2 a5 j1 a0 a3 ]: { - recv(sockClient, recvBuf, strlen(recvBuf)+1, MSG_PEEK);. T' y+ N) k0 M% r
- printf("to me:%s\n",recvBuf);
. x7 P/ P' z, E! b4 d* g* ?9 E1 V# I - memset(recvBuf, 0, 100);8 q( U) U2 k5 `% \. e
- } k5 H/ I2 S. X( T5 z
- closesocket(sockClient);
( t' P* X# @' l6 k - system("pause");
+ I- C- D5 U7 M; q* ]( D9 n) ^6 [ - return 0;
( q3 V: y4 \; L! l - }
复制代码 客户端代码:, ?, S' _0 X: k
- #include<winsock2.h>
9 B3 R: N0 \/ q, g' m2 V - #include<stdio.h>
$ f1 g8 X3 Z ?0 f* J8 D - #pragma comment(lib, "ws2_32.lib"): w( b) _2 ?) o; Q
- void main()
9 I" G$ r& D+ ?$ m, `5 t - {2 |( h. I) r7 J$ G+ B
- WSADATA wsaData;
+ V, y0 L1 _) J0 `9 I# \* a3 h - SOCKET sockClient;//客户端Socket. b) e) z W+ a( m
- SOCKADDR_IN addrServer;//服务端地址# G5 D# O" x( s# ^$ E: w/ ], J3 x
- WSAStartup(MAKEWORD(2, 2), &wsaData);$ S. m4 k- K$ O5 N5 Q8 g7 e+ b
- char message[20] = "HelloSocket!";( J* T8 m4 \; o+ l
- //定义要连接的服务端地址6 u5 K4 S8 D9 e3 P
- addrServer.sin_addr.S_un.S_addr = inet_addr("127.0.0.1");//目标IP(127.0.0.1是回送地址)
0 z1 Q3 p) M$ ~% e; B1 [7 y# k - addrServer.sin_family = AF_INET;
1 o. M8 T* ^7 t7 F- l( x5 B5 q - addrServer.sin_port = htons(6000);//连接端口6000
( {# D0 M& `3 ?/ e) V. o - //新建客户端socket
- q. D7 c# b! `. \ a3 ^ - sockClient = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
' M# q- L5 M3 c0 \# ` - int s = 0;
, I$ [: E! Z4 k# V$ l. N - s = connect(sockClient, (SOCKADDR*)&addrServer, sizeof(SOCKADDR));$ w% K, Z# X1 Z: V: {7 ~
3 G" v3 s. G5 ~( D3 K- A+ B/ c- while (1){
# M0 @. `* Q T2 a/ M - //发送数据
8 i8 F2 v! H* {( V) N7 l" p1 N - int a = send(sockClient, message, strlen(message) + 1, 0);
! u1 F! T! v8 O' Y v- P - printf("to server:");
: e0 x) F6 `( t - scanf("%s", message);1 p3 Z& | \! O- f' `% N
- 7 x1 G; S% }1 F+ C
- }! o; r* v5 R2 t8 A4 `
- //关闭socket
; Q( D7 G5 ^' X& z% m5 s3 X - closesocket(sockClient);
. L+ A* x* K' q4 [4 M; ~ - WSACleanup();
- Y% v* t& b& G! i: s - }
复制代码 求懂的高手来解答一下。
0 M6 B; p' J! O' O( q; e" l谢谢了!" U. c' h! G9 h) `) k" {
1 R- C3 k, c7 l% T, k7 Y$ Z
{& W/ T$ g8 b( k! D" c7 V2 \% y8 h8 n; F$ j7 b& G# x
|
|