How to connect client and server by socket



The steps to establish a socket on the client side:

1. Create a socket with the socket() system call

int socket(int domain, int type, int protocol);

There are two possible address domains, the unix domain for two process( client and server) which share a common file system, and the Internet domain for any two hosts on the internet. The symbol constant AF_UNIX is used for th e former, and AF_INET for the latter(there are actually many other options which can be sued here for specialized purposes).
The second argument is the type of socket. There are two choices, a stream socket in which characters are read in a continuous stream as if from a fileor pipe, and a datagram socket, in which messages are read in chunks. The two symbolic constants are SOCK_STREAM and SOCK_DGRAM.

For instance,
int sockfd = socket(AF_INET, SOCK_STREAM, 0);

2. Connect the socket to the address of the server using the connect() system call

int connect(int sockfd, const struct sockaddr *serv_addr, socklen_t addrlen);

The connect function is called by the client to establish a connection to the server. The first argument is socket file descriptor( an integer), the second is the host to which it wants to connect(include the port number of the server), and the the last is is the size of this address. This function returns 0 on success and  -1 if it fails.

For instance,

struct sockaddr_in serv_addr; // see here for definition of sockaddr
if(connect(sockfd, &serv_addr_addr, sizeof(serv_addr)) <  0)
{ // do something...}

3. Send and receive data.

#include
ssize_t read(int fd, void *buf, size_t count);

For instance,
int n =  read(sockfd, buffer, 255);

The first argument is the socket file descriptor. It read up to count bytes from fd into the buffer starting at buf. On success, the number of bytes read is returned and the file position is advanced by this number. It is not an error if this number is smaller than the number of bytes requested; this may happen for example because fewer bytes are actually available right now(maybe  because we were close to end-of-file, or because we are reading from a pipe, or from  terminal), or because read() was interrupted by a signal. On error, -1 is returnned and errno is set appropriately. In this case it is left unspecified whether the file position(if any) changes.

#include <unistd.h>
ssize_t write(int fd, const void *buf, size_t count);

For instance,
int n = wirte(sockfd, buffer, strlen(buffer));
if (n <  0)
 { // print error message}


write() writes upt to count bytes from the buffer pointed buf to the file referred to by the file descriptor fd. For detail see here.

int send(int sockfd, const void *msg, int len, int flags); //see here.

For instance,
send(sockfd, "hello, world\n",  15, 0);


int recv(int sockfd, void *buf, int len, int flags); // see here




The steps to establish a socket on the server side:

1. Create a socket with the socket() system call
2. Bind the socket to an address using the bind() system call. For a server socket on the Internet, an address consists of a port number on the host machine.

int bind(int sockfd, struct sockaddr *my_addr, int addrlen); // see here.

For instance,
if (bind(sockfd, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0)
{// error msg...}

3. Listen for connections with the listen() system call

int listen(int sockfd, int backlog); // see here

For instance,
listen(sockfd, 5);


4. Accept a connection with the accept() system call. This call typically blocks until a client connects with the server.


int accept(int sockfd, struct sockaddr *addr, socklen_t *addrlen); // see here and here


For instance,
struct sockaddr_in cli_addr;
stocklen_t clilen;
int newsockfd = accept(sockfd, (struct sockaddr *) &cli_addr, &clilen);


5. Send and receive data.

No comments: