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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T05:09:11+00:00 2026-05-29T05:09:11+00:00

I am learning the Sockets networking API. In this process, I have written a

  • 0

I am learning the Sockets networking API. In this process, I have written a simple Echo server that uses TCP. I wrote the code in such a way that, as long as the server is running, anything typed on the client’s console should be echoed back to it. However, I am unable to achieve this. Although, for the first input, I get the echo, from next time onwards, I do not get any message.
I know, we can implement it to run for many clients using fork(), but I want to know the reason behind the blocking of the client, and if possible ways to correct it.
Here is the code for the client:

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

#define MAXCOUNT 1024

int main(int argc, char* argv[])
{
    int sfd;
    char msg[MAXCOUNT];
    char blanmsg[MAXCOUNT];
    struct sockaddr_in saddr;

    memset(&saddr,0,sizeof(saddr)); 
    sfd = socket(AF_INET,SOCK_STREAM,0);
    saddr.sin_family = AF_INET;
    inet_pton(AF_INET,"127.0.0.1",&saddr.sin_addr);
    saddr.sin_port = htons(5004);

    connect(sfd,(struct sockaddr*) &saddr, sizeof(saddr));
    for(; ;) {
        memset(msg,0,MAXCOUNT);
        memset(blanmsg,0,MAXCOUNT);
        fgets(msg,MAXCOUNT,stdin);
        send(sfd,msg,strlen(msg),0);
        recv(sfd,blanmsg,sizeof(blanmsg),0);
        printf("%s",blanmsg);
        fflush(stdout);
    }
    exit(0);
}   

Here is the code for the server:

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

#define MAXCOUNT 1024

int main(int argc, char* argv[])
{
    int sfd,nsfd,n,i,cn;
    char buf[MAXCOUNT];
    socklen_t caddrlen;
    struct sockaddr_in caddr,saddr; //Structs for Client and server Address in the Internet 

    sfd = socket(AF_INET,SOCK_STREAM,0);
    memset(&saddr,0,sizeof(saddr)); //Clear the Server address structure

    saddr.sin_family = AF_INET; //Internet Address Family
    saddr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
    saddr.sin_port = htons(5004);

    bind(sfd, (struct sockaddr*) &saddr,sizeof(saddr));
    listen(sfd,1);

    for(; ;) {
        caddrlen = sizeof(caddr);
        nsfd = accept(sfd,(struct sockaddr*) &caddr,&caddrlen);
        cn = recv(nsfd,buf,sizeof(buf),0);
        if(cn == 0) {
            exit(0);
        }
        buf[cn] = '\0';
        send(nsfd,buf,strlen(buf),0);
        }
    close(nsfd);
    exit(0);
}       
  • 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-05-29T05:09:12+00:00Added an answer on May 29, 2026 at 5:09 am

    You shouldn’t call accept within the loop on the server. Move the accept before the for loop on the server and it should work how you expect.

    Calling accept in the loop like that will make the server block until a new connection comes in. Your client is only opening a single connection, so the server will block on the second call to accept.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm learning socket programming in C & downloaded a simple tcp server source file.
I am learning socket programming in c, I wrote this simple program to accept
I'm learning to use raw sockets, and im trying to prase out the tcp
Learning java server technologies, trying to clarify some things. There are few technologies that
I am using Csharp tcp sockets to send data between a client and server.
For learning purposes I am writing a socket based client/server application. I have designed
For learning purposes I am writing a socket based client/server application. I have designed
I have never written any IPC C++ on Linux before. My problem is that
I recently started learning networking with sockets in java. So i've created a multiplayer
I have simple python server and client. Server: import SocketServer import threading class MyTCPHandler(SocketServer.BaseRequestHandler):

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.