Reputation: 140
Edit: Turned out to be a problem with my embedded ethernet adapter. Works well now. Thanks everyone.
When I send a file over a tcp socket it takes a long time (~4 seconds for 1.5M bytes) for the transfer to complete. The same file travels almost instantly over tftp. I know tftp uses UDP, which should be faster, but I still think my transfer is WAY too slow. I'm connected 100Mbps half duplex, through a crossover cable. The sender is UNIX and the receiver is .Net on Windows TcpClient.
So, what does everyone think? Do I need some better C code? Is there maybe something wrong with the network?
Here is my C code:
int main(void)
{
//some initializing stuff
int AcceptSocket, ClientRecvSocket;
alen = sizeof(fsin);
int AcceptSocket = passiveTCP("20075", 10);
//Wait for client connections, and spawn a new thread to communicate with each one
pszRecvBuf = malloc((size_t) BUFSIZE);
while (1)
{
ClientRecvSocket = accept(AcceptSocket, &fsin, &alen);
printf("\nDebug: Accepted Connection\n");
if (ClientRecvSocket < 0)
{
sprintf(szStr, "Error accepting client connection : %d",
ClientRecvSocket);
perror(szStr);
}
else
{
printf("\nDebug: Starting Thread\n");
ThreadStatus = pthread_create(&ClientThread, NULL, ClientRecv,
(void *) &ClientRecvSocket);
pthread_join(ClientThread, NULL);
}
}
}
void *ClientRecv(void *ClientSocket)
{
pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL);
int Socket = *(int *) ClientSocket;
unsigned char *file_buffer;
file_buffer = malloc(1572864 * sizeof(unsigned char));
//set file data to something
SendLen = send(Socket, file_buffer, 1572864 * sizeof(unsigned char), 0);
shutdown(Socket, SHUT_RDWR);
free(file_buffer);
}
int passiveTCP(char *service, int qlen)
{
return passivesock(service, "tcp", qlen);
}
Upvotes: 0
Views: 1448
Reputation: 34592
At a glance, looking at it, within your function ClientRecv
, you're consuming too much resources...
unsigned char *file_buffer;
file_buffer = malloc(1572864 * sizeof(unsigned char));
You're allocating memory for that, but where is it gone to.... should try free'ing the pointer to that buffer....
As a matter of interest to serve and help others, is that some kind of wrapper framework you're using and please specify what kind.... as I strongly suspect that it is a non-standard software you are using - perhaps that software has certain "issues"?
Upvotes: 1
Reputation: 339985
Try connecting full duplex instead.
Since TCP requires an acknowledgement for (roughly) each transmitted packet, your half duplex link will have to stop transmitting to handle receipt of those ACKs. That could manifest as collisions, causing packet loss and ultimately triggering TCP's congestion controls.
Upvotes: 0