Network Programming Quick Tutorial |
![]() |
Tuesday, 03 May 2011 | |
work in progress! come back later. For network programming we will use Client / Server architecture. Server is a program that stays in memory and accept requests on a port. A client connects to the server using an IP address of the server and port of the server. Server respond to the client and send back the data client requested. TCP Server functions (the order is also important) socket(); // function creates an endpoint for comunication bind(); // asigns a local protocol address to a socket listen(); // listen for connections on a socket accept(); // after this function server block execution of the program until a client connects to the server. fork(); read(); write(); read(); close(); If we do not assign a port to the structure used by bind for the tcp server, the kernel will create an ephemeral port for the socket when listen() or connect() functions are called. In this case we must use getsockname to return the value of the efemeral port assigned by the kernel. TCP Client functions socket(); connect(); // connects to the server write(); read(); close(); Functions in detail ------------------ socket() function ------------------- socket() function creates an endpoint for comunication. socket() function #include <sys/types.h>#include <sys/socket.h> int socket(int domain, int type, int protocol); Where domain can be: bind() function #include <sys/types.h>#include <sys/socket.h> int bind(int s, const struct sockaddr *addr, socklen_t addrlen); If we want to specify a wildcard for IP address, we must use the constant INADDR_ANY (default value is 0): listen() function #include <sys/types.h>#include <sys/socket.h> int listen(int sockfd, int backlog); listen() function moves the socket from CLOSED to LISTEN state.
#include <sys/types.h> #include <sys/socket.h> int accept(int s, struct sockaddr * restrict addr, socklen_t * restrict addrlen);
connect() function #include <sys/types.h>#include <sys/socket.h> int connect(int s, const struct sockaddr *name, socklen_t namelen); If connect() fails we must close() the socket descriptor before calling connect() again. fork() function ------------------ fork() function creates a new process. fork() function #include <sys/types.h>#include <unistd.h> pid_t fork(void); fork() function returns 0 in child process, process ID of child in parent and -1 on error. Notes ------- - between client and the server before sending the data is a connection negotiatiation called three way handshake, after that data is requested by the client, server send the data to the client and then client send back to the server a End of File notification. For more information see man pages of those functions. Tip1. When working with network functions always check for errors for every function call. To simplify the code build your own wrapper to well known function you use. And use a naming convention for that. For example your socket function wrapper could be called Socket (with capital S). |
|
Last Updated ( Thursday, 01 March 2012 ) |