?
Solved

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

Posted on 2003-02-27
5
Medium Priority
?
190 Views
Last Modified: 2010-04-15
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);
    }
  }  
0
Comment
Question by:Reme
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
5 Comments
 

Author Comment

by:Reme
ID: 8035804
Or would i just make the ports different?  I tried that and my error stopped.
0
 
LVL 16

Expert Comment

by:imladris
ID: 8036799
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.
0
 
LVL 8

Expert Comment

by:akshayxx
ID: 8039078
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.
0
 
LVL 8

Expert Comment

by:akshayxx
ID: 8039097
>>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
0
 
LVL 8

Accepted Solution

by:
akshayxx earned 300 total points
ID: 8039196
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.

http://pont.net/socket/prog/mcastServer.c
http://pont.net/socket/prog/mcastClient.c
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.
Suggested Courses

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question