高分求助,Winsocket收数据时出错,Error Code 10014-CSDN论坛
发布网友
发布时间:2022-04-25 15:30
我来回答
共1个回答
热心网友
时间:2023-10-13 02:00
int AddShareThread::Run()
{
strcpy(szServerIP,"192.168.0.93");
uServerPort = 5590;
//连接服务器
sockaddr_in server_addr;
server_addr.sin_family = AF_INET;
server_addr.sin_addr.s_addr = inet_addr(szServerIP);
server_addr.sin_port = htons(uServerPort);
SOCKET client = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if ( client == INVALID_SOCKET )
{
//出错处理,返回错误码
return -1;
}
int nResult = connect(client,(SOCKADDR*)&server_addr,sizeof(server_addr));
if ( nResult == SOCKET_ERROR )
{
return -2;
}
bool flag =1;
setsockopt( client, IPPROTO_TCP, TCP_NODELAY, (char *)&flag, sizeof(flag) );
CString szShareXml = GetFileXml(szPathArray);
char *xmlBuffer = new char[szShareXml.GetLength()*2];
memset(xmlBuffer,0,szShareXml.GetLength()*2);
strcpy(xmlBuffer,(char *)szShareXml.GetBuffer());
//发送请求头
Package p;
p.request = UPDATE;
p.len = strlen(xmlBuffer) + 1;
send(client,(char*)&p,sizeof(p),0);
//发送数据给服务器
flag = 0;
setsockopt( client, IPPROTO_TCP, TCP_NODELAY, (char *)&flag, sizeof(flag) );
int nSends = 0;
nSends = send(client,xmlBuffer,strlen(xmlBuffer)+1,0); //如果这里数据还在socket的缓冲区
closesocket(client); //这里的应该会把所有数据发送出去吧?
delete []xmlBuffer;
return 0; //closesocket后,线程马上返回会不会危险?比如数据没发完就中断了?
//return CWinThread::Run();
}