How to implement multiple sockets per client?

Pupeno pupeno@REDACTED
Sat Aug 26 02:50:24 CEST 2006


On Friday 25 August 2006 22:31, Jeff Crane wrote:
> I want to have a client connect to my barebones server
> on 2 sockets. How are you supposed to ensure that a
> second connection is from the same client?

You can check for the IP.

> Is there a way to spawn 2 sockets from an accept?

Now, you listen and accept incoming connections. On accept launch a new thread 
and you can accept as many connections (creating as many sockets) as you 
want. You'll have to limit it to two of course. I'd code this as two 
gen_servers, one the launcher, listening for incoming connections and keeping 
track of who connected and how many times and then another to handle the 
connection itself (hint: there's a function to reassign the ownership of the 
socket to a new process).

> Do I have to listen then initiate to do this
> correctly?

Have you coded a simple server first ? I'd recommend to star there.

> The 2 sockets would be one for reading (input messages
> from client) and one for sending (output messages to client).

You can do reading and witting on the same socket, on the same connection. It 
has been that way for what ? 20 years ? 30 years ?

-- 
Pupeno <pupeno@REDACTED> (http://pupeno.com)
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 198 bytes
Desc: not available
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20060826/b66e32fa/attachment.bin>


More information about the erlang-questions mailing list