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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T01:08:14+00:00 2026-06-04T01:08:14+00:00

I am trying out with a TCP server/client program, and wonder why at server

  • 0

I am trying out with a TCP server/client program, and wonder why at server side, the program loop at accept() ….

My idea is to develop a TCP server which receive 1 request at a time from multiple clients. The connection between server and client shall close upon completing single operation. For eg
1) The TCP server shall be hearing port 30000 at all time.
2) TCP client 1 connect to TCP server, send “hello”, and wait for “hello” feedback from server, and close the connection.
3) TCP client 2 connect to TCP server, send “hello”, and wait for “hello” feedback from server, and close the connection.
and so on until TCP client n….

All of the operation above is executed one at a time, meaning TCP client 1 and 2 will not send message to TCP server at the same time, and, once TCP client 1 complete the operation it will not connect to server anymore (if necessary, a new connection can be triggered).

the output shows

======== NW Test =======
1) Start (T)CP Server
2) Start T(C)P  Client
3) (S)end TCP Message
4) Close TCP S(O)cket
0) (Q)uit
================================
Option: t
Starting TCP server...
create socket success.
bind socket success.
listen socket success.

I am expecting also…

accept socket success.
pthread_create success.
TCP server started, listening socket [tcpsocket]

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <pthread.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <netinet/in.h>

struct attr {
  int tcpsocket;
};

static int nwMenu();
static void *event_start(void* param);

int tcpserver(){
    int thread_id;
    int tcpSocket;
    int listenSocket;
int nRet;
    socklen_t nLen;        
struct attr atr;
    struct sockaddr_in saTCPServer, saTCPClient;

fprintf(stderr, "Starting TCP server...\n");

    listenSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
    if (listenSocket < 0)
    {
  fprintf(stderr, "fail to create socket.\n");
      return -1;
    } else {
  fprintf(stderr, "create socket success.\n");
}

    saTCPServer.sin_family = AF_INET;
    saTCPServer.sin_addr.s_addr = INADDR_ANY;
    saTCPServer.sin_port = 30000;

    nRet = bind(listenSocket, (struct sockaddr *) &saTCPServer, sizeof(struct sockaddr));
    if (nRet < 0)
    {
  fprintf(stderr, "fail to bind socket.\n");
      close (listenSocket);
      return -1;
    } else {
  fprintf(stderr, "bind socket success.\n");
}

    if (listen(listenSocket, 5) < 0) {
   fprintf(stderr, "fail to listen socket.\n"); 
       close (listenSocket);
       return -1;
    } else {
  fprintf(stderr, "listen socket success.\n");
}

    nLen = sizeof(saTCPClient);

    tcpSocket = accept(listenSocket, (struct sockaddr *) &saTCPClient,  &nLen);
    if (tcpSocket < 0)
    {      
  fprintf(stderr, "fail to accept socket.\n");
      close (listenSocket);
      return -1;      
    }  else {
  fprintf(stderr, "accept socket success.\n");
}
atr.tcpsocket = tcpSocket;

    close (listenSocket); 

    if(pthread_create(&thread_id, NULL, (void*) event_start, (void*) &atr) != 0){
    fprintf(stderr, "pthread_create failed.\n");
} else {
  fprintf(stderr, "pthread_create success.\n");
};

fprintf(stderr, "TCP server started, listening socket %d\n", tcpSocket);

return 0;

}

static void *event_start(void* param) {

    int quit = 0;
    int nRet = 0;
int tcpSocket;
    char szBuf[4096];

struct attr *p_atr = (struct attr* )param;
tcpSocket = p_atr->tcpsocket;

    while (!quit){

        nRet = recv(tcpSocket, szBuf, sizeof(szBuf), 0);
        if (nRet <= 0 ) // peer closed
         {
            fprintf(stderr, "receiver quit recv\n");
        quit = 1;
        break;
          }

        fprintf(stderr, "recv: %s\n", szBuf);

        send(tcpSocket, szBuf, sizeof(szBuf), 0);
    }

     close(tcpSocket);
}


int tcpclient(){
    int tcpSocket;
    struct sockaddr_in saTCPServer;

    bzero((void *) &saTCPServer, sizeof(saTCPServer));

    char* szServer = "127.0.0.1";
    int nPort = 30000;

    inet_aton(szServer, &saTCPServer.sin_addr);

    tcpSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); 
    if (tcpSocket < 0){
        return -1;
    } else {
        fprintf(stderr, "client tcp socket: %d\n", tcpSocket);
    }

    saTCPServer.sin_family = AF_INET;
    saTCPServer.sin_port = htons(nPort);

    if (connect(tcpSocket, (struct sockaddr *) &saTCPServer, sizeof(saTCPServer)) < 0 )     {
        return -1;
    }

    fprintf(stderr, "TCP client started, listening socket %d\n", tcpSocket);

    return 0;
}

int send_tcp_msg(int tcpSocket){
    int nRet;
    char szBuf[4096];

    memset(szBuf, 0x00, sizeof(szBuf));

    send(tcpSocket, "hello", sizeof("hello"), 0);

    nRet = recv(tcpSocket, szBuf, sizeof(szBuf), 0);

    fprintf(stderr, "recv: %s\n", szBuf);

return 0;
}

int close_tcp(int tcpSocket){
    close(tcpSocket);

return 0;
}    

static int nwMenu(){
    int ret = 0;
    int socket;
    char buff[1024];

    do{
    fprintf(stderr, "======== NW Test =======\n");
    fprintf(stderr, "1) Start (T)CP Server\n");
    fprintf(stderr, "2) Start T(C)P  Client\n");
    fprintf(stderr, "3) (S)end TCP Message\n");
    fprintf(stderr, "4) Close TCP S(O)cket\n");     
    fprintf(stderr, "0) (Q)uit\n"); 
    fprintf(stderr, "================================\n");
    fprintf(stderr, "Option: ");
    memset(buff, 0x00, sizeof(buff));
    gets(buff);

    if (buff[0] == 't' || buff[0] == 'T'){          
        ret = tcpserver();          
    } else if (buff[0] == 'c' || buff[0] == 'C'){
        ret = tcpclient();
    } else if (buff[0] == 'o' || buff[0] == 'O'){
        fprintf(stderr, "Enter socket fd:\n");
        gets(buff);         
        ret = close_tcp(atoi(buff));
    } else if (buff[0] == 's' || buff[0] == 'S'){
        fprintf(stderr, "Enter socket fd:\n");
        gets(buff);         
        ret = send_tcp_msg(atoi(buff));
    } 

    }while(buff[0] != 'q' && buff[0] != 'Q');

    return 0;
}

int main(int argc, const char* argv[]){

    nwMenu();

    return 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-06-04T01:08:15+00:00Added an answer on June 4, 2026 at 1:08 am

    Your server does:

     saTCPServer.sin_port = 30000;
    

    That should be

     saTCPServer.sin_port = htons(30000);
    

    The port have to be specified in network order, if you’re on a little endian machine , 30000 does not translate to the port 30000, but to port 12405. So your server is listening on a different port than what your client is connecting to.

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

Sidebar

Related Questions

I have client and server programs which now communicate via TCP. I'm trying out
I am trying out WebIssues link text which allows for a client-server issue tracker.
I have a TCP server running which spits out messages of 2 bytes at
I'm trying to make a server/client program where the client sends the text Hello
I'm trying to implement a TCP connection, everything works fine from the server's side
I am trying to make a simple TCP client and host chat program in
I am trying to develop a TCP client that runs on mobile devices using
im making a simple TCP client-server in c and im trying to send a
I am having a problem with a client-server TCP connection. I am trying to
This is a conceptual programming question. I'm writing a tcp server/client program in Java.

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.