After call accept() in a network server run by systemd. Netstat doesn't show my service
<p>I am not sure if anyone has asked this before. 
There is a service running fine on my Linux server (3.14.39) by systemd.
Now, I want to add a function which creates a thread and bring up a small network server listening to port <code>6666</code>.
Simply do,</p>

<pre><code>   memset(&amp;server_addr,0, sizeof(server_addr)); 
   server_addr.sin_family = AF_INET;
   server_addr.sin_addr.s_addr = htons(INADDR_ANY);
   server_addr.sin_port = htons(6666);

   server_socket = socket(PF_INET,SOCK_STREAM,0);
   setsockopt(server_socket,SOL_SOCKET,SO_REUSEADDR,&amp;opt,sizeof(opt));
   bind(server_socket,(struct sockaddr*)&amp;server_addr,sizeof(server_addr));
   listen(server_socket, 10);
   new_server_socket = accept(server_socket,(struct sockaddr*)&amp;client_addr,&amp;length);
</code></pre>

<p>All of above calls return successfully and the program seems waiting the client connection.</p>

<p>However, when I use <code>netstat -a</code> to check my server state there is nothing!
And I cannot connect to the socket by telnet either.</p>

<pre><code> netstat -ant
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address           Foreign Address         State
tcp        0      0 0.0.0.0:5355            0.0.0.0:*               LISTEN
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN
tcp        0      0 0.0.0.0:8080            0.0.0.0:*               LISTEN
tcp        0      0 0.0.0.0:443             0.0.0.0:*               LISTEN
tcp        0      0 :::5355                 :::*                    LISTEN
tcp        0      0 :::80                   :::*                    LISTEN
tcp        0      0 :::22                   :::*                    LISTEN
tcp        0      0 :::443                  :::*                    LISTEN
tcp        0      0 :::6556                 :::*                    LISTEN
</code></pre>

<p>I did an experiment to extract the same C subroutine and build it as an standalone executable server program then run it after system boot up. Everything works fine.... So I suppose it is not coding error, firewall.</p>

<p>I also check the <code>lsof -ni</code>  and I can see my server and socket information.</p>

<pre><code>root@:lsof -ni |grep 25199
316  /usr/sbin/test   socket:[25199]
</code></pre>

<p>Hope I am clear myself..
I just have no clue and direction how to debug this. Anyone has idea what I can do or check right now? Even suggest a document would help! 
Thanks!</p>

<hr>

<p>Add more information</p>

<pre><code>1. ping 127.0.0.1 is ok so I guess LO is up

    root@:~# ping 127.0.0.1
    PING 127.0.0.1 (127.0.0.1) 56(84) bytes of data.
    64 bytes from 127.0.0.1: icmp_seq=1 ttl=64 time=0.050 ms
    64 bytes from 127.0.0.1: icmp_seq=2 ttl=64 time=0.066 ms
    64 bytes from 127.0.0.1: icmp_seq=3 ttl=64 time=0.053 ms
    64 bytes from 127.0.0.1: icmp_seq=4 ttl=64 time=0.042 ms
    64 bytes from 127.0.0.1: icmp_seq=5 ttl=64 time=0.041 ms
</code></pre>

<ol start="2">
<li><p>I suspect that my service may be brought up before the network so I add network-online.target in my systemd unit file. But it's not working either.</p>

<p>After=network-online.target</p></li>
</ol>