Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7984585
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T11:19:24+00:00 2026-06-04T11:19:24+00:00

I have written the following code as an intermediate to connect two programs. There

  • 0

I have written the following code as an intermediate to connect two programs. There is a server program running and a client program on two different systems. This code is expected to act as an intermediate between these two programs.

#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <netdb.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>

//Connect with program on server side
char * serv_con(char app_data[50])
{
int sock, bytes_recieved;  
char send_data[1024];
char *recv_data = malloc(1024);
struct hostent *host;
struct sockaddr_in server_addr;  
host = gethostbyname("10.47.3.249");
if ((sock = socket(AF_INET, SOCK_STREAM, 0)) == -1)
{

    perror("Socket");
    exit(1);

}
server_addr.sin_family = AF_INET;     
server_addr.sin_port = htons(3128);   
server_addr.sin_addr = *((struct in_addr *)host->h_addr);
bzero(&(server_addr.sin_zero),8); 
if (connect(sock, (struct sockaddr *)&server_addr, sizeof(struct sockaddr)) == -1) 
{

    perror("Connect");
    exit(1);

}
bytes_recieved=recv(sock,recv_data,1024,0);
recv_data[bytes_recieved] = '\0';
send(sock, app_data, 50, 0);
return recv_data;
//close(sock);

}


//Connect with client app
char * cli_con(char ser_data[50])
{
int sock, connected, bytes_recieved , true = 1;  
char send_data [1024];
char *recv_data = malloc(1024);       
struct sockaddr_in server_addr,client_addr;    
int sin_size;
if ((sock = socket(AF_INET, SOCK_STREAM, 0)) == -1)
{

    perror("Socket");
    exit(1);

}
if (setsockopt(sock,SOL_SOCKET,SO_REUSEADDR,&true,sizeof(int)) == -1)
{

    perror("Setsockopt");
    exit(1);

}
server_addr.sin_family = AF_INET;         
server_addr.sin_port = htons(5000);     
server_addr.sin_addr.s_addr = INADDR_ANY; 
bzero(&(server_addr.sin_zero),8); 
if (bind(sock, (struct sockaddr *)&server_addr, sizeof(struct sockaddr)) == -1)
{

    perror("Unable to bind1");
    exit(1);

}
if (listen(sock, 5) == -1)
{

    perror("Listen");
    exit(1);

}
sin_size = sizeof(struct sockaddr_in);
connected = accept(sock, (struct sockaddr *)&client_addr,&sin_size1);
printf("\n I got a connection from (%s , %d)",inet_ntoa(client_addr.sin_addr),ntohs(client_addr.sin_port));
bytes_recieved = recv(connected,recv_data,1024,0);
recv_data[bytes_recieved] = '\0';   
send(connected, ser_data,50, 0);
//close(sock);

}


int main()
{

char *ser_data, *app_data;
int pid = fork();
while(1)
{

    if(pid == 0)
        app_data = serv_con(ser_data);

    else
        ser_data = cli_con(app_data);

}

}

It works fine until the client side app runs. But as soon as the client side app runs, the code exit giving the error:

Unable to bind: Address already in use
I got a connection from (192.168.0.3 , 45691)

What modification should I make in the code to rectify this error? I am working on linux. Thanks in advance.

EDIT:
I have removved the comment from close(sock)and added close(connect) in the function cli_con. The code on the client side is given below:

int sock, bytes_recieved;  
char send_data[1024],recv_data[1024];
struct hostent *host;
struct sockaddr_in server_addr;  
host = gethostbyname("192.168.0.2");
if ((sock = socket(AF_INET, SOCK_STREAM, 0)) == -1)
{
    perror("Socket");
    exit(1);
}
server_addr.sin_family = AF_INET;     
server_addr.sin_port = htons(5555);   
server_addr.sin_addr = *((struct in_addr *)host->h_addr);
bzero(&(server_addr.sin_zero),8); 
if (connect(sock, (struct sockaddr *)&server_addr,
sizeof(struct sockaddr)) == -1) 
{
    perror("Connect");
    exit(1);
}
while(1)
{
    //necessary codes
    if (connect(sock, (struct sockaddr *)&server_addr, sizeof(struct sockaddr)) == -1) 
    {
        close(sock);
        goto connct;
    }
}

But now on running, the first program doesnot exit but doesnot even print

I got a connection from (192.168.0.3 , 45691)

But just keeps on running without printing ANY messages. But on the other hand, the client exits showing the error:

Connect: Connection reset by peer

What should I do now?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T11:19:26+00:00Added an answer on June 4, 2026 at 11:19 am

    When a client disconnects you create a new server socket and bind it to the same port. If the server side socket was not closed the port is still in use, so bind fails.

    Usually the server side of a socket program has a loop around accept to allow it process connections from many clients. This way bind and listen are called only once.

    while (connected = accept(sock, (struct sockaddr *)&client_addr,&sin_size1)) {
        printf("\n I got a connection from (%s , %d)",inet_ntoa(client_addr.sin_addr),ntohs(client_addr.sin_port));
        bytes_recieved = recv(connected,recv_data,1024,0);
        recv_data[bytes_recieved] = '\0';   
        send(connected, ser_data,50, 0);
        close(connected);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i have written the following code: As you can see there is a for
I have written following PHP code: $input=menu=1&type=0&; print $input.<hr>.ereg_replace('/&/', ':::', $input); After running above
I have written following code for the client of RMI. But getting java.rmi.ConnectException: Connection
I have written the following code to place the image path into sql server
I have written following code to attach gesture recogniser to multiple imageviews. [imageview1 setUserInteractionEnabled:YES];
I am trying my hands on WPF MVVM. I have written following code in
I have written a following code to get just the file name without extension
I have written the following code choice /m Do you want to add another
I have written the following code. But it is removing only &nbsp; not <br>
I have written the following code in Perl. I want to iterate through a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.