|Winsock & .NET |Winsock | < Socket Programming Introduction | Linux Socket Index | Socket Programming Interface (APIs) > |















Network Story 1

Network Story 2

Network Story 3

Network Story 4

Network Story 5

Network Story 6

Socket Example 1

Socket Example 2

Socket Example 3

Socket Example 4

Socket Example 5

Socket Example 6

Socket Example 7

Advanced TCP/IP 1

Advanced TCP/IP 2

Advanced TCP/IP 3

Advanced TCP/IP 4

Advanced TCP/IP 5

My Training Period:  xx hours


Note: Program examples if any, compiled usinggcc on Linux Fedora Core 3 machine with several update, as normal user.  The Fedora machine used for the testing having the "No Stack Execute" disabled and the SELinux set to default configuration.


The Client-Server Model

  • TCP/IP enables peer-to-peer communication.

  • Computers can cooperate as equals or in any desired way.

  • Most distributed applications have special roles.  For example:

  1. Server waits for a client request.

  2. Client requests a service from server.

Some Security Definitions

Connectionless (UDP) vs Connection-Oriented (TCP) Servers

  • Programmer can choose a connection-oriented server or a connectionless server based on their applications.

  • In Internet Protocol terminology, the basic unit of data transfer is a datagram.  This is basically a header followed by some data.  The datagram socket is connectionless.

  • User Datagram Protocol (UDP):

  1. Is a connectionless.

  2. A single socket can send and receive packets from many different computers.

  3. Best effort delivery.

  4. Some packets may be lost some packets may arrive out of order.

  • Transmission Control Protocol (TCP):

  1. Is a connection-oriented.

  2. A client must connect a socket to a server.

  3. TCP socket provides bidirectional channel between client and server.

  4. Lost data is re-transmitted.

  5. Data is delivered in-order.

  6. Data is delivered as a stream of bytes.

  7. TCP uses flow control.

  • It is simple for a single UDP server to accept data from multiple clients and reply.

  • It is easier to cope with network problems using TCP.

Stateless vs Stateful Servers

  • A stateful server remembers client data (state) from one request to the next.

  • A stateless server keeps no state information.

  1. Specify complete file names in each request.

  2. Specify location for reading or writing.

  3. Re-authenticate for each request.

  1. More robust.

  2. Lost connections can't leave a file in an invalid state.

  3. Rebooting the server does not lose state information because there is no state information hold.

  4. Rebooting the client does not confuse a stateless server.








Concurrent Processing



Network Concurrency

Server Concurrency

Programs vs Processes

e.g.: Multiple pico processes have different text on the screen.

Concurrency using fork() in UNIX/Linux

/* testpid.c */

#include <stdio.h>

#include <stdlib.h>

#include <unistd.h>

#include <sys/types.h>

#include <sys/wait.h>


int main (int argc, char **argv)


    int i, pid;

    pid = fork();


    printf("Forking...the pid: %d\n", pid);

    for (i = 0; i < 5; i++)

        printf(" %d    %d\n", i, getpid());

    if (pid)


   return 0;



[bodo@bakawali testsocket]$ gcc -g testpid.c -o testpid

[bodo@bakawali testsocket]$ ./testpid

Forking...the pid: 0

 0    27166

 1    27166

 2    27166

 3    27166

 4    27166

Forking...the pid: 27166

 0    27165

 1    27165

 2    27165

 3    27165

 4    27165

Time slicing

Using exec family to execute a new program in UNIX/Linux

int execve(const char *file, char *const argv [ ], char *const envp[ ]);

int execl(const char *path, const char *arg, ...);

int execlp(const char *file, const char *arg, ...);

int execle(const char *path, const char *arg, ..., char * const envp[ ]);

int execv(const char *path, char *const argv[ ]);

int execvp(const char *file, char *const argv[ ]);

Context Switching

Asynchronous I/O

Programming Interfaces


TCP/IP Application Programming Interface (API)

Required Functionality

System Calls


The Socket Interface

Socket APIs for connection-oriented TCP


Figure 8

Socket APIs for connectionless UDP

Figure 9


Berkeley Sockets: API for TCP/IP Communication

Socket Descriptors

System Data Structures for Sockets

  1. Protocol Family: a parameter to the socket call.

  2. Service Type (Stream, Datagram): parameter to socket.

  3. Local IP Address: can be set with bind().

  4. Local Port: can be set with bind().

  5. Remote IP Address: can be set with connect().

  6. Remote Port: can be set with connect().

Active vs Passive Sockets

Socket Endpoints

Socket type




TCP, Systems Network Architecture (SNA-IBM), Sequenced Packet eXchange (SPX-Novell).






UDP, SNA, Internetwork Packet eXchange (IPX-Novell).






Table 4:  AF_INET socket combinations

  1. AF_UNIX

  2. AF_NS


AF_UNIX address family

struct sockaddr_un {

short sun_family;

char sun_path[126];


AF_NS address family

struct   sockaddr_ns {

unsigned short   sns_family;

struct ns_addr   sns_addr;

char sns_zero[2];


AF_TELEPHONY address family

struct sockaddr_tel {

short stel_family;

struct tel_addr stel_addr;

char stel_zero[4];


struct tel_addr {

unsigned short t_len;

char t_addr[40];



Continue on next Module…More in-depth discussion about TCP/IP suite is given inAdvanced TCP/IP Programming Tutorials.








Further reading and digging:


  1. Check thebest selling C/C++, Networking, Linux and Open Source books at Amazon.com.

  2. Protocol sequence diagram examples.

  3. Another site for protocols information.

  4. RFCs.

  5. GCC, GDB and other related tools.






|Winsock & .NET |Winsock | < Socket Programming Introduction | Linux Socket Index | Socket Programming Interface (APIs) > |