我把server端放在一台公网服务器上,然后让很多人来连接测试,大部分电脑没任何问题,无论发什么文件.(发1M的EXE和100M的TXT都没问题)
然后我给另外一台电脑测试,是光纤的网络,出现那个神奇的问题(发送1M多的EXE文件出现10054错误,发送14M的txt则没问题?)
当然还有客户端些连接上来时,服务器端recv时也有时会出错10054
10054:是说远程端关掉了连接,也就是说客户端主动closesocket了咯.可没有啊...
测试是这样的,服务器send不出去了,然后发现10054,而客户端一直在recv(阻塞)的等数据包过来,也就是发那个EXE文件时总在93%时候卡住不动了.
起初以为是防火墙把我的socket关掉了,后来退出卡巴斯基了也不行...
server端:
1:监听线程(来一连接,便创建一接收线程服务于它)
DWORD WINAPI CTCPServer_FT::ThreadListen(LPVOID lpParam)
{
CTCPServer_FT* pServer = (CTCPServer_FT*)lpParam;
SOCKET sockListen = socket(AF_INET, SOCK_STREAM, 0);
SOCKADDR_IN sin;
sin.sin_family = AF_INET;
sin.sin_addr.s_addr = 0;
sin.sin_port = htons(pServer->m_iListenPort);
if(bind(sockListen, (SOCKADDR*)&sin, sizeof(sin)) == SOCKET_ERROR)
{
closesocket(sockListen);
pServer->m_pWnd->SendMessage(WM_BINDERROR);
return 1;
}
if(listen(sockListen, 5) == SOCKET_ERROR)
{
closesocket(sockListen);
pServer->m_pWnd->SendMessage(WM_LISTENERROR);
return 1;
}
pServer->m_pWnd->SendMessage(WM_STARTLISTEN);
fd_set fdListen;
timeval seltime;
seltime.tv_sec = 0;
seltime.tv_usec = 10000;
while(!pServer->m_bEndListenThread)
{
FD_ZERO(&fdListen);
FD_SET(sockListen, &fdListen);
if(select(0, &fdListen, NULL, NULL, &seltime) <= 0 || !FD_ISSET(sockListen, &fdListen) )
continue;
int len = sizeof(sin);
SOCKET sock = accept(sockListen, (SOCKADDR*)&sin, &len);
PARAMRECV* pParamRecv = new PARAMRECV;
pParamRecv->sock = sock;
pParamRecv->ptr = pServer;
DWORD id;
HANDLE h = CreateThread(NULL, 0, ThreadRecv, pParamRecv, 0, &id);
CloseHandle(h);
}
closesocket(sockListen);
return 0;
}
2:接收线程
DWORD WINAPI CTCPServer_FT::ThreadRecv(LPVOID lpParam)
{
PARAMRECV* pParam = (PARAMRECV*)lpParam;
CTCPServer_FT* pServer = (CTCPServer_FT*)pParam->ptr;
fd_set fdRecv;
timeval seltime;
seltime.tv_sec = 0;
seltime.tv_usec = 5000;
while(1)
{
FD_ZERO(&fdRecv);
FD_SET(pParam->sock, &fdRecv);
if(select(0, &fdRecv, NULL, NULL, &seltime) <= 0 || !FD_ISSET(pParam->sock, &fdRecv) )
continue;
MSGREQUEST msgRequest;
int iRecvCnt = recv(pParam->sock, (char*)&msgRequest, sizeof(msgRequest), 0);
if(iRecvCnt <= 0)
break;
//请求文件列表
if(msgRequest.iCommand == FILELIST)
{
pServer->m_pWnd->SendMessage(WM_SENDFILELIST, (WPARAM)&pParam->sock, 0);
}
//请求文件数据
else if(msgRequest.iCommand == FILEDATA)
{
long lFileOffset = msgRequest.lFileOffset;
CFile file;
BOOL bResult = file.Open(msgRequest.sServerPath, CFile::modeRead|CFile::shareDenyNone|CFile::typeBinary, NULL);
if(!bResult)
{
break; //如果文件打开失败就终止线程
}
char sSendBuf[SENDSIZE]={NULL};
while(lFileOffset < msgRequest.lFileLength)
{
int iSeek = file.Seek(lFileOffset, CFile::begin);
int iReadCnt = file.Read(sSendBuf, SENDSIZE);
if(iReadCnt ==0)
break;
int iSendCnt = send(pParam->sock, sSendBuf, iReadCnt, 0);
//发送失败就终止线程
if(iSendCnt == -1)//我发送一个1.5M的EXE文件,每次接收到93%多时就出10054错误了,发大数据量TXT没问题
{
DWORD dwError = GetLastError();
TRACE(" 错误代码(%d),已发送字节(%d),须发送(%d)\n", dwError, lFileOffset, msgRequest.lFileLength );
break;
}
else
lFileOffset += iSendCnt;
}//end of while(lFileOffset < lFileLength)
file.Close();
}//end of else if(msgRequest.iCommand == FILEDATA)
break;
}//end of while(1)
closesocket(pParam->sock);
delete pParam;
return 0;
}
client端:
1: 创建线程(请求下载文件)
void CTCPClient_FT::RequestFile(MSGREQUEST msgRequest)
{
PARAMREQUEST* pParam = new PARAMREQUEST;
strcpy(pParam->sIP, m_sIP);
pParam->iPort = m_iServerPort;
pParam->msgRequest.iCommand = msgRequest.iCommand ;
pParam->msgRequest.lFileLength = msgRequest.lFileLength;
pParam->msgRequest.lFileOffset = msgRequest.lFileOffset;
strcpy(pParam->msgRequest.sClientPath, msgRequest.sClientPath);
strcpy(pParam->msgRequest.sServerPath, msgRequest.sServerPath);
pParam->ptr = m_pWnd;
DWORD id;
HANDLE h = CreateThread(NULL, 0, ThreadRequestFile, pParam, 0, &id);
CloseHandle(h);
}
2: 文件下载线程
DWORD WINAPI CTCPClient_FT::ThreadRequestFile(LPVOID lpParam)
{
PARAMREQUEST* pParam = (PARAMREQUEST*)lpParam;
CWnd* pWnd = (CWnd*)pParam->ptr;
SOCKET sock = socket(AF_INET, SOCK_STREAM, 0);
SOCKADDR_IN sin;
sin.sin_family = AF_INET;
sin.sin_addr.s_addr = inet_addr(pParam->sIP);
sin.sin_port = htons(pParam->iPort);
if(connect(sock, (SOCKADDR*)&sin, sizeof(sin)) != SOCKET_ERROR)
{
//发送请求
int iSendCnt = send(sock, (char*)&pParam->msgRequest, sizeof(MSGREQUEST), 0);
int iRecvCnt = -1;
if(pParam->msgRequest.iCommand == FILELIST) //如果是请求文件列表
{
while(!CTCPClient_FT::m_bEndRecvThread)
{
MSGFILELIST msgFileList;
iRecvCnt = recv(sock, (char*)&msgFileList, sizeof(msgFileList), 0);
if(iRecvCnt <= 0)
break;
pWnd->SendMessage(WM_RECVFILELIST, (WPARAM)&msgFileList, 0);
}
}
else if(pParam->msgRequest.iCommand == FILEDATA) //如果是请求文件数据
{
char sRecvBuf[RECVSIZE] = {NULL};
long lFileOffset = pParam->msgRequest.lFileOffset;
CFile file;
BOOL bResult = file.Open(pParam->msgRequest.sClientPath, CFile::modeWrite|CFile::modeNoTruncate|CFile::typeBinary, NULL);
while(!CTCPClient_FT::m_bEndRecvThread && bResult && lFileOffset < pParam->msgRequest.lFileLength)
{
int iRecvCnt = recv(sock, sRecvBuf, RECVSIZE, 0);
if(iRecvCnt <= 0)
{
DWORD dwError = GetLastError();
CString strWarring;
strWarring.Format("接收实际文件(%s),接收字节: %d, 错误代码(%d)\n", pParam->msgRequest.sServerPath, iRecvCnt, dwError);
AfxMessageBox(strWarring);
break;
}
file.Seek(lFileOffset, CFile::begin);
file.Write(sRecvBuf, iRecvCnt);
lFileOffset += iRecvCnt;
MSGFILESTATUS msgFileStatus;
msgFileStatus.lFileOffset = lFileOffset;
strcpy(msgFileStatus.sServerPath, pParam->msgRequest.sServerPath);
pWnd->SendMessage(WM_FILESTATUS, (WPARAM)&msgFileStatus, 0);
}
file.Close();
}
}
else
{
pWnd->SendMessage(WM_CONNECTERROR);
}
DWORD dwError = GetLastError();
CString strWarring;
strWarring.Format("类型: %d, 实际文件(%s), 错误代码(%d)\n",pParam->msgRequest.iCommand, pParam->msgRequest.sServerPath, dwError);
AfxMessageBox(strWarring);
closesocket(sock);
delete pParam;
return 0;
}
困扰我两天了,哪位大哥大姐帮忙看看...在此谢过先...
ps:这是我看到的网上一仁兄的例子,哪位大哥有空可以留个email,我直接发完整代码你看看...
24 个解决方案
#1
代码太长了,看不过来。
发送exe文件被对方强行关闭连接,而发送txt文件没问题,很容易让人联想到是安全软件起了作用。
发送exe文件被对方强行关闭连接,而发送txt文件没问题,很容易让人联想到是安全软件起了作用。
#2
试试把exe文件名称改成txt看看如何。
#3
另外再换成其它exe文件试试。
#4
估计是防火墙的问题!
#5
估计防火墙问题,你把文件后缀改了..
#6
看不懂
#7
vckbase上的代码吧,我也用过,没出问题呀。
#8
up
#9
嗯,是VCKBASE上的代码,大部分的电脑都没问题,但有些电脑就是出这问题
汗,还是这么神奇...
我改后缀也没用,换其他的EXE只要是小于1M的基本上可以成功下载,而1M以上的一般就下载到80% 90%多服务器就报10054...
防火墙啥都给关了也不行...
哎...
汗,还是这么神奇...
我改后缀也没用,换其他的EXE只要是小于1M的基本上可以成功下载,而1M以上的一般就下载到80% 90%多服务器就报10054...
防火墙啥都给关了也不行...
哎...
#10
出现10054的根源是什么咧?
#11
没人
此帖就此沉沦?
此帖就此沉沦?
#12
服务器报10054的同时客户端有没getlasterror看看
#13
客户端一直阻塞的recv等,超时后也报10054
#14
代码不是这么写的,和杀读软件无关
if(select(0, &fdRecv, NULL, NULL, &seltime) <= 0 ¦ ¦ !FD_ISSET(pParam->sock, &fdRecv) )
continue;
MSGREQUEST msgRequest;
int iRecvCnt = recv(pParam->sock, (char*)&msgRequest, sizeof(msgRequest), 0);
if(iRecvCnt <= 0)
break;
if(select(0, &fdRecv, NULL, NULL, &seltime) <= 0 ¦ ¦ !FD_ISSET(pParam->sock, &fdRecv) )
continue;
MSGREQUEST msgRequest;
int iRecvCnt = recv(pParam->sock, (char*)&msgRequest, sizeof(msgRequest), 0);
if(iRecvCnt <= 0)
break;
#15
还请楼上大哥详细说明下,谢谢
下面的recv应该就是MSGREQUEST msgRequest; 需要再初始化一下吧?
上面的select咧? 具体哪里错了,我还不是很清楚,请指点
下面的recv应该就是MSGREQUEST msgRequest; 需要再初始化一下吧?
上面的select咧? 具体哪里错了,我还不是很清楚,请指点
#16
这是标准的select模型...我没有看出什么大不同...请帮忙,谢谢
#include <winsock.h>
#include <stdio.h>
#define PORT 5150
#define MSGSIZE 1024
#pragma comment(lib, "ws2_32.lib")
int g_iTotalConn = 0;
SOCKET g_CliSocketArr[FD_SETSIZE];
DWORD WINAPI WorkerThread(LPVOID lpParameter);
int main()
{
WSADATA wsaData;
SOCKET sListen, sClient;
SOCKADDR_IN local, client;
int iaddrSize = sizeof(SOCKADDR_IN);
DWORD dwThreadId;
// Initialize Windows socket library
WSAStartup(0x0202, &wsaData);
// Create listening socket
sListen = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
// Bind
local.sin_addr.S_un.S_addr = htonl(INADDR_ANY);
local.sin_family = AF_INET;
local.sin_port = htons(PORT);
bind(sListen, (struct sockaddr *)&local, sizeof(SOCKADDR_IN));
// Listen
listen(sListen, 3);
// Create worker thread
CreateThread(NULL, 0, WorkerThread, NULL, 0, &dwThreadId);
while (TRUE)
{
// Accept a connection
sClient = accept(sListen, (struct sockaddr *)&client, &iaddrSize);
printf("Accepted client:%s:%d\n", inet_ntoa(client.sin_addr), ntohs(client.sin_port));
// Add socket to fdTotal
g_CliSocketArr[g_iTotalConn++] = sClient;
}
return 0;
}
DWORD WINAPI WorkerThread(LPVOID lpParam)
{
int i;
fd_set fdread;
int ret;
struct timeval tv = {1, 0};
char szMessage[MSGSIZE];
while (TRUE)
{
FD_ZERO(&fdread);
for (i = 0; i < g_iTotalConn; i++)
{
FD_SET(g_CliSocketArr[i], &fdread);
}
// We only care read event
ret = select(0, &fdread, NULL, NULL, &tv);
if (ret == 0)
{
// Time expired
continue;
}
for (i = 0; i < g_iTotalConn; i++)
{
if (FD_ISSET(g_CliSocketArr[i], &fdread))
{
// A read event happened on pfdTotal->fd_array[i]
ret = recv(g_CliSocketArr[i], szMessage, MSGSIZE, 0);
if (ret == 0 || (ret == SOCKET_ERROR && WSAGetLastError() == WSAECONNRESET))
{
// Client socket closed
printf("Client socket %d closed.\n", g_CliSocketArr[i]);
closesocket(g_CliSocketArr[i]);
if (i < g_iTotalConn - 1)
{
g_CliSocketArr[i--] = g_CliSocketArr[--g_iTotalConn];
}
}
else
{
// We received a message from client
szMessage[ret] = '\0';
send(g_CliSocketArr[i], szMessage, strlen(szMessage), 0);
}
}
}
}
return 0;
}
#include <winsock.h>
#include <stdio.h>
#define PORT 5150
#define MSGSIZE 1024
#pragma comment(lib, "ws2_32.lib")
int g_iTotalConn = 0;
SOCKET g_CliSocketArr[FD_SETSIZE];
DWORD WINAPI WorkerThread(LPVOID lpParameter);
int main()
{
WSADATA wsaData;
SOCKET sListen, sClient;
SOCKADDR_IN local, client;
int iaddrSize = sizeof(SOCKADDR_IN);
DWORD dwThreadId;
// Initialize Windows socket library
WSAStartup(0x0202, &wsaData);
// Create listening socket
sListen = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
// Bind
local.sin_addr.S_un.S_addr = htonl(INADDR_ANY);
local.sin_family = AF_INET;
local.sin_port = htons(PORT);
bind(sListen, (struct sockaddr *)&local, sizeof(SOCKADDR_IN));
// Listen
listen(sListen, 3);
// Create worker thread
CreateThread(NULL, 0, WorkerThread, NULL, 0, &dwThreadId);
while (TRUE)
{
// Accept a connection
sClient = accept(sListen, (struct sockaddr *)&client, &iaddrSize);
printf("Accepted client:%s:%d\n", inet_ntoa(client.sin_addr), ntohs(client.sin_port));
// Add socket to fdTotal
g_CliSocketArr[g_iTotalConn++] = sClient;
}
return 0;
}
DWORD WINAPI WorkerThread(LPVOID lpParam)
{
int i;
fd_set fdread;
int ret;
struct timeval tv = {1, 0};
char szMessage[MSGSIZE];
while (TRUE)
{
FD_ZERO(&fdread);
for (i = 0; i < g_iTotalConn; i++)
{
FD_SET(g_CliSocketArr[i], &fdread);
}
// We only care read event
ret = select(0, &fdread, NULL, NULL, &tv);
if (ret == 0)
{
// Time expired
continue;
}
for (i = 0; i < g_iTotalConn; i++)
{
if (FD_ISSET(g_CliSocketArr[i], &fdread))
{
// A read event happened on pfdTotal->fd_array[i]
ret = recv(g_CliSocketArr[i], szMessage, MSGSIZE, 0);
if (ret == 0 || (ret == SOCKET_ERROR && WSAGetLastError() == WSAECONNRESET))
{
// Client socket closed
printf("Client socket %d closed.\n", g_CliSocketArr[i]);
closesocket(g_CliSocketArr[i]);
if (i < g_iTotalConn - 1)
{
g_CliSocketArr[i--] = g_CliSocketArr[--g_iTotalConn];
}
}
else
{
// We received a message from client
szMessage[ret] = '\0';
send(g_CliSocketArr[i], szMessage, strlen(szMessage), 0);
}
}
}
}
return 0;
}
#17
在线等...哪位路过的大哥帮忙看...
愿给出我所有可用分表示感谢...嘿...
愿给出我所有可用分表示感谢...嘿...
#18
UP
#19
Windows的关闭了没有
#20
什么叫windows的关闭了没?
我把服务器端与客户端的closesocket全部去掉了,也还有10054,证明关闭这sokcet的是其他软件引起的,是这样不?
也就是客户端机器上的安全软件引起的...
都汗了,我把卡巴斯基退掉了也还有这问题
我把服务器端与客户端的closesocket全部去掉了,也还有10054,证明关闭这sokcet的是其他软件引起的,是这样不?
也就是客户端机器上的安全软件引起的...
都汗了,我把卡巴斯基退掉了也还有这问题
#21
呵,头一次感觉这里这么冷清.
我的问题没描述清楚,还是有点叼钻?
那问下,哪位有好点的传输文件的实例给我借鉴下,我用IOCP也测试过,效果也不理想,那个光纤的网络还是接收不到。
谢谢先。
email: yijshu@163.com
我的问题没描述清楚,还是有点叼钻?
那问下,哪位有好点的传输文件的实例给我借鉴下,我用IOCP也测试过,效果也不理想,那个光纤的网络还是接收不到。
谢谢先。
email: yijshu@163.com
#22
mark
#24
一个月了,问题依然还是没解决,但帖子还是得结...
#1
代码太长了,看不过来。
发送exe文件被对方强行关闭连接,而发送txt文件没问题,很容易让人联想到是安全软件起了作用。
发送exe文件被对方强行关闭连接,而发送txt文件没问题,很容易让人联想到是安全软件起了作用。
#2
试试把exe文件名称改成txt看看如何。
#3
另外再换成其它exe文件试试。
#4
估计是防火墙的问题!
#5
估计防火墙问题,你把文件后缀改了..
#6
看不懂
#7
vckbase上的代码吧,我也用过,没出问题呀。
#8
up
#9
嗯,是VCKBASE上的代码,大部分的电脑都没问题,但有些电脑就是出这问题
汗,还是这么神奇...
我改后缀也没用,换其他的EXE只要是小于1M的基本上可以成功下载,而1M以上的一般就下载到80% 90%多服务器就报10054...
防火墙啥都给关了也不行...
哎...
汗,还是这么神奇...
我改后缀也没用,换其他的EXE只要是小于1M的基本上可以成功下载,而1M以上的一般就下载到80% 90%多服务器就报10054...
防火墙啥都给关了也不行...
哎...
#10
出现10054的根源是什么咧?
#11
没人
此帖就此沉沦?
此帖就此沉沦?
#12
服务器报10054的同时客户端有没getlasterror看看
#13
客户端一直阻塞的recv等,超时后也报10054
#14
代码不是这么写的,和杀读软件无关
if(select(0, &fdRecv, NULL, NULL, &seltime) <= 0 ¦ ¦ !FD_ISSET(pParam->sock, &fdRecv) )
continue;
MSGREQUEST msgRequest;
int iRecvCnt = recv(pParam->sock, (char*)&msgRequest, sizeof(msgRequest), 0);
if(iRecvCnt <= 0)
break;
if(select(0, &fdRecv, NULL, NULL, &seltime) <= 0 ¦ ¦ !FD_ISSET(pParam->sock, &fdRecv) )
continue;
MSGREQUEST msgRequest;
int iRecvCnt = recv(pParam->sock, (char*)&msgRequest, sizeof(msgRequest), 0);
if(iRecvCnt <= 0)
break;
#15
还请楼上大哥详细说明下,谢谢
下面的recv应该就是MSGREQUEST msgRequest; 需要再初始化一下吧?
上面的select咧? 具体哪里错了,我还不是很清楚,请指点
下面的recv应该就是MSGREQUEST msgRequest; 需要再初始化一下吧?
上面的select咧? 具体哪里错了,我还不是很清楚,请指点
#16
这是标准的select模型...我没有看出什么大不同...请帮忙,谢谢
#include <winsock.h>
#include <stdio.h>
#define PORT 5150
#define MSGSIZE 1024
#pragma comment(lib, "ws2_32.lib")
int g_iTotalConn = 0;
SOCKET g_CliSocketArr[FD_SETSIZE];
DWORD WINAPI WorkerThread(LPVOID lpParameter);
int main()
{
WSADATA wsaData;
SOCKET sListen, sClient;
SOCKADDR_IN local, client;
int iaddrSize = sizeof(SOCKADDR_IN);
DWORD dwThreadId;
// Initialize Windows socket library
WSAStartup(0x0202, &wsaData);
// Create listening socket
sListen = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
// Bind
local.sin_addr.S_un.S_addr = htonl(INADDR_ANY);
local.sin_family = AF_INET;
local.sin_port = htons(PORT);
bind(sListen, (struct sockaddr *)&local, sizeof(SOCKADDR_IN));
// Listen
listen(sListen, 3);
// Create worker thread
CreateThread(NULL, 0, WorkerThread, NULL, 0, &dwThreadId);
while (TRUE)
{
// Accept a connection
sClient = accept(sListen, (struct sockaddr *)&client, &iaddrSize);
printf("Accepted client:%s:%d\n", inet_ntoa(client.sin_addr), ntohs(client.sin_port));
// Add socket to fdTotal
g_CliSocketArr[g_iTotalConn++] = sClient;
}
return 0;
}
DWORD WINAPI WorkerThread(LPVOID lpParam)
{
int i;
fd_set fdread;
int ret;
struct timeval tv = {1, 0};
char szMessage[MSGSIZE];
while (TRUE)
{
FD_ZERO(&fdread);
for (i = 0; i < g_iTotalConn; i++)
{
FD_SET(g_CliSocketArr[i], &fdread);
}
// We only care read event
ret = select(0, &fdread, NULL, NULL, &tv);
if (ret == 0)
{
// Time expired
continue;
}
for (i = 0; i < g_iTotalConn; i++)
{
if (FD_ISSET(g_CliSocketArr[i], &fdread))
{
// A read event happened on pfdTotal->fd_array[i]
ret = recv(g_CliSocketArr[i], szMessage, MSGSIZE, 0);
if (ret == 0 || (ret == SOCKET_ERROR && WSAGetLastError() == WSAECONNRESET))
{
// Client socket closed
printf("Client socket %d closed.\n", g_CliSocketArr[i]);
closesocket(g_CliSocketArr[i]);
if (i < g_iTotalConn - 1)
{
g_CliSocketArr[i--] = g_CliSocketArr[--g_iTotalConn];
}
}
else
{
// We received a message from client
szMessage[ret] = '\0';
send(g_CliSocketArr[i], szMessage, strlen(szMessage), 0);
}
}
}
}
return 0;
}
#include <winsock.h>
#include <stdio.h>
#define PORT 5150
#define MSGSIZE 1024
#pragma comment(lib, "ws2_32.lib")
int g_iTotalConn = 0;
SOCKET g_CliSocketArr[FD_SETSIZE];
DWORD WINAPI WorkerThread(LPVOID lpParameter);
int main()
{
WSADATA wsaData;
SOCKET sListen, sClient;
SOCKADDR_IN local, client;
int iaddrSize = sizeof(SOCKADDR_IN);
DWORD dwThreadId;
// Initialize Windows socket library
WSAStartup(0x0202, &wsaData);
// Create listening socket
sListen = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
// Bind
local.sin_addr.S_un.S_addr = htonl(INADDR_ANY);
local.sin_family = AF_INET;
local.sin_port = htons(PORT);
bind(sListen, (struct sockaddr *)&local, sizeof(SOCKADDR_IN));
// Listen
listen(sListen, 3);
// Create worker thread
CreateThread(NULL, 0, WorkerThread, NULL, 0, &dwThreadId);
while (TRUE)
{
// Accept a connection
sClient = accept(sListen, (struct sockaddr *)&client, &iaddrSize);
printf("Accepted client:%s:%d\n", inet_ntoa(client.sin_addr), ntohs(client.sin_port));
// Add socket to fdTotal
g_CliSocketArr[g_iTotalConn++] = sClient;
}
return 0;
}
DWORD WINAPI WorkerThread(LPVOID lpParam)
{
int i;
fd_set fdread;
int ret;
struct timeval tv = {1, 0};
char szMessage[MSGSIZE];
while (TRUE)
{
FD_ZERO(&fdread);
for (i = 0; i < g_iTotalConn; i++)
{
FD_SET(g_CliSocketArr[i], &fdread);
}
// We only care read event
ret = select(0, &fdread, NULL, NULL, &tv);
if (ret == 0)
{
// Time expired
continue;
}
for (i = 0; i < g_iTotalConn; i++)
{
if (FD_ISSET(g_CliSocketArr[i], &fdread))
{
// A read event happened on pfdTotal->fd_array[i]
ret = recv(g_CliSocketArr[i], szMessage, MSGSIZE, 0);
if (ret == 0 || (ret == SOCKET_ERROR && WSAGetLastError() == WSAECONNRESET))
{
// Client socket closed
printf("Client socket %d closed.\n", g_CliSocketArr[i]);
closesocket(g_CliSocketArr[i]);
if (i < g_iTotalConn - 1)
{
g_CliSocketArr[i--] = g_CliSocketArr[--g_iTotalConn];
}
}
else
{
// We received a message from client
szMessage[ret] = '\0';
send(g_CliSocketArr[i], szMessage, strlen(szMessage), 0);
}
}
}
}
return 0;
}
#17
在线等...哪位路过的大哥帮忙看...
愿给出我所有可用分表示感谢...嘿...
愿给出我所有可用分表示感谢...嘿...
#18
UP
#19
Windows的关闭了没有
#20
什么叫windows的关闭了没?
我把服务器端与客户端的closesocket全部去掉了,也还有10054,证明关闭这sokcet的是其他软件引起的,是这样不?
也就是客户端机器上的安全软件引起的...
都汗了,我把卡巴斯基退掉了也还有这问题
我把服务器端与客户端的closesocket全部去掉了,也还有10054,证明关闭这sokcet的是其他软件引起的,是这样不?
也就是客户端机器上的安全软件引起的...
都汗了,我把卡巴斯基退掉了也还有这问题
#21
呵,头一次感觉这里这么冷清.
我的问题没描述清楚,还是有点叼钻?
那问下,哪位有好点的传输文件的实例给我借鉴下,我用IOCP也测试过,效果也不理想,那个光纤的网络还是接收不到。
谢谢先。
email: yijshu@163.com
我的问题没描述清楚,还是有点叼钻?
那问下,哪位有好点的传输文件的实例给我借鉴下,我用IOCP也测试过,效果也不理想,那个光纤的网络还是接收不到。
谢谢先。
email: yijshu@163.com
#22
mark
#23
#24
一个月了,问题依然还是没解决,但帖子还是得结...