473,405 Members | 2,421 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

socket programming, multiple connections

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

Mar 28 '07 #1
2 5329
Jack wrote:
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
Mar 28 '07 #2
On 28 Mar, 19:48, "Jack" <accpac...@hotmail.comwrote:
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

Mar 28 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Jason | last post by:
After a server accepts a client connection on a certain port, a new socket is created on the server on a system managed dynamic port to handle the connection. Please confirm this. If so, how...
5
by: John Sheppard | last post by:
Hi all, I am not sure that I am posting this in the right group but here it goes anyway. I am new to socket programming and I have been searching on the internet to the questions I am about to pose...
13
by: coloradowebdev | last post by:
i am working on basically a proxy server that handles requests via remoting from clients and executes transactions against a third-party server via TCP. the remoting site works like a champ. my...
6
by: Sharon | last post by:
Hi all. I'm trying first time async socket connection. In all the examples i've seen, the server connection is closed when the message is complete. Is it common to close the connection after...
0
by: Macca | last post by:
Hi, I am writing an asychronous socket server to handle 20+ simulataneous connections. I have used the example in MSDN as a base. The code is shown at end of question. Each connection has a...
2
by: Ben | last post by:
I need to send data from a client to a server. In the server code I have: s = ServerSocket.Accept() If (s.Connected = False) Then MsgBox("Unable to connect", , "Server Error") Exit Sub End...
4
by: Engineerik | last post by:
I am trying to create a socket server which will listen for connections from multiple clients and call subroutines in a Fortran DLL and pass the results back to the client. The asynchronous socket...
11
by: atlaste | last post by:
Hi, In an attempt to create a full-blown webcrawler I've found myself writing a wrapper around the Socket class in an attempt to make it completely async, supporting timeouts and some scheduling...
14
by: =?Utf-8?B?TWlrZVo=?= | last post by:
I have a sync socket application. The client is blocked with Socket.Receive(...) in a thread, another thread calls Socket.Close(). This unblock the blocked thread. But the socket server is still...
0
by: kaps | last post by:
Hi all, After reading for a while I have successfully prepared a server side listening and accepting multiple connections, and a client connecting to server. I need help on loop of send/receive...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.