Link to home
Start Free TrialLog in
Avatar of Reme
Reme

asked on

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);
      perror(buffer);
      exit(1);
    }
   
    if (setsockopt(list[i].sockfd,SOL_SOCKET,SO_REUSEADDR,&yes,sizeof(int)) == -1) {
      sprintf(buffer, "setsockopt %d", j);
      perror(buffer);
      exit(1);
    }
   
    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);
      perror(buffer);
      exit(1);
    }
  }  
Avatar of Reme
Reme

ASKER

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
ASKER CERTIFIED SOLUTION
Avatar of akshayxx
akshayxx
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial