1 of 8

Slide Notes

DownloadGo Live

Chatting Program in C

Published on Nov 19, 2015

No Description

PRESENTATION OUTLINE

Chatting Program in C

Operating Systems Class
Photo by Naveg

The steps involved in establishing a socket on the server side

  • Create a socket with the socket() system call
  • Bind the socket to an address using the bind() system call.
  • Listen for connections with the listen() system call
  • Accept a connection with the accept() system call.
  • Send and receive data

The steps involved in establishing a socket on the client side

  • Create a socket with the socket() system call
  • Connect the socket to the address of the server using the connect() system call
  • Send and receive data.
  • Simplest is to use the read() and write() system calls

Untitled Slide

  • #include This header file contains declarations used in most input and output and is typically included in all C programs.
  • #include This header file contains definitions of a number of data types used in system calls. These types are used in the next two include files.
  • #include The header file socket.h includes a number of definitions of structures needed for sockets.  
  • #include The header file netinet/in.h contains constants and structures needed for internet domain addresses.

int main(int argc, char *argv[])
{
int sockfd, newsockfd, portno, clilen, n;
sockfd and newsockfd are file descriptors, i.e. array subscripts into the file descriptor table . These two variables store the values returned by the socket system call and the accept system call.
portno stores the port number on which the server accepts connections.

clilen stores the size of the address of the client. This is needed for the accept system call.

n is the return value for the read() and write() calls; i.e. it contains the number of characters read or written.

char buffer[256];
The server reads characters from the socket connection into this buffer.

struct sockaddr_in serv_addr, cli_addr;
A sockaddr_in is a structure containing an internet address. This structure is defined in . Here is the definition:
struct sockaddr_in {
short sin_family;
u_short sin_port;
struct in_addr sin_addr;
char sin_zero[8];
};
An in_addr structure, defined in the same header file, contains only one field, a unsigned long called s_addr. The variable serv_addr will contain the address of the server, and cli_addr will contain the address of the client which connects to the server.

Untitled Slide