Connecting Tech Pros Worldwide Forums | Help | Site Map

socket programming, multiple connections

Jack
Guest
 
Posts: n/a
#1: Mar 28 '07
Hi all
I need some help. I have 10 machines here and I want to write a peer
to peer tcp connection between them. So I have a main server that all
the machine's login to and get each other's IP address. As implied by
'peer to peer' I need these machines to talk to each other directly.
So my question is since in order to create a tcp connection between
two machine one has to write the following, do I need to write this 10
times (assuming one machine is creating the connection to 10 other
machines (including the server), or is there way way that I can
declare one socketChannel and have it connect to multiple IP
addresses. So here is my code for one machine..

struct sockaddr_in socketChannel;
int sd;

socketChannel.sin_family = AF_INET;
socketChannel.sin_port = htons(PORT);
inet_pton(AF_INET, serverIP, &socketChannel.sin_addr);
sd = socket (AF_INET, SOCK_STREAM, 0);

connect (sd, (struct sockaddr*) &socketChannel, sizeof(struct
sockaddr);

I guess I am wondering if there is a more efficient way to do this
rather than running 10 instances of sockaddr_in

Thanks

Default User
Guest
 
Posts: n/a
#2: Mar 28 '07

re: socket programming, multiple connections


Jack wrote:
Quote:
Hi all
I need some help. I have 10 machines here and I want to write a peer
to peer tcp connection between them.
Off-topic here. From a look at your code, I'd say comp.unix.programmer
is the correct place for your question.




Brian
PAolo
Guest
 
Posts: n/a
#3: Mar 28 '07

re: socket programming, multiple connections


On 28 Mar, 19:48, "Jack" <accpac...@hotmail.comwrote:
Quote:
Hi all
I need some help. I have 10 machines here and I want to write a peer
to peer tcp connection between them. So I have a main server that all
the machine's login to and get each other's IP address. As implied by
'peer to peer' I need these machines to talk to each other directly.
So my question is since in order to create a tcp connection between
two machine one has to write the following, do I need to write this 10
times (assuming one machine is creating the connection to 10 other
machines (including the server), or is there way way that I can
declare one socketChannel and have it connect to multiple IP
addresses. So here is my code for one machine..
>
struct sockaddr_in socketChannel;
int sd;
>
socketChannel.sin_family = AF_INET;
socketChannel.sin_port = htons(PORT);
inet_pton(AF_INET, serverIP, &socketChannel.sin_addr);
sd = socket (AF_INET, SOCK_STREAM, 0);
>
connect (sd, (struct sockaddr*) &socketChannel, sizeof(struct
sockaddr);
>
I guess I am wondering if there is a more efficient way to do this
rather than running 10 instances of sockaddr_in
>
Thanks
I think you need to do 10 socket and connects. I think you must have
10 file descriptors, right?
You don't run an instance of sockadr_in, since it is not a class, it
is simply a data structure...

PAolo

Closed Thread