socket problem

This is the forum for miscellaneous technical/programming questions.

Moderator: 2ffat

socket problem

Postby rajesh » Fri Feb 05, 2010 6:35 am

I am using TServerSocket component for server and TClientSocket for client
they are non blocking . I want to know how many clients can server handle
smoothly . I am facing problem with clients .SendBuf method of the
server return -1 value if I send data to the clients in most cases
in loop i.e. so client not getting data smoothly every time.
this problem occurs randomly with any client . my code is

for(int i = 0 ; i < server->activecon ; i++)
{
int return = server->connecttion[i]->sendbuf(data,size);
// here value of return in -1 for many clients randomly
//data size is 50 bytes
// timer is 100 ms so tha that for loop is run after every 100ms
// client connected = 15 (fifteen)

}
rajesh
Active Poster
Active Poster
 
Posts: 19
Joined: Mon Oct 26, 2009 1:25 am

Re: socket problem

Postby arisme » Fri Feb 05, 2010 1:25 pm

rajesh wrote:I want to know how many clients can server handle
smoothly

"As many as there are in the queue..."

I think you may be testing the wrong thing - but that depends on how you plan to use the sockets. What is your ultimate objective? A broadcast to many clients? Individual responses to individual clients?

In practice, server communications would never be triggered by a timer - so your code is not testing a real life situation.

If you can, please explain what your objective is.

Aris
arisme
BCBJ Master
BCBJ Master
 
Posts: 357
Joined: Thu Jun 07, 2007 9:35 pm
Location: UK

Re: socket problem

Postby gambit47 » Sat Feb 06, 2010 12:26 am

rajesh wrote:I am facing problem with clients .SendBuf method of the server return -1 value


Assuming you do not have an OnClientError event handler assigned that is setting ErrorCode=0 to discard socket errors, then the only other way SendBuf() can return -1 without throwing an exception is if the send operation would block the socket (the WinSock API send() function returned SOCKET_ERROR, and WSAGetLastError() returned WSAEWOULDBLOCK), which is not allowed since you are using a non-blocking server. In that situation, you must wait for the blocked client's OnClientWrite event to fire before you can send new data to it. You will have to cache your unsent data and re-send it after that event has fired. What I usualy do is assign a TMemoryStream to each client's Data property, and then whenever SendBuf() returns -1 without exception, write the unsent data to the end of that TMemoryStream and let the OnClientWrite event send the contents of the stream to the client when it is ready. For example:

http://groups.google.com/group/borland.public.cppbuilder.internet.socket/msg/aef461da92b38bc1
Remy Lebeau (TeamB)
http://www.lebeausoftware.org
User avatar
gambit47
BCBJ Author
BCBJ Author
 
Posts: 472
Joined: Wed Jun 01, 2005 3:21 am
Location: California, USA

Re: socket problem

Postby rajesh » Sat Feb 06, 2010 12:46 am

I want Individual responses to individual clients . Actuall my server is listenning on particular port .
each client connect with the server at this port .I send him some data continously.

I have a database in which 30 column and 100 rows . data in database is changing randomly . 50 -100 values
continously every 100 ms . so what I am doing I am using a Timer of 100ms . and see data change in database
and if any change in the field in database I send the data to all connected client through loop defined above
and my data packet is only 50 bytes buffer . but sendbuf return -1 randomly for any clients.
suppose on any tick to timer it returns -1 for some clients and on another timer tick it return -1 for
another clients . And I am very much clear that every clients are connected. I am using non blocking sockets.
some time it succefully send data to all clients but it rarely happen. MY sevver is working 24 hours
I checked No memory leak in server . ram is 2 GB and server is windows 2003.
All the project on internet .
can you tell me where should I write the code for sending data to clients
rajesh
Active Poster
Active Poster
 
Posts: 19
Joined: Mon Oct 26, 2009 1:25 am

Re: socket problem

Postby rajesh » Sat Feb 06, 2010 1:04 am

I had already implemented OnClientError event handler and write setting ErrorCode=0 to discard socket errors
rajesh
Active Poster
Active Poster
 
Posts: 19
Joined: Mon Oct 26, 2009 1:25 am

Re: socket problem

Postby gambit47 » Sat Feb 06, 2010 8:08 pm

rajesh wrote:I want Individual responses to individual clients . Actuall my server is listenning on particular port .
each client connect with the server at this port .I send him some data continously.


The code I directed you to will be able to handle that. Each client has its own TMemoryStream cache allocate to it, and the OnClient... events are fired for individual clients independant of the other clients.

rajesh wrote:I have a database in which 30 column and 100 rows . data in database is changing randomly . 50 -100 values
continously every 100 ms . so what I am doing I am using a Timer of 100ms . and see data change in database
and if any change in the field in database I send the data to all connected client through loop defined above
and my data packet is only 50 bytes buffer .


It does not matter how large your messages are. Blocking can occur at any time for any byte size. It depends on the particular state of the socket at the time SendBuf() is called. Please study the code snippet I directed you to. The last portion of the code demonstates broadcasting a message to all connected clients, caching and all. I have used that approach in actual projects and it works fine when multiple clients are involved.

rajesh wrote:but sendbuf return -1 randomly for any clients.


I explained that in my previous reply. That is perfectly normal behavior for non-blocking sockets. Your code is just not handling it the way it needs to be handled yet.

rajesh wrote:suppose on any tick to timer it returns -1 for some clients and on another timer tick it return -1 for
another clients .


That is normal. You need to handle it on a per-client, per-send basis, as I have already dmonstrated to you.

rajesh wrote:I am using non blocking sockets.


That is exactly why you are getting into this situation in the first place. The -1 result means you are performing a blocking send at that moment, because the socket is still busy dealin with earlier data. You need to detect and handle the -1 correctly, as I explained to you earlier, in order to respect the non-blocking requirement of your sockets.

rajesh wrote:can you tell me where should I write the code for sending data to clients


I already did, in my previous reply. Please re-read it again, more carefully.
Remy Lebeau (TeamB)
http://www.lebeausoftware.org
User avatar
gambit47
BCBJ Author
BCBJ Author
 
Posts: 472
Joined: Wed Jun 01, 2005 3:21 am
Location: California, USA

Re: socket problem

Postby rajesh » Sun Feb 07, 2010 11:44 pm

Thanks for your help . I will r&d as you tell and let u know if face problems
rajesh
Active Poster
Active Poster
 
Posts: 19
Joined: Mon Oct 26, 2009 1:25 am

Re: socket problem

Postby rajesh » Fri Mar 12, 2010 11:34 pm

Thanku you now all is working fine . I want to know few more things

1. Is there any code left to write so that I update it. Please let me know

2.I have to stick with c++ builder6 because there is no socket components in the later version
can you suggest me the components I have to used in the later version to implement my server and client .becasue I want to switch from c++ builder6 please tell me the best components.

3. Is Indy tcpserver and tcpclient are best for my use .please tell me version also

4. can you send me the links where plenty of socket examples is c++builder6 so ,that I can develope my
client and server application bug free and very powerfull
rajesh
Active Poster
Active Poster
 
Posts: 19
Joined: Mon Oct 26, 2009 1:25 am

Re: socket problem

Postby gambit47 » Sat Mar 13, 2010 9:55 pm

rajesh wrote:I had already implemented OnClientError event handler and write setting ErrorCode=0 to discard socket errors


Then SendBuf() will return -1 on ANY socket failure, whether it be WSAEWOULDBLOCK or otherwise. Your code that is calling SendBuf() will have to manually call WSAGetLastError() directly to find out why SendBuf() returned -1, and then act accordingly (buffer the data if WSAEWOULDBLOCK is reported and wait for OnClientWrite, or fail your send disconnect on any other error).
Remy Lebeau (TeamB)
http://www.lebeausoftware.org
User avatar
gambit47
BCBJ Author
BCBJ Author
 
Posts: 472
Joined: Wed Jun 01, 2005 3:21 am
Location: California, USA

Re: socket problem

Postby gambit47 » Sat Mar 13, 2010 10:07 pm

rajesh wrote:I have to stick with c++ builder6 because there is no socket components in the later version


Yes, there is. There is the CLX TTcpClient/TcpServer components (which I do not recommend), and also Indy (such as TIdTCPClient/TIdTCPServer) is shipped with the IDE as well. The older TClientSocket/TServerSocket components are still available as well, they are just not installed by default anymore. You can manually install the "dclsocket..." package to contine using them (note that there are some Ansi/Unicode bugs in them in CB2009+, but that only affects methods like SendText() and ReceiveString() - use SendBuf() and ReceiveBuf() directly to work around those issues if needed).

rajesh wrote:Is Indy tcpserver and tcpclient are best for my use .


"best" is subjective. Indy will certainly handle everything that TClientSocket/TServerSocket is dong for you. Just keep in mind that Indy runs exclusively in blocking mode only, so you will have extra worker threads running, 1 for each listening port and 1 for each connected client, and you will have to make sure your event handler code is thread-safe. But if you are comfortable with TClientSocket/TServerSocket, then continue using them.
Remy Lebeau (TeamB)
http://www.lebeausoftware.org
User avatar
gambit47
BCBJ Author
BCBJ Author
 
Posts: 472
Joined: Wed Jun 01, 2005 3:21 am
Location: California, USA

Re: socket problem

Postby rajesh » Sat Mar 13, 2010 11:13 pm

Ya thanks I am Comfortable with my current code With TServerSocket and TClientSocket . But for future point of view I want to ship my code to Indy version . I will be glade if you provide me the some links that show indy socket implementation for client server model .
rajesh
Active Poster
Active Poster
 
Posts: 19
Joined: Mon Oct 26, 2009 1:25 am

Re: socket problem

Postby gambit47 » Sun Mar 14, 2010 11:49 pm

rajesh wrote:I will be glade if you provide me the some links that show indy socket implementation for client server model .


Look at the demos that are available on Indy's website, and search the forum archives at http://www.deja.com and http://forums.embarcadero.com for additional snippets that have been posted before.
Remy Lebeau (TeamB)
http://www.lebeausoftware.org
User avatar
gambit47
BCBJ Author
BCBJ Author
 
Posts: 472
Joined: Wed Jun 01, 2005 3:21 am
Location: California, USA

Re: socket problem

Postby gtokas » Thu Mar 18, 2010 11:28 am

First of all sorry for the long delay....
Without any disrespect to Remy.
Anyway,
All depends on the exchanged data volume...
Don't forget that the endpoint of the connection is one and it is "like" a serial interface...
So your limit inside a LAN is the speed supported...
As for the TClientSocket connections to a TServerSocket IN THEORY is about 60000.
When small packets of data are exchanged a PC with recent hardware and a 4 core CPU can serve in XP or newer OS (non server OS) at least 1000 connections without a problem...
Windows Server editions on the other hand can serve more...
The problems arise when the "server" program memory usage is increased when simultaneus connections rise...
THAT is why my first comment was that "All depents..."

George Tokas.
"Father is C++ Builder. I'm C++ Killer"
Vangelis Tokas.
12 years old.
gtokas
BCBJ Editor
BCBJ Editor
 
Posts: 65
Joined: Mon Feb 13, 2006 4:41 pm
Location: Thessaloniki Greece


Return to Technical

Who is online

Users browsing this forum: Yahoo [Bot] and 1 guest