管理员
论坛积分
分
威望 点
贡献值 个
金币 枚
|
大家好,感谢你的回复。8 p1 v3 H+ U+ s
我用c 写了一个socket通信的小程序,写好了服务端和客户端,可遇到个问题,就是每次启动客户端只能发送第一条消息,之后服务端就不能再收到消息了。8 U, R6 @2 i( I- X, Z- W
Talk is cheak , show me the code.
* F* i( d8 e0 }1 R4 |服务端代码:
; a: Q% i2 S$ r: X. T J+ R- L( d& z- #include<stdlib.h>
6 v' H6 I6 J3 @5 S - #include<WinSock2.h>
) C* t$ m$ E: a% S - #pragma comment(lib,"ws2_32.lib")2 w+ N6 ^* m* o8 z" F0 F f
- int main(){+ I4 F" W& T6 U) i/ Y, C9 J
- WSADATA wsd;
7 m" I) V' F' \2 B& S- U% E - SOCKET sockServer;; D# f0 S' d( [7 D; U
- SOCKADDR_IN serveraddr;// 服务端套接字 该结构中包含了要结合的地址和端口号
+ v5 |/ S; p* k! ~& o$ v - SOCKET sockClient;
6 w* t& N* K' O: F - SOCKADDR_IN clientaddr;/ r0 |7 P2 L; B2 h% G
- WSAStartup(MAKEWORD(2,2),&wsd);//初始化网络接口- h& W6 b2 p: j/ N0 Y8 c% M- l
- sockServer = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);//创建服务器socket
3 T l% V5 g+ U- a" i; A. m - ! c" }8 G! n1 J
- 4 t* Z8 B1 [$ g
- if (sockServer != -1 && sockServer != INVALID_SOCKET){
8 k% I: y3 u( G, J - printf("Socket has been created :%d", sockServer);
; U; @; S8 i2 d5 _2 d: y - }
7 ~/ x7 ~; M Y' \" ` L - else{1 D" G E8 H$ m; e/ K! C4 @* g
- printf("Socket create failed.");
' J. Z& B l- P; Y3 @8 t# N# F - exit(0);
3 v4 U( z% o3 W9 o5 G+ ` - }
$ I/ P' B) f2 P. h1 K - serveraddr.sin_addr.S_un.S_addr = htonl(INADDR_ANY);
/ E/ i, F, a6 i, }; L$ D - serveraddr.sin_family = AF_INET;
* X9 p. Q+ ^) u6 P - serveraddr.sin_port = htons(6000);//绑定端口6000
: \" I5 j6 z- c/ S* _) c - int bindInfo = bind(sockServer, (SOCKADDR*)&serveraddr, sizeof(SOCKADDR));1 j9 @, j/ y- A2 U$ `, F$ @
- char recvBuf[100];
/ K$ W E. m6 ]! f# v/ L1 i7 K u - int len = sizeof(SOCKADDR);
! v) s7 L z: b8 x - listen(sockServer, 5);//5为等待连接数
4 B7 v4 t, N, G3 g3 f' ?4 Y - while (1){
6 ^: N3 w4 K- u& s5 ]- U( H - sockClient = accept(sockServer, (SOCKADDR*)&clientaddr, &len);
/ j# p! i% v: ~& A7 _0 } - //接收客户端数据
6 I/ j* O4 {$ x/ x; e2 Q( b h - recv(sockClient, recvBuf, strlen(recvBuf)+1, MSG_PEEK);
; `& x' ` j/ F* k1 _ - printf("to me:%s\n",recvBuf);
: `+ p& x# Z0 o4 q - memset(recvBuf, 0, 100);
0 G u: y+ w8 C9 y' G z - }; a: { V' R, ?! ~
- closesocket(sockClient);
- ~/ O! f. b9 Q7 c \& ~ - system("pause");/ e9 b: z# q1 g! y {" V
- return 0;. u: S( g) h7 |3 b# C
- }
复制代码 客户端代码:* F+ n* d3 ?! X. }
- #include<winsock2.h>
2 G9 Z' S O# O" \0 x; U - #include<stdio.h>" J7 o' v1 ~7 {
- #pragma comment(lib, "ws2_32.lib")+ ]7 V& I# `- j b5 V
- void main()1 E5 m6 F& S) W0 a8 b6 v0 ?
- {( ~1 H4 q; L& s, e0 L0 ?9 W2 p
- WSADATA wsaData;: Q N3 O# X ]
- SOCKET sockClient;//客户端Socket& K% V, {1 G# V2 G1 G2 ]
- SOCKADDR_IN addrServer;//服务端地址5 v, D& u* n: Y+ N! l" C
- WSAStartup(MAKEWORD(2, 2), &wsaData);8 M' `2 v o- i$ E
- char message[20] = "HelloSocket!";7 y# H9 X) z; |( l
- //定义要连接的服务端地址* i. j* I5 p& T, |8 @1 q4 |2 ]
- addrServer.sin_addr.S_un.S_addr = inet_addr("127.0.0.1");//目标IP(127.0.0.1是回送地址)
* ]. ~6 f8 n4 M0 D. N - addrServer.sin_family = AF_INET;
+ K6 r% g0 J% I7 j* F - addrServer.sin_port = htons(6000);//连接端口60006 s9 P2 }0 g. f7 `0 m& r
- //新建客户端socket
7 Z; W0 ]$ {. _6 z - sockClient = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);6 {6 O* Y, P9 a: q
- int s = 0;5 n- Z# t2 D3 ?# e& \" y L6 D5 n) D
- s = connect(sockClient, (SOCKADDR*)&addrServer, sizeof(SOCKADDR));4 [. q) I+ h8 L+ K* @( ?( J
{$ A7 i7 S7 ?. d. h+ j3 S- while (1){' ]3 J# b" N+ \
- //发送数据" z) V h8 `2 m" E
- int a = send(sockClient, message, strlen(message) + 1, 0);! [, B8 v7 J0 `9 L% J K, n
- printf("to server:");
: ~2 c) @( i( w4 { - scanf("%s", message);
1 W0 B/ F% J' w4 v - : j: q+ v% e+ n6 `( z+ m$ M; O% B/ P
- }0 q3 R5 a$ {1 u3 f/ r, W9 X
- //关闭socket0 R9 C# x& e4 T7 J3 U
- closesocket(sockClient);5 M" k' _ P6 W8 U* A7 d: I
- WSACleanup();* D" m( s% a& H) X# a: o3 q6 {
- }
复制代码 求懂的高手来解答一下。" t0 @) Y* E4 @' w
谢谢了!
" \8 ]/ M1 L5 W* ?, E5 g4 t, E4 Z9 C D
6 b9 y+ F Y4 R& x3 L- r/ f4 ?& H" w. t; A6 j4 }
|
|