socket programming, multiple connections

J

Jack

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
 
D

Default User

Jack said:
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
 
P

PAolo

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

Forum statistics

Threads
473,982
Messages
2,570,189
Members
46,735
Latest member
HikmatRamazanov

Latest Threads

Top