管理员
论坛积分
分
威望 点
贡献值 个
金币 枚
|
大家好,感谢你的回复。6 m5 o0 y5 ], ^8 m
我用c 写了一个socket通信的小程序,写好了服务端和客户端,可遇到个问题,就是每次启动客户端只能发送第一条消息,之后服务端就不能再收到消息了。
1 S, _$ ?; \7 g1 iTalk is cheak , show me the code.0 ?& T. S- X3 {* m' k
服务端代码:8 J! m" `; y5 n) K# N9 I( b
- #include<stdlib.h>, B% s) j1 |9 G# o$ K" @- k; x
- #include<WinSock2.h>
8 u' f! N) Q! w* M) t( i - #pragma comment(lib,"ws2_32.lib")8 Y+ i7 v, _: R' D) u4 a' K
- int main(){
1 T- \6 i6 F1 J3 K) I* U - WSADATA wsd;4 v! R, t$ a7 M1 x1 H! @
- SOCKET sockServer;0 f1 w9 a& K7 A. e' |5 u- o* O5 J
- SOCKADDR_IN serveraddr;// 服务端套接字 该结构中包含了要结合的地址和端口号
* ]2 [$ m( d6 j/ C - SOCKET sockClient;
/ [) Y+ A* q0 B5 @8 V - SOCKADDR_IN clientaddr;1 t7 [# \" |4 G( T) t+ b1 s
- WSAStartup(MAKEWORD(2,2),&wsd);//初始化网络接口
* \! J2 J& \, S" N! |% J7 O) y4 ^# [ - sockServer = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);//创建服务器socket: M9 d* w1 k' l6 B% V
- " L# U) m( @- w/ |$ \" B/ H' d; G
9 h, ?$ Y6 A* u+ c- if (sockServer != -1 && sockServer != INVALID_SOCKET){
& S1 Q+ o& q; q! c - printf("Socket has been created :%d", sockServer);
( W2 @, n2 ]+ D5 ~ - }( P z7 }- h& {5 m
- else{! e- A+ u2 ?9 R$ e5 L; q
- printf("Socket create failed.");
9 ~1 I# F7 P$ K2 ? - exit(0);+ e. g7 s7 A8 I: C# C r6 D" s
- }2 [3 m4 y2 i( l# n" m
- serveraddr.sin_addr.S_un.S_addr = htonl(INADDR_ANY);2 U' r: T& b; M( {
- serveraddr.sin_family = AF_INET;/ N3 {- l+ L) a: I8 B
- serveraddr.sin_port = htons(6000);//绑定端口6000; B5 t" f/ @' J7 u% p
- int bindInfo = bind(sockServer, (SOCKADDR*)&serveraddr, sizeof(SOCKADDR));; W0 ^" L! K. P7 o m
- char recvBuf[100];
+ R# H9 V8 K( { ? - int len = sizeof(SOCKADDR);( e& e( |- a& b& `3 |) S8 S
- listen(sockServer, 5);//5为等待连接数
4 f1 `: a/ [' c5 q( q - while (1){
- u! \% G- X% m" L+ N - sockClient = accept(sockServer, (SOCKADDR*)&clientaddr, &len);3 t+ s: q9 R/ H9 b- s
- //接收客户端数据/ y3 F7 a* P* U' s$ e! G. ~
- recv(sockClient, recvBuf, strlen(recvBuf)+1, MSG_PEEK);- b O1 f+ v6 e7 c+ f
- printf("to me:%s\n",recvBuf);- U" b o2 D1 ? R; J
- memset(recvBuf, 0, 100);1 W% S; J. L, B
- }/ j' [3 _! t$ P5 ?/ x' d' ^
- closesocket(sockClient);
% k, o8 s: V/ t9 a* ?; u; C) _ - system("pause");
6 U" M, G6 M3 C0 Q+ ^6 p# `' ` - return 0;
' R" o T% L! i - }
复制代码 客户端代码:$ K7 ?+ `: x' ~
- #include<winsock2.h>/ a& _+ O( N8 o6 t, C* G$ Q9 |( b
- #include<stdio.h>
5 m7 Z& y& x7 ]; i1 F2 k; T - #pragma comment(lib, "ws2_32.lib")) j5 y0 g- B. t* o
- void main()1 T2 J1 u. D, `8 g; X
- {
% n& X/ h8 C! l+ b+ Y+ n# R$ c - WSADATA wsaData;
4 M% X( z k& ` - SOCKET sockClient;//客户端Socket
0 O( K$ J2 d; D) V - SOCKADDR_IN addrServer;//服务端地址- j1 {! ?6 ?9 a( Y U( I" d
- WSAStartup(MAKEWORD(2, 2), &wsaData);) W! j! N: a* F" ^
- char message[20] = "HelloSocket!";
) A: n; G) t! z0 a7 v! c# q - //定义要连接的服务端地址
# [) }6 f4 F, k - addrServer.sin_addr.S_un.S_addr = inet_addr("127.0.0.1");//目标IP(127.0.0.1是回送地址)
) p% a! G; w2 q2 m7 P - addrServer.sin_family = AF_INET;
/ A# c0 u6 c5 ^! p( j8 [ i5 C - addrServer.sin_port = htons(6000);//连接端口6000
% O( m- z. p4 H) D1 I1 q u - //新建客户端socket w, n: j$ X% j5 O5 s [& Z' w8 C
- sockClient = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);4 X( }' g6 h( I. @( {" S, N5 S
- int s = 0;
& w# @, o7 L% v4 ` - s = connect(sockClient, (SOCKADDR*)&addrServer, sizeof(SOCKADDR));
. p A. @+ B& r8 s1 a' o d - 8 W# m: f" w+ p! |* k& \. {4 ~
- while (1){
9 ]2 n' B' |, a7 M1 I - //发送数据* Y: \8 ^! j2 E/ j4 Q! ~
- int a = send(sockClient, message, strlen(message) + 1, 0);: m ~6 f' }' [9 d' f4 T
- printf("to server:");( ]0 Y9 M6 y- D6 M9 f
- scanf("%s", message);
, ]' a7 o! X; ~+ b
0 H, l* ^& ^; f) `3 A- }" R6 Y) `# G: r' {
- //关闭socket
5 K& H( M* C3 G' f - closesocket(sockClient);
3 I3 K) p1 b" p - WSACleanup();# O7 E3 x: u9 C' T/ U
- }
复制代码 求懂的高手来解答一下。- a+ r0 Q/ D1 q) U. o% @) Q
谢谢了!( w# E; ~+ |" d4 J, {& b
' w; K8 F; Z' t/ v, {8 ?" E
. g: t5 _. T* s _3 X9 L6 a. n
^+ S4 n& g9 l) _3 H0 s |
|