Sockets

.

Code Example

http://www.binarytides.com/socket-programming-c-linux-tutorial/

Code example ( Server & Client ) with explanaition
http://www.cs.rpi.edu/~moorthy/Courses/os98/Pgms/socket.html

C socket server example, handles multiple clients using threads
https://gist.github.com/silv3rm00n/5821760

.

Click here to see the socket () man page.

Click here to see the man page for bind().

Click here to see the man page for listen () .

Click here to see the man page for read().

Click here to see the man page for write ().

.

libc :  http://www.gnu.org/software/libc/manual/pdf/libc.pdf

.

 

  Server         1. socket ()                       2. bind ()                                                                                           3. listen ()                                              4. accept ()

                                              5. write () ….

  Client          1. socket ()                       2. connect ()                                              3. write ()  .read ()….write ()….. 

 

.

struct sockaddr_in
A sockaddr_in is a structure containing an internet address.
This structure is defined in <netinet/in.h>. Here is the definition:

struct sockaddr_in
{
short   sin_family;
u_short sin_port;
struct  in_addr sin_addr;
char    sin_zero[8];
};

.

Accepting Connections

A socket that has been established as a server can accept connection requests from multiple clients. The server’s original socket does not become part of the connection; instead, accept makes a new socket which participates in the connection. accept returns the descriptor for this socket. The server’s original socket remains available for listening for further connection requests.

The number of pending connection requests on a server socket is finite. If connection requests arrive from clients faster than the server can act upon them, the queue can fill up and additional requests are refused with an ECONNREFUSED error. You can specify the maximum length of this queue as an argument to the listen () function, although the system may also impose its own internal limit on the length of this queue.

int accept (int socket, struct sockaddr *addr, socklen t *length_ptr)

This function is used to accept a connection request on the server socket socket.The accept function waits if there are no connections pending, unless the socket socket has nonblocking mode set. (You can use select to wait for a pending connection, with a nonblocking socket.) After accept, the original socket socket remains open and unconnected, and continues listening until you close it. You can accept further connections with socket by calling accept again. If an error occurs, accept returns -1. The following errno error conditions are defined for this function:

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

Leave a comment