UNIX Socket FAQ

A forum for questions and answers about network programming on Linux and all other Unix-like systems

You are not logged in.

#1 2012-05-31 07:11 AM

renju
Member
Registered: 2012-05-31
Posts: 9

Socket

Hi,

    I want to develop multiple server and single client application. I created  sockets for the client and servers. While one server and client is communicated though their assigned socket, whether its possible for the client to use the same socket to communicate with another server?

Offline

#2 2012-05-31 09:13 AM

i3839
Oddministrator
From: Amsterdam
Registered: 2003-06-07
Posts: 2,239

Re: Socket

Hi Renju,

No, you can't use a TCP socket for multiple connections.
You can't even re-use a disconnected socket, you have to
create a new one for every connection.

With UDP you can use one socket for everything because
UDP is connectionless.

Offline

#3 2012-06-01 06:49 AM

renju
Member
Registered: 2012-05-31
Posts: 9

Re: Socket

Hi

     Thanks for your valuable reply. That means its not possible to use same socket in the client side to communicate with all the servers. isn't it? For that its need to create unique sockets for each connection. what will the maximum number of connection( max no of sockets) that we can create in the client application? is this unlimited or according to the port availability?

                                                                                                                                                       Thanks

Offline

#4 2012-06-01 12:47 PM

i3839
Oddministrator
From: Amsterdam
Registered: 2003-06-07
Posts: 2,239

Re: Socket

Indeed, you need one socket for each connection. The tricky bit of network
programming is handling many sockets. Broadly speaking there are two
approaches:

- Handle multiple sockets with one thread or process, by using non-blocking
  sockets and multiplexing between them with select/poll/epol/kqueue.

- Handle one socket per thread or process, by using multiple threads or
  processes, one per socket.

Combinations of the above are also possible of course.

The hard limit is ulimit -n, the max number of open file descriptors.
If you use the second approach, you have to watch out for ulimit -u too,
the max number of processes. There may be other limits elsewhere too,
depending on your OS, system or even crappy NAT router, which often
can't handle many open connections.

You don't have to worry about port availability except if you make many
connections to the same remote host and port. This is because as TCP is
connection based every connections is uniquely identified by also the
remote address and port, in addition to the local port number.

If you would open many UDP sockets then you would have to worry about
the limited number of local ports, but as said before, with UDP you can
just use one socket anyway.

Offline

#5 2012-06-01 01:20 PM

RobSeace
Administrator
From: Boston, MA
Registered: 2002-06-12
Posts: 3,839
Website

Re: Socket

Offline

#6 2012-06-01 03:04 PM

i3839
Oddministrator
From: Amsterdam
Registered: 2003-06-07
Posts: 2,239

Re: Socket

Damn, really? I assumed it would work both ways and that they fixed
the limit for client sockets too, considering the kernel needs to keep
track of connections anyway and having a port limit would only add
extra, unnecessary restrictions...

So big oops and apology from my side for spreading misinformation.

Offline

#7 2012-06-01 09:37 PM

RobSeace
Administrator
From: Boston, MA
Registered: 2002-06-12
Posts: 3,839
Website

Re: Socket

Offline

#8 2012-06-04 05:50 AM

renju
Member
Registered: 2012-05-31
Posts: 9

Re: Socket

hi,
   
    Thanks for the replies.So what will be exact things need to follow for my application(Single client and multiple servers. client need to connect all the servers at same time)?

Offline

#9 2012-06-04 12:27 PM

RobSeace
Administrator
From: Boston, MA
Registered: 2002-06-12
Posts: 3,839
Website

Re: Socket

I'm not sure what you mean exactly...  But, i3839 already outlined different approaches to handling multiple sockets...  Which one you should use depends entirely on your particular needs, which we have no idea about...  If interaction with each server is separate and distinct from all others, then perhaps one socket per process/thread is ideal...  If data needs to be shared between each server, then you may want to handle them all in a single process/thread...

Offline

#10 2012-06-07 05:32 AM

renju
Member
Registered: 2012-05-31
Posts: 9

Re: Socket

hi,

     whether its possible to use asynchronous socket( using select()) to identify the socket set(using unique socket for each connection with the servers) in my client side? or there any better method available?

                                                                                                                                                                                                                      Thanks

Offline

#11 2012-06-07 12:23 PM

RobSeace
Administrator
From: Boston, MA
Registered: 2002-06-12
Posts: 3,839
Website

Re: Socket

Yes, of course it's possible to use select()...  Why wouldn't it be?  As for better methods, there's poll(), or if on Linux epoll*() ("man 7 epoll" for an overview)...

Offline

#12 2012-06-08 05:52 AM

renju
Member
Registered: 2012-05-31
Posts: 9

Re: Socket

i told like because i cant find any client code using select() socket. most of the tutorial are only dealt with server side asynchronous select() method.
It will be helpful if you share some tutorial links which describe client side asynchronous sockets. thank you.

Offline

#13 2012-06-08 08:34 AM

i3839
Oddministrator
From: Amsterdam
Registered: 2003-06-07
Posts: 2,239

Re: Socket

The client code is exactly the same as the server code, the only difference
is who calls connect() and who calls accept(), but otherwise the principle
is the same. So use asynchronous multiplexing server code as a guideline.

Offline

#14 2012-06-08 12:18 PM

RobSeace
Administrator
From: Boston, MA
Registered: 2002-06-12
Posts: 3,839
Website

Re: Socket

Yes, the main difference from a server's select() usage will be that there's no listening socket to worry about and do accept() on and then add another new socket to the fd_sets...  Presumably these clients will all be connecting to the multiple servers up front?  So, there will always be the same set of sockets being worked with...  (Though, you probably want to handle I/O failures on any socket by trying to reconnect, which may require you to tweak the fd_sets...)

As for specific client examples/tutorials, I can't really think of any...  It's not really a very common situation to be connecting to multiple servers simultaneously...  Or, if you do, you often want them handled separately, so you give each a dedicated process/thread to deal with it...  But, the basics are all the same whether you're a client or a server, so any select() (or poll()/epoll*()/etc.) examples you find should be applicable...

Offline

#15 2012-06-18 10:57 AM

renju
Member
Registered: 2012-05-31
Posts: 9

Re: Socket

hi,
    I developed my application using select(). Set the readset and writeset using FD_SET .  while debugging its show both readset and writeset count as 1.
But after calling select() the readset is changed to 0 and writeset remains as 1. why this readset count changes?

Offline

#16 2012-06-18 11:43 AM

RobSeace
Administrator
From: Boston, MA
Registered: 2002-06-12
Posts: 3,839
Website

Re: Socket

That means the socket is not currently readable; ie., there's no data to read at that time...  In general, it's utterly pointless to select() on sockets for writability, since they're almost always writable, unless you manage to completely fill the send queue...  So, you probably only want to be using the read fd_set, anyway...

But, that's how select() works: it modifies the fd_sets you pass in to identify the sockets which are currently readable (or writable, or have OOB data, if using the other fd_sets) when it returns a value greater than zero...  Since you're using the write fd_set as well, it's just returning immediately, because the socket is always writable...  But, if you got rid of that and only selected for readability, then select() would block until some data was available to be read, and the read fd_set would identify which sockets were readable...

Offline

#17 2012-06-19 08:35 AM

renju
Member
Registered: 2012-05-31
Posts: 9

Re: Socket

hi thanks,

           Is libevent can be used as a replacement for select()?. I read that libevent can be used for asynchronous socket and its better that select(). but i didn't get further tutorial regarding libevent while searching!!

Offline

#18 2012-06-19 01:33 PM

RobSeace
Administrator
From: Boston, MA
Registered: 2002-06-12
Posts: 3,839
Website

Re: Socket

Offline

#19 2012-06-19 01:38 PM

renju
Member
Registered: 2012-05-31
Posts: 9

Re: Socket

thanks. can we use poll() in Windows platform?

Last edited by renju (2012-06-19 01:39 PM)

Offline

#20 2012-06-19 01:50 PM

RobSeace
Administrator
From: Boston, MA
Registered: 2002-06-12
Posts: 3,839
Website

Re: Socket

I really don't know...  I don't work with or use Windows in any way, and haven't in almost 20 years...  I'm strictly a Unix/Linux person...  But, based on what I've heard in the past, I was under the impression that Windows only supported select() and not poll()...  (And, further, its select() was horribly broken, in that it only works on sockets, and not normal file descriptors...  In fact, the entire Windows socket implementation appears to be horribly broken, in that sockets are not actually file descriptors themselves, but some bizarre custom type...)

Offline

#21 2012-06-20 06:04 AM

renju
Member
Registered: 2012-05-31
Posts: 9

Re: Socket

hi ,

     what are the methods available for using asynchronous socket communication(other than selec,poll :) ).?
     which one is the best method that can be used based on connections(max connection)?
     which one is the best which is based on portability( linux and windows)?


I tried to use select() and poll() for my application. but poll is only working with linux and select is for windows. But there is a method WSAPoll() which can be used in windows. but its minimum requirement client is windows vista.

i also heard about IOCompletion port. Is it a good one when compare with select()?



                                                                                                                                          thanks

Last edited by renju (2012-06-20 06:08 AM)

Offline

#22 2012-06-20 12:46 PM

RobSeace
Administrator
From: Boston, MA
Registered: 2002-06-12
Posts: 3,839
Website

Re: Socket

Offline

#23 2012-06-22 12:34 AM

RipRage
Member
From: England
Registered: 2010-01-06
Posts: 146

Re: Socket

Offline

#24 2012-06-22 12:37 PM

RobSeace
Administrator
From: Boston, MA
Registered: 2002-06-12
Posts: 3,839
Website

Re: Socket

Offline

#25 2012-06-22 05:29 PM

RipRage
Member
From: England
Registered: 2010-01-06
Posts: 146

Re: Socket

Offline

Board footer

Powered by FluxBB