Linux sockets: Difference between revisions

From miki
Jump to navigation Jump to search
(Created page with "== Tutorials = * [https://www.binarytides.com/socket-programming-c-linux-tutorial/ Socket programming in C on Linux – tutorial]. : A thoough step-by-step example of client a...")
 
Line 1: Line 1:
== Tutorials =
== Tutorials ==
* [https://www.binarytides.com/socket-programming-c-linux-tutorial/ Socket programming in C on Linux – tutorial].
* [https://www.binarytides.com/socket-programming-c-linux-tutorial/ Socket programming in C on Linux – tutorial].
: A thoough step-by-step example of client and socket code.
: A thoough step-by-step example of client and socket code.
* [https://www.thegeekstuff.com/2011/12/c-socket-programming/ C Socket Programming for Linux with a Server and Client Example Code]
* [https://www.thegeekstuff.com/2011/12/c-socket-programming/ C Socket Programming for Linux with a Server and Client Example Code]
: Simple example of client and server code using Linux sockets.
: Simple example of client and server code using Linux sockets.

== Troubleshooting ==
=== Use errno / strerror to get error ===
<source lang=c>
if( bind(socket_desc,(struct sockaddr *)&server , sizeof(server)) < 0)
{
printf("bind failed: %s (errno %d)", strerror(errno), errno);
return 1;
}
</source>

=== Use <code>netstat</code> to get socket status ===
<source lang="bash">
./server
# bind done
# Waiting for incoming connections...
# Connection accepted
# Handler assigned
# ^C

./server
# bind failed: Address already in use (errno 98)

netstat -a | grep 8888
# tcp 0 0 zavcxl0005:58888 165.225.76.32:http ESTABLISHED
# tcp 0 0 zavcxl0005:48888 10.75.126.1:https ESTABLISHED
# tcp 0 0 localhost.localdom:8888 localhost.localdo:50310 TIME_WAIT

# ... So port is still in use
</source>

Revision as of 14:53, 14 September 2018

Tutorials

A thoough step-by-step example of client and socket code.
Simple example of client and server code using Linux sockets.

Troubleshooting

Use errno / strerror to get error

if( bind(socket_desc,(struct sockaddr *)&server , sizeof(server)) < 0)
{
    printf("bind failed: %s (errno %d)", strerror(errno), errno);
    return 1;
}

Use netstat to get socket status

./server
# bind done
# Waiting for incoming connections...
# Connection accepted
# Handler assigned
# ^C

./server
# bind failed: Address already in use (errno 98)

netstat -a | grep 8888
# tcp        0      0 zavcxl0005:58888        165.225.76.32:http      ESTABLISHED
# tcp        0      0 zavcxl0005:48888        10.75.126.1:https       ESTABLISHED
# tcp        0      0 localhost.localdom:8888 localhost.localdo:50310 TIME_WAIT  

# ... So port is still in use