-
April 2nd, 2004, 10:27 AM
#1
Sockets transmission speed
Hello everybody:
I am writing a nonMFC server application using winsock2, first of all, i must tell that i have 2 classes, CServer and CServerClient...i have created one server instance listening on a port, and when a client arrives, the server creates a thread and put an instance of the client in the thread, all this seems to work well, but the problem is that every client receive data and put that data into a file, (for example if i had 4 clients y would have 4 files), the problem is that when i send a big amount of data (for example 890 kb) it works really really slow.
I receive the data one character at time, when i got an entire line, then i write in the file....i dont know if there is another way that can improve the transmission of data, i tried to use a big buffer on recv command, but for example if i connect to mi server using a telnet window, i start receivng the data character by character!! i hope somebody can help me with this...
//here is the client code who receive the data
while(1)
{
do{ // reading a line
if(recv(this->hndSocket, Mibuff,1, 0) == SOCKET_ERROR)
break;
strncat(line, Mibuff, 1); //Mibuff is char[5]
//line is char[1024]
}while (Mibuff[0] != '\n');
if (Mibuff[0] == '\n') {
//here comes the writing on the file, line by line
//and the memset of line and mibuff, to go on in the while
//
}
On the other hand, my server is supposed to handle a big amount of connections, and i think that my way of creating the threads is insufficient to handle a lot of connections, i dont even know how many sockets connections you can open, i hear that you can only open 255 sockets but im not sure of that, i hope somebody can tell me how can i improve mi connection pool
//this code is placed in a function called, acceptconnection()
//this function belong to the server class
LPVOID dwThrdParam;
ServerClient * client;
client = new ServerClient;
client->hndSocket = socAceptado;
dwThrdParam = client;
hThread = CreateThread(
NULL, // no security attributes
0, // use default stack size
ThreadFunc, // thread function
dwThrdParam, // argument to thread function
0, // use default creation flags
&dwThreadId); // returns the thread identifier
//as you can see i am passing the client containing the accepted
//socket handle
Well, i hope mi question is clear and hope to hear form you soon
Greetings
Kcires
-
April 3rd, 2004, 03:00 AM
#2
Re: Sockets transmission speed
Originally posted by Kcires
Hello everybody:
I am writing a nonMFC server application using winsock2, first of all, i must tell that i have 2 classes, CServer and CServerClient...i have created one server instance listening on a port, and when a client arrives, the server creates a thread and put an instance of the client in the thread, all this seems to work well
fine...
, but the problem is that every client receive data and put that data into a file, (for example if i had 4 clients y would have 4 files), the problem is that when i send a big amount of data (for example 890 kb) it works really really slow.
There are many reasons to why you are recving slowly. But why are you recving byte by byte as shown in the code? That's bad. The job of a good receiver is to drain the buffer as quickly as possible and then process later. The same with the sender. If Nagle alg. doesn't come into play, then the overhead is almost 98%!!! A big wastage of bandwidth. Set the len parameter of recv (the third) to the size of the buffer. A good way to set the recv buffer is to set it on multiples of 4K. Another point to note is that tcp doesn't preserver message boundaries. So you may get it combined.
On the other hand, my server is supposed to handle a big amount of connections, and i think that my way of creating the threads is insufficient to handle a lot of connections, i dont even know how many sockets connections you can open, i hear that you can only open 255 sockets but im not sure of that, i hope somebody can tell me how can i improve mi connection pool
So what do you mean by 'big amount'? Is it in hundreds, thousands, or ten thousands? In the first case your strategy of one client per thread is fine. In the second case WSAEventSelect() model might be fine. In the final case high performance IOCP model is the best. for more info you may look at
this link
Even if our suggestions didn't help, please post the answer once you find it. We took the effort to help you, please return it to others.
* While posting code sections please use CODE tags
* Please check the codeguru FAQ and do a little search to see if your question have been answered before.
* Like a post, Rate The Post
* I blog: Network programming, Bible
I do all things thru CHRIST who strengthens me
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|