LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Networking
User Name
Password
Linux - Networking This forum is for any issue related to networks or networking.
Routing, network cards, OSI, etc. Anything is fair game.

Notices


Reply
  Search this Thread
Old 11-21-2014, 12:05 PM   #1
bill_nimmo
LQ Newbie
 
Registered: Nov 2014
Posts: 20

Rep: Reputation: Disabled
Post c program: server serving multiple clients using sockets


Hello All,

I am working on a client/server system (written in C, using TCP/IP sockets). There is a central server handling all clients. There could literally be 50 to 100 clients out there trying to connect at one time to send data to the server. Each client connects, pushes data, disconnects, waits 5 seconds, then continues the cycle. Currently, the code doesn't seem to be working very well. E.g., I am not getting all of the records that the clients are pushing.

I ran a test with "nc" (from a client box, to my server, with my port number). In many cases nc took 2 to 50 minutes just to connect and send data. That would seem to indicate that the server was busy and had a bunch of requests queued up. Yes/no?

In reading up (Stevens book, web searches), I saw there are different methods for servers serving up multiple clients. I was hoping to get some of you knowledgeable folks to direct me to some examples.

I have seen some cases where the server executes 'accept' then forks off a process to do the actual work (read the socket, process, etc). This method seems like something I might want to pursue.

I have seen other cases where some suggest using 'threading'. But I have also seen some discounting that method since you are using shared memory and that could lead to bugs/leaks.

I am open to any suggestions. I didn't see a good 'fork' example in the Stevens book or a good 'thread' example.

I appreciate all help. Thank you.

Bill
 
Old 11-21-2014, 10:09 PM   #2
linosaurusroot
Member
 
Registered: Oct 2012
Distribution: OpenSuSE,RHEL,Fedora,OpenBSD
Posts: 982
Blog Entries: 2

Rep: Reputation: 244Reputation: 244Reputation: 244
http://lmgtfy.com?q=socket+bind+listen+accept+fork

http://www.tutorialspoint.com/unix_s...er_example.htm
 
Old 11-22-2014, 08:08 AM   #3
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,573

Rep: Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142
Here's an example with fork:
http://beej.us/guide/bgnet/output/ht...entserver.html
 
Old 11-24-2014, 04:12 PM   #4
bill_nimmo
LQ Newbie
 
Registered: Nov 2014
Posts: 20

Original Poster
Rep: Reputation: Disabled
Question select?

Thank you linosaurusroot and suicidalegroll for your responses.

I am still trying to come to grips with the code that I inherited. I noticed that in between the listen and accept they stuffed in a 'select'. If I am a single server accepting connections from multiple clients what purpose does having the 'select' serve me? I don't see that they are doing much with the select in the inherited code. All examples I have seen (some that you have pointed me to) don't have use a select.

Any ideas?

thank you,
Bill
 
Old 11-24-2014, 06:08 PM   #5
jpollard
Senior Member
 
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 4,912

Rep: Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513
Depends on the example.

The select puts the process to sleep while waiting for input from one or more file descriptors.

The advantage this has is that you can have a single process (one descriptor is listening). When any connection is made the select returns which descriptors have data for processing.

If the descriptor is the listening socket, then it can accept the connection, and add the new descriptor to the list of descriptors that may have data. If it is a data descriptor, then it can process it, returning any other data.

In either case, when the select returns, all descriptors that have data should be processed.

The problem with this is that all processing is sequential - thus two data channels will not be processed in parallel - one will be waiting until the other is completed (though not necessarily disconnected - just the data from the socket is read, processed, and any return data sent). This makes response appear slow if there are very many connections.

The advantage of the forking (or thread) server is that each connection is handled by an independent process (or thread). Thus much more responsive as the separate connections are handled in parallel. The usual problem with a forking/threaded server is the processing of the data. If the application is a database (for instance) then appropriate locking, transactions, and other synchronization has to be done... otherwise the data would get corrupted. Now, this is less of an issue if the application is just a message exchange, though the "processed" data returned to the connection may have to support queuing data for return so that the messages sent to the user (within limits - if it remains below a certain size, a sendmsg can be atomic (I think this would be much easier when it is threaded).
 
Old 11-25-2014, 05:35 AM   #6
linosaurusroot
Member
 
Registered: Oct 2012
Distribution: OpenSuSE,RHEL,Fedora,OpenBSD
Posts: 982
Blog Entries: 2

Rep: Reputation: 244Reputation: 244Reputation: 244
Quote:
Originally Posted by jpollard View Post
Depends on the example.
select is also useful for timing ... if you want a bot in an IRC channel giving you alarms before the expiry of some timer (e.g. while playing an online game) you can't leave the bot blocked until it next sees traffic.

And the fork model (if the server processes are independent) is simple and you can verifiy there are no resource leaks when the short-lived processes exit.
 
Old 12-01-2014, 08:22 AM   #7
bill_nimmo
LQ Newbie
 
Registered: Nov 2014
Posts: 20

Original Poster
Rep: Reputation: Disabled
Thank you

Thank you for your responses. I very much appreciate it. The biggest issue with my server application is the number of clients concurrently trying to connect. So far, in tests, the forking method seems to have cleared up that issue. I see where select can be helpful too, but if I am reading your responses correctly the select may not be needed here.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
One server many client chat program using sockets lekshminair Linux - Newbie 6 09-11-2011 06:47 AM
Creating multiple UDP sockets in a 'C' program saikrishnan7 Programming 20 05-01-2009 04:08 AM
Program to forward tcp sockets to unix domain sockets mikepol Linux - Networking 0 09-27-2007 09:49 AM
Sockets: multiple send() calls throttle server framerate. JCipriani Programming 3 09-22-2005 07:06 PM
serving multiple domain websites on one server? gsgleason Linux - Software 1 06-03-2005 10:50 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Networking

All times are GMT -5. The time now is 07:22 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration