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 131303
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T06:05:03+00:00 2026-05-11T06:05:03+00:00

I am using Berkeley sockets (both: Internet domain and Unix domain) and I was

  • 0

I am using Berkeley sockets (both: Internet domain and Unix domain) and I was wondering if the server can use the same sockets for reading the request and writing a response to the client. Or should the client create an other socket to wait for the replay and the server connect to it after processing the message received.

By the way, I am talking about connection oriented sockets (stream sockets, TCP, …).

This is the simplified server code (I ommit error checking on system calls here just for simplicity):

int main() {      int server_socket, connected_socket;     struct sockaddr_in server_addr;     char buf[1024];     char aux[256];     int bytes_read;      server_socket = socket(AF_INET, SOCK_STREAM, 0);          server_addr.sin_family = AF_INET;     server_addr.sin_addr.s_addr = INADDR_ANY;     server_addr.sin_port = htons(1234);     bind(server_socket, &server_addr, sizeof(server_addr))      listen(server_socket, 5)      connected_sodket = accept(server_socket, 0, 0);     do {         bzero(buf, sizeof(buf));         bytes_read = read(connected_socket, buf, sizeof(buf));             } while (bytes_read > 0);                /* Here I want to use connected_socket to write the reply, can I? */      close(connected_socket);             close(server_socket);      return (EXIT_SUCCESS); } 

And this is the simplified client code (I ommit error checking on system calls here just for simplicity):

int main() {      int client_socket;     struct sockaddr_in server_addr;      client_socket = socket(AF_INET, SOCK_STREAM, 0);      hp = gethostbyname('myhost');     server_addr.sin_family = AF_INET;     memcpy(&server_addr.sin_addr, hp->h_addr_list[0], hp->h_length);     server_addr.sin_port = htons(1234);      connect(client_socket, &server_addr, sizeof(server_addr));      write(client_socket, MSG, sizeof(MSG));      /* Here I want to wait for a response from the server using client_socket, can I? */      close(client_socket);      return (EXIT_SUCCESS); } 

Can I use connected_socket in the server and client_socket in the client to pass a response message back? Or should I use the client address I get in the server when in ‘accept’ to connect to a socket in the client?

I have tried by using read/wrint in the client/server where the comment is shown but that way both programs keep blocked, it seems to be a dead-lock.

Thanks ins advance! Regards.

  • 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. 2026-05-11T06:05:03+00:00Added an answer on May 11, 2026 at 6:05 am

    You can use the same socket BUT your program is set up to have the server read EVERYTHING the client sends before attempting to reply. So the loop in the server won’t complete until the client closes the write side of its socket so the server gets an EOF (0 bytes read), and thus the server will never send back its response.

    There are a couple of ways you can deal with this.

    1. You can break the loop in the server after its seen the whole request, rather than reading until EOF. This requires that the data sent by the client be self-delimiting somehow, so the server can know when its read it all.
    2. You can use a second connection for the reply. Probably not the best.
    3. You can use asymmetric shutdown of the socket. Have the client do shutdown(client_socket, SHUT_WR) to half-close the socket. The server will then see the EOF (and the loop will finish), but the other direction on the socket will still be open for the reply.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 236k
  • Answers 236k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Why don't you change your production.rb config to match the… May 13, 2026 at 6:28 am
  • Editorial Team
    Editorial Team added an answer Found the problem. We had a script in the parent… May 13, 2026 at 6:28 am
  • Editorial Team
    Editorial Team added an answer You can do that using regular expression, and more precisely… May 13, 2026 at 6:28 am

Related Questions

I am writing a simple app with 24 items in a hash to be
I enjoy developing algorithms using the STL, however, I have this recurring problem where
I am using SQL Server 2005. I have a table with a text column
I am using LINQ to query a generic dictionary and then use the result

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.