socket网络编程,两个客户端线程同时跟服务器端进行网络通信时遇到问题
主线程(线程1)是一个与服务器端对话的普通线程
线程2的作用是接收报警消息,服务器端安装了报警采集装置,当有报警发生时会向线程2发送一条报警消息。
因为大部分时间都没有报警发生,服务器端没有给线程2发送任何消息,所以线程2应该是阻塞在recv函数那儿的。但是我运行程序的时候线程2中的recv()函数并没有阻塞,当报警发生时,线程2也收不到报警了
我怀疑是线程1的socket通信对线程2产生了影响,但是我把_beginthread(client , 0, NULL); //线程2启动 放到main()函数中的最后(while循环前面)时还是这个结果。
请高人指点!不慎感激!
运行结果是:
Sock 1 ============== 1924
Sock 2 ============== 1920
Thread 2 is Waitting.....................
Thread 1 Hello,Jim! I'm Lilei
Thread 1 Hello Lilei,I'm Jim
Thread 1 I wang you come to my home tonight
Thread 1 Sorry,I have no time tonight
Thread 2 len ===== 0
Thread 2 len ===== 0
Thread 2 len ===== 0
Thread 2 len ===== 0
Thread 2 len ===== 0
Thread 2 len ===== 0
Thread 2 len ===== 0
Thread 2 len ===== 0
Thread 2 len ===== 0
.
.
.
.
.
代码如下:
include <stdio.h>
#include <WINSOCK2.H>
#include <process.h>
#pragma comment(lib, "Ws2_32.lib")
void client(void *);
int main()
{
_beginthread(client , 0, NULL); //线程2启动
int sock;
WORD wVersionRequested;
WSADATA wsaData;
int err;
wVersionRequested = MAKEWORD( 2, 2 );
err = WSAStartup( wVersionRequested, &wsaData );
if ( err != 0 )
{
printf("Can't find a usable WinSock DLL\n");
return -1;
}
sock = socket(AF_INET, SOCK_STREAM, 0);
if (sock < 0)
{
printf("Create socket error: %d\n", WSAGetLastError());
return -1;
}
printf("Sock 1 ============== %d\n",sock);
sockaddr_in addr;
addr.sin_family = AF_INET;
addr.sin_port = htons(6000);
addr.sin_addr.s_addr = inet_addr("112.95.146.7");
intret;
ret = connect(sock, (sockaddr*)&addr, sizeof(sockaddr_in));
if (ret < 0)
{
printf("Connect error :%d\n", WSAGetLastError());
return -1;
}
charrecvbuf[1024] = {0};
charsendbuf[1024] = {0};
intlen;
strcpy(sendbuf, "Hello,Jim! I'm Lilei");
len = send(sock, sendbuf, strlen(sendbuf), 0);
printf("Thread 1 %s\n", sendbuf);
memset(sendbuf, 0, sizeof(sendbuf));
len = recv(sock, recvbuf, sizeof(recvbuf), 0);//server回复"Hello Lilei,I'm Jim"
printf("Thread 1 %s\n", recvbuf);
memset(recvbuf, 0, sizeof(recvbuf));
Sleep(1000);
strcpy(sendbuf, "I wang you come to my home tonight");
len = send(sock, sendbuf, strlen(sendbuf), 0);
printf("Thread 1 %s\n", sendbuf);
memset(sendbuf, 0, sizeof(sendbuf));
len = recv(sock, recvbuf, sizeof(recvbuf), 0); //server回复"Sorry,I have no time tonight"
printf("Thread 1 %s\n", recvbuf);
memset(recvbuf, 0, sizeof(recvbuf));
while(1)
{
};
}
void client(void *)
{
int sock2;
WORD wVersionRequested;
WSADATA wsaData;
int err;
wVersionRequested = MAKEWORD( 2, 2 );
err = WSAStartup( wVersionRequested, &wsaData );
if ( err != 0 )
{
printf("Can't find a usable WinSock DLL\n");
return ;
}
sock2 = socket(AF_INET, SOCK_STREAM, 0);
if (sock2 < 0)
{
printf("Create socket error: %d\n", WSAGetLastError());
return ;
}
printf("Sock 2 ============== %d\n",sock2);
sockaddr_in addr;
addr.sin_family = AF_INET;
addr.sin_port = htons(63456);
addr.sin_addr.s_addr = inet_addr("112.95.146.7");
intret,len;
charrecvbuf[1024] = {0};
ret = connect(sock2, (sockaddr*)&addr, sizeof(sockaddr_in));
if (ret < 0)
{
printf("Connect error :%d\n", WSAGetLastError());
return ;
}
printf("Thread 2 is Waitting.....................");
while (1)
{
len = recv(sock2, recvbuf, sizeof(recvbuf), 0);
if (len > 0)
{
printf("Thread 2 recvbuf==============%s\n", recvbuf); //等待server回复
memset(recvbuf, 0, sizeof(recvbuf));
Sleep(1000);
}
else
{
printf("Thread 2 len =====%d\n",len);
Sleep(1000);
}
}
}
[解决办法]
recv返回
<0:出错,
=0:连接关闭,
>0接收到数据大小
还是我1楼说的 0 已经断开了