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

  • SEARCH
  • Home
  • 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 8737585
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T10:33:30+00:00 2026-06-13T10:33:30+00:00

Could someone help identify why my server cannot accept more than one message from

  • 0

Could someone help identify why my server cannot accept more than one message from the client?

I am attempting to have the flow be like the following:
1. Client sends size of message to server
2. Server receives the size and sends a response back. In this case 0.
3. Client checks response and then writes message to server.
4. Server reads message and prints it out.

The problem I am getting is that the accept() at step 4 is never unblocking.

CLIENT

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

int main(int argc, char *argv[])
{
int sock = socket(AF_INET, SOCK_STREAM, 0);

struct sockaddr_in s_address;
s_address.sin_family = AF_INET;
s_address.sin_port = htons(51717);
s_address.sin_addr.s_addr = INADDR_ANY;
if (connect(sock, (struct sockaddr *) &s_address, sizeof(s_address)) < 0) {
    printf("ERROR: Cannot connect()\n");
    exit(0);
}

char *org_msg = "Hello";

printf("Writing size of Hello\n");
char msg1[1];
msg1[0] = sizeof(org_msg);
write(sock, msg1, sizeof(msg1));

printf("Waiting for response from server\n");
struct sockaddr_in c_address;
socklen_t c_length = sizeof(c_address);
int new_sock = accept(sock, (struct sockaddr *) &c_address, &c_length);

printf("Reading response from server\n");
char stat[1];
read(new_sock, stat, 1);

if (atoi(stat) == 0) {
    printf("Writing Hello to server\n");
    write(sock, org_msg, sizeof(org_msg));
}
close(sock);
close(new_sock);
}

SERVER

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

int main(int argc, char *argv[])
{
int sock = socket(AF_INET, SOCK_STREAM, 0);

struct sockaddr_in s_address;
s_address.sin_family = AF_INET;
s_address.sin_port = htons(51717);
s_address.sin_addr.s_addr = INADDR_ANY;
if (bind(sock, (struct sockaddr *) &s_address, sizeof(s_address)) < 0) {
    printf("ERROR: Cannot bind()\n");
    exit(0);
}

listen(sock, 3);

printf("Waiting for client message\n");
struct sockaddr_in c_address;
socklen_t c_length = sizeof(c_address);
int new_sock = accept(sock, (struct sockaddr *) &c_address, &c_length);

printf("Reading client message\n");
char msg[1];
read(new_sock, msg, 1);

printf("Writing response to client\n");
char stat[1];
stat[0] = '0';
write(new_sock, stat, sizeof(stat));

printf("Waiting for client message\n");
int new_sock2 = accept(sock, (struct sockaddr *) &c_address, &c_length);

printf("Reading client message\n");
char msg2[atoi(msg)];
read(new_sock2, msg2, sizeof(msg2));

printf("MESSAGE: %s\n", msg2);

close(sock);
close(new_sock);
close(new_sock2);
}
  • 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-13T10:33:32+00:00Added an answer on June 13, 2026 at 10:33 am

    You should not call accept() on an already-connected socket. Once you have a connected socket in the server (the socket returned by accept()) you should just keep reading and writing that socket until the connection is closed. The steps for the server should be similar to:

    listen_socket = socket(...);
    listen(listen_socket, ...);
    connected_socket = accept(listen_socket, ...);
    read(connected_socket, ...)
    write(connected_socket, ...)
    read(connected_socket, ...)
    write(connected_socket, ...)
    ...
    

    Similarly the client should just keep reading and writing the socket once it has been connected successfully – the steps for the client should be:

    connected_socket = socket(...);
    connect(connected_socket, ...);
    write(connected_socket, ...);
    read(connected_socket, ...);
    write(connected_socket, ...);
    read(connected_socket, ...);
    ...
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Could someone help me on this, I have created simple web services using axis2
HI All I was hoping someone could help me improve a query I have
Could someone help me understand how to preload the images from random.php page so
I was hoping someone could help with a MS Word Macro. Basically, I have
Could someone help me to add A-atype B-btype C-ctype ONLY as a legend to
Could someone help me to write a function that checks if a string is
Could someone help me to write a logic that converts a ID array to
could someone help me on this problem, i want to access facebook API through
could someone help me with the regex pattern that i could use to match
Could someone help me clarify the following conecpts, and the relationship among them ?

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.