我正在构建一个具有RelayTCP功能的程序。 它的职责是客户端和服务器之间的转发数据。
客户< ----> RelayTCP< ----->服务器
以下代码在转发数据时属于RelayTCP。
char *buff = new char[BUF_MAX];
int recvLength = 0;
int sendLength = 0;
recvLength = recv(inSock, buff, BUF_MAX, 0);
buff[recvLength] = '\0';
sendLength = send(outSock, buff, recvLength, 0);
delete buff;
虽然我将BUF_MAX设置得非常大(例如:BUF_MAX = 4096000),但客户端非常缓慢地向服务器发送数据。我试图将套接字选项TCP_NODELAY设置为加速,但不成功
但是,在客户端直接连接到服务器的情况下。传输数据的速度非常快。
请告诉我为什么使用RelayTCP传输数据的速度非常慢。
--------------新信息---------------
对不起所有关于我发布的信息不足。 实际上,我的项目有两个程序:父母和孩子。
Parent将等待来自Client的连接,在接受后,它将连接到Server。之后它会生成Child进程。子进程继承了Parent的两个套接字。
Parent.cpp
ownAddr.sin_family = AF_INET;
ownAddr.sin_addr.s_addr = htonl(INADDR_ANY);
ownAddr.sin_port = htons(49280);
listenSock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
bind(listenSock, (SOCKADDR *)&ownAddr, sizeof(SOCKADDR_IN));
listen(listenSock, 5);
inSock = accept(listenSock, 0, 0);
BOOL bOpt = TRUE;
setsockopt(inSock, IPPROTO_TCP, TCP_NODELAY, (char *)&bOpt, sizeof(bOpt));
serverAddr.sin_family = AF_INET;
serverAddr.sin_addr.s_addr = inet_addr("192.168.44.131");
serverAddr.sin_port = htons(22);
outSock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
connect(outSock, (SOCKADDR *)&serverAddr, sizeof(SOCKADDR_IN));
setsockopt(outSock, IPPROTO_TCP, TCP_NODELAY, (char *)&bOpt, sizeof(bOpt));
STARTUPINFO startInfo = {0};
PROCESS_INFORMATION processInfo = {0};
TCHAR cmd[1024] = {0};
TCHAR buff[1024];
GetCurrentDirectory(1024, buff);
wsprintf(cmd, _T("\"%s\\RelayTCP.exe\""), buff);
ZeroMemory(buff, 1024);
wsprintf(buff, _T(" %d %d"), inSock, outSock);
_tcscat(cmd, buff);
CreateProcess(NULL, cmd, NULL, NULL, TRUE, CREATE_NEW_CONSOLE, NULL, NULL, &startInfo, &processInfo);
Child.cpp
WSAEVENT eventArray[2];
SOCKET sockArray[2];
int result = 0;
sockArray[0] = (SOCKET)_tstoi(argv[1]);
sockArray[1] = (SOCKET)_tstoi(argv[2]);
WSAStartup(MAKEWORD(2, 2), &wsaData);
for (int i = 0; i < 2; i++)
{
eventArray[i] = WSACreateEvent();
WSAEventSelect(sockArray[i], eventArray[i], FD_READ | FD_WRITE | FD_CLOSE);
}
DWORD index = 0;
WSANETWORKEVENTS networkEvent;
while(TRUE)
{
index = WSAWaitForMultipleEvents(eventTotal, eventArray, FALSE, WSA_INFINITE, FALSE);
index = index - WSA_WAIT_EVENT_0;
for (int i = index; i < eventTotal; i++)
{
index = WSAWaitForMultipleEvents(1, &eventArray[i], TRUE, 1000, FALSE);
if (index == WSA_WAIT_FAILED || index == WSA_WAIT_TIMEOUT)
{
continue;
}
index = i;
WSAResetEvent(eventArray[index]);
WSAEnumNetworkEvents(sockArray[index], eventArray[index], &networkEvent);
if (networkEvent.lNetworkEvents & FD_READ)
{
char *buff = new char[BUF_MAX];
int recvLength = 0;
int sendLength = 0;
recvLength = recv(sockArray[index], buff, BUF_MAX, 0);
buff[recvLength] = '\0';
sendLength = send(sockArray[1 - index], buff, recvLength, 0);
printf("recvLength = %d, sendLength = %d\n", recvLength, sendLength);
delete [] buff;
}
if (networkEvent.lNetworkEvents & FD_WRITE)
{
// do nothing
}
if (networkEvent.lNetworkEvents & FD_CLOSE)
{
for (int i = 0; i < 2; i++)
{
closesocket(sockArray[i]);
WSACloseEvent(eventArray[i]);
}
break;
}
}
}