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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T02:56:13+00:00 2026-05-17T02:56:13+00:00

I have written a very simple socket server. It listens in post 63254. First

  • 0
  1. I have written a very simple socket server.
  2. It listens in post 63254.
  3. First i did a socket_create, socket_bind, socket_listen so here a connection is listening.
  4. Then in a loop i do the socket accpet. so here another listen.
  5. the read function reads untill i input exit.
  6. after that the resource id by socket_accept closes.
  7. and then the main connection closes.

when i checked this process in TCPview after closing all connections i can still see the system process showing TIME_WAIT for post 63254

if i again run the socket server program it is connecting and when one full process is over all the connection is closed and the program terminated and now i can see another TIME_WAIT for the same port. but still i could connect to the same port the third time.

in stackover question answer it is said that connection cannot be done for port which is in wait state.

I opened firefox browser it opened 4 connections.
when i closed it all closed and the system process showed 4 time waits for 2 minutes.
all time wait stays for 2 minutes and disappears.

so what i conclude is for every connection close the time wait is occurs and cannot be avoided.

i read many posts in stack overflow flow but still wasn’t sure of it.

i run the following code in command line.

My server Code

<?
error_reporting(E_ALL);
set_time_limit(0);
ob_implicit_flush(); 

$str = '';
$buff = '';

$s = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
if(!$s)die('Unable to create socket');

if(!socket_bind($s,'127.0.0.1',63254))
    die("\nTrying to Bind: ".socket_strerror(socket_last_error()));

if(!socket_listen($s,1))
    die(socket_strerror(socket_last_error()));

    while(1)
    {
        $acc = socket_accept($s);
        if(!$acc)die(socket_strerror(socket_last_error()));
//      echo "\n".gettype($acc);
        if(!$acc)die(socket_strerror(socket_last_error()));

        while(1)
        {
            $str = socket_read($acc,512);
            $buff.= $str;
            echo $str;
//          echo '::'.gettype($str);

            if($str===false)die(socket_strerror(socket_last_error()));
            if($str=="exit\r\n")break;          
        }

//      if(!socket_shutdown($acc,2))echo socket_strerror(socket_last_error());  
        socket_close($acc);     
        if(preg_match('/exit/',$buff))break;
    }
//echo "\nConnection closed by server\n";   
//if(!socket_shutdown($s,2))echo socket_strerror(socket_last_error());
socket_close($s);
?>

The client code

<?
    set_time_limit(0);
    $f = fsockopen('127.0.0.1',63254,$a,$b,10);
    if(!$f)die('cannot connect');
    echo "\nConnected: \n";
    do{
        $buff = fgets(STDIN);   
        fwrite($f,$buff);
    }while($buff!="exit\r\n");
    fclose($f);
?>

need suggestions to improve a better client server if this is not sufficient. this code is just a child’s play. just trying to understand the way communication works.

  • 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-17T02:56:14+00:00Added an answer on May 17, 2026 at 2:56 am

    In stackover question answer it is
    said that connection cannot be done
    for port which is in wait state.

    I don’t know what answer you’re referring to, but you cannot bind to a port which is in TIME_WAIT state. If you are a server you can use setReuseAddress() to overcome this. If you’re a client you have to wait, or use a different outbound port, or best of all don’t specify an outbound port at all, let the system find one. You are a server so this doesn’t apply to you.

    I opened firefox browser it opened 4
    connections. when i closed it all
    closed and the system process showed 4
    time waits for 2 minutes. all time
    wait stays for 2 minutes and
    disappears.

    But those are client ports. Outbound ports. At your server they were inbound ports, and there was also a listening port on the same port number. As long as there is a listening port, an inbound connnection can succeed.

    so what i conclude is for every
    connection close the time wait is
    occurs and cannot be avoided.

    TIME_WAIT occurs when you are the end that sends the close first. If you are the end that received the close, and closed in response, your port doesn’t go into TIME_WAIT at all.

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

Sidebar

Related Questions

I have a very simple client server code written java(server listens on some port
I have a written a very simple UDP Server using Netty - it quite
I have a very simple server written in C and an equally simple client
I have a very simple TCP server written in C. It runs indefinitely, waiting
I have a very simple web sever written in Python. It listens on port
I have written very very simple console application which supports some command line options.
I have written a very simple CTE expression that retrieves a list of all
I have written a very simple application to update my twitter status on a
I have written a very simple control. C# Visual Studio 2008. Its output should
I have written a very simple WCF service, that worked fine (code below), then

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.