• C

Communication between processes via sockets Problem.......

I am trying to create a multi-process program that communicates between processes using sockets.  How would i bind all the sockets to the same ip?  Here is what I have for my code, it is far from complete.  Should I create the socket after I create the process or does order matter?  This code gives me a binding error.  Thanks for any input.

  for(j = 0; j < amount; j++) {
    if ((list[i].sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
      sprintf(buffer, "socket %d", j);
    if (setsockopt(list[i].sockfd,SOL_SOCKET,SO_REUSEADDR,&yes,sizeof(int)) == -1) {
      sprintf(buffer, "setsockopt %d", j);
    my_addr.sin_family = AF_INET;         // host byte order
    my_addr.sin_port = htons(MYPORT);     // short, network byte order
    my_addr.sin_addr.s_addr = INADDR_ANY; // automatically fill with my IP
    memset(&(my_addr.sin_zero), '\0', 8); // zero the rest of the struct
    if (bind(list[i].sockfd, (struct sockaddr *)&my_addr, sizeof(struct sockaddr)) == -1) {
      sprintf(buffer, "bind %d", j);
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

RemeAuthor Commented:
Or would i just make the ports different?  I tried that and my error stopped.
Only one process can be listening at a particular port. (If there were more than one, there would ambiguity as to which proces would get information arriving at that port. All of them? (would the sender get multiple responses then?), a random one? in sequence?).

If a second process tries to listen at that port, it will get a binding error.
if all of ur processes needs to have common port/address of exchanging the information .. then use multicast addresses

there wont be much changes .. but here is the way to do this..
multicast addresses are a range of ip addrsses  which allow multicast messages..
in that every one of ur processes will connect to that multicast address..(IP + port)
then in each process u can add a read thread that will continuosly wait for messages on that socket
in the multicast system .. if one some one write message on a socket .. then it is received by all the 'peers' connected to that multicst address.. including the sender
so extra thing u need to implement will be ..the message structure  that will have an identification key..to know who sent the message .. and if u wish u can add the intended recepient's id . so that each one receiving the message will look up that field and see if its id is there .. if not drop the message .. if yes.. you know what to do..

tell me if this scenarios suits you ..
otherwise no option but to have server client concept ..
the good thing about multicasting is ..no server client concept involved.
>>How would i bind all the sockets to the same ip?  
if this is the thing bother you and others u have solved...then if IP is same .. but  u can always have different ports for different processes.
but in this scenario .. whenever one process has to send some information to other process.. then it must have some look up table of the particular process.. to store the corresponding port number
so in this scenario each process will have one listener on private port .. and others will use that to talk to 'him'

other wise to have common port of communication .. i would highly recommend multicasting
have a look at these samples..
they have made two files separate for client and server .. actually in ur case both will be part of ur each process.. Server.c is the read thread part..  
joining the multicast group will be done only once in the program.


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.