Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium


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

Posted on 2003-02-27
Medium Priority
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);
    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);
Question by:Reme
  • 3

Author Comment

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

Expert Comment

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.

Expert Comment

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.

Expert Comment

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

Accepted Solution

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.


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

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.

577 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