Disclaimer: This is an example of a student written essay.
Click here for sample essays written by our professional writers.

Any opinions, findings, conclusions or recommendations expressed in this material are those of the authors and do not necessarily reflect the views of UKEssays.com.

Simple Client Server Chatting Application Information Technology Essay

Paper Type: Free Essay Subject: Information Technology
Wordcount: 5305 words Published: 1st Jan 2015

Reference this

With the rapid advances in the technology the network communication between different people has made much easier than one can expect. And the network is the heart of communication. Socket API Application program interface is the main utilization and which plays a important base for this purpose. The current application allows multiple users to communicate with each other which is enhanced to allow the socket application program interface. Thus communication mechanism makes much easier. Application allow different type of users to have communication mechanism by implementing a simple client to server communication for mutual chatting application. The objective of the chat server is to transfer messages reliably and efficiently, data and voice.

Get Help With Your Essay

If you need assistance with writing your essay, our professional essay writing service is here to help!

Essay Writing Service

Chat server is a standalone application that is made up the combination of two-application, server application (which runs on server side) and client application (which runs on client side). This application is using for chatting in stand alone system using loop back network. Initially when we want to send a message to the client and every user a server should be connected.

1.1 Objective:

The aim of this project is to express how we can implement a simple chat application between a server and a client? The application is a console based application and is implemented using Linux BSD Sockets. The project is developed in C language executed on a single stand-alone Linux machine across a network using loop back address concept.

Application consists of two programs:

Server

Client

Server

The server module of the application waits for the client to connect to it. Then if connection is granted a client interacts communicates and connects to the server, it can mutually communicate with the server. The duty of the server is to let clients exchange the messages.

Client

The client module is the one that utilizer sends requests to the server. Utilizer utilizes the client as the means to connect to the server. Once he establishes the connection, he can communicate to the connected server.

1.2 Execution of the project:

Below are steps to create the development environment and execute the current project application:

Start Linux OS and let the kernel up.

Enter as any utilizer (preferably as root utilizer).

Open two consoles for developing the server and client applications.

Open the editors (VI editor) and edit the server and client application programs.

Compile and run the server program first and next the client program.

As per the client a request, the chat server act in response to the client and then chatting can be done between the two.

CHAPTER 2

BRIEF OUT ON SOCKET PROGRAMMING AND CLIENT – SERVER MODEL IN LINUX

2.1 Socket Types

Socket plays important role in programming initially when a socket has to define or when a socket is created the program has to describe or introduce the socket i.e socket type and socket address. If two sockets are in same domain and same type they are said to be that they can communicate each other.

Sockets are most widely used address domains i.e families and unix domain were both processes can share a common file system communication; and the Internet domain in which both the socket type and socket address are running on any two hosts on the Internet communicate. These are called as local sockets or network sockets .

In UNIX domain the address of socket is a character string which is initially entry in a file system

The low numbers are reserved in UNIX which are standard services. Basically standard services are kept on all PCs at the same port so the users or clients are much familiar with their address. Port number above 2000 can be used.

There are usually two types of socket stream sockets which are connection oriented, and datagram sockets which are connection less. All sockets are assumed by various port numbers on the host. Port numbers are generally 32 bit unsigned integers. Stream connection oriented socket are continuous flow of stream of characters, while connection less sockets are able to read the entire message at a time. Each socket utilizes its individual transportation protocol.

STREAM SOCKETS uses transmission protocol which are reliable and when we compared to DATAGRAM ORIENTED SOCKETS utilizing user protocol which are capable of using the linux. These sockets are message oriented and unreliable.

The current project sockets utilizes the Internet domain (or family) using the TCP protocol.

2.2 The client server model

 

Client server model diagram

source: http://www.javaworld.com/javaworld/jw-10-2001/jw-1019-jxta.html

Most inter process communication (IPC) utilizes the client servers. There are two types of inter process communication one is client process and the other server process in which both process will communicate with each other at any time and any were in the network. Generally client is on one side of process and server is attached with some basic data and information. So that the client and server can communication and exchange their data and information based on inter process model. For example, a user or person making phone call to other user or person

Client has to note the address of the server were it is to connect with it and communicate and whereas server doesn’t need any address location and presence to communication because the server is already in network therefore a connection is established between client and server.

Once a connection is established between both process client and server can send and receive information and data. The application program interface usually introduces and establishes a different connection between client and server, but both utilize the basic construction of socket. A socket is known as one end of an inter process communication (IPC) channel.

The two processes set up’s their own socket.

Following are the steps for establishing a socket on client side:

Initially Creating a socket with the socket() API

connect() API is used to connect the socket to the address of the server

read () and write () APIs are used for processing the data i.e. the data can be send and receive in simple way

Following are the steps for establishing a socket on the server side:

socket() API is initially creates a socket

Bind () API is used to bind the socket to a particular address on a server socket on the Internet. Since, address consists of port number IP port on the host.

listen() API listen application program interface is used for listen to the conection.

Agree to () API this socket is used for agree to the connection. Generally the connection is restricted until client connects and communicates with the server

Receiving and sending of data.

CHAPTER 3

BRIEF TUTORIALS ON CHAT SERVER AND CLIENT

3.1 Chat Server

In general chat server is an application that does the following:

Listens requests for incoming calls from clients.

Listens messages from all the connected clients.

Broadcasts the messages from clients to all the clients connected in the server.

Also we can type-in messages in the server that will be broadcasted to all the clients.

3.2 Chat Client

Chat client does the following:

Sends messages to server as well as to all the connected clients in the network.

Also views the messages from all the clients and server.

CHAPTER 4

SOCKETS IN DETAIL WITH REFERENCE TO LINUX INTERNALS

4.1 Sockets

The basic dissimilarity between a “server” socket and a “client” socket is more like on and off switch. When a network is established connection is fluctuated. The client application for example our browser network uses the client, where as web servers are mainly used to communicate with the client sockets and server sockets. Sockets can communicate with more than one user for establishing a connection.

Sockets are considerably the most popular of different forms of inter process communication. On any type of particular platform there are more than one that is it has multiple forms of inter process communication which are rapid fast and easy. But for external communication only single platform is used by the socket.[d]

4.2 How to create a Socket

  Syntax

  #include

  #include

  int socket(int address_family,

int type,

int protocol)

  Service Program Name: QSOSRV1

  Default Public Authority: *USE

It appears as following

#create an INET, STREAMing socket s = socket. Socket (socket.AF_INET, socket.SOCK_STREAM)

#web server is attached on port number 80

# – is normal http port s.connect (“www.mcmillan-inc.com”, 80)

While 1:

//connection are agreed from outside

(Clientsocket, address) = serversocket.agree to ()

#now does something with the clientsocket

#in this case, we’ll pretend this is a threaded server

Ct = client_thread (clientsocket)

Serversocket.listen

ct.run ()

There are generally 3 ways in which this loop could work – create a new process client socket, dispatching a thread client socket or restructure this application to approach a unblocked socket and cross multiply between our client socket and server socket which is used bye a select. The basic essential thing to understand is all about server socket is. It doesn’t accomplish any communication. Just it generates “client” sockets. Sockets can be used from one host to other host connection. Listening a socket tells that a socket is waiting for a next connection The receiver then gets an indication that there is a new connection. It generates a new socket which represents the new connection. It then does a recv to receive data from the connection. The sender after returning from the connect call uses the send call to send data to the receiver. After creating a client socket we wait for new call and we listen to more connection. clients are allocated port with dynamism which will be indicates the sockets which is going to transmit the exchange data. Using a connect() each client socket uses a port number to communicate with other port number. sockets are then bind to agree and utilize the connection.

Only one socket is used for one transfer basically http protocol. Initially client sends a request and then reads a reply. Client can detect the end of the report as it receives zero bytes that is it is said to be the socket is disabled.

But if you plan to reutilize your socket for further transfers, you have to assume that there is no End of Transfer on a socket. The connection has been broken if a socket receives or sends.

Assuming that if you do not want to end the connection, the easiest way of solution is a fixed length message:

Class mysocket:

”’demonstration for class only “

– coded for clarity, not efficiency”’

def __init__Stream(self, sock=None):

if sock is None:

self.sockect = socket.socket (

socket.AF_INET, socket.SOCK_STREAM)

else:

self.sock = sock

def connect(host, port):

self.sock.connectrec ((host, port))

def myreceive ():

msg = ”

while len(msg) < MSGLEN:

chunk = self.sock.receive (MSGLEN-len(msg))

if chunk == ”:

raise RuntimeError,

“socket connection broken”

msg = msg + chunk

http://www.amk.ca/python/howto/sockets/

def my send (msg):

totalsent = 0

while totalsent < MSGLENGTH:

sent = self.sock.send (msg[total sent:],STREAM)

if sent == 0:

raise Runtime Error,

“socket connection broken”

totalsent = totalsent + sent

return msg

Length () in a socket address which determines the string length and capable of sending code .codes can be used again for any message to send and receive data. String characters can only be used by pythons. Complexity of receiving a code is very high. The socket program enable to send a request and receive a reply .

The first character of the socket program is assumed as message type and this type is determine as length this is said to be easiest way of enhancement with this there are two types of receives which are the indicated as the length of the socket is placed at first character and other character is used to find a loop.[11]

Protocols are used to allow end to end multiple message which are sent back to back and you go on receive smaller size. you may end up reading the start of a following message. Once a message has been received you have to keep it aside and hold on it until it is needed.by this ypu can manipulate more complex and sender can get rid of everything which is determined in a single pass.

A message generally has length of 5 characters which has prefixed and it is more efficient and complex; because we cannot find it in a single receiver you may find it in different receivers. In the following socket network, you’ll get away with it; but if don’t utilize two receivers the code will definitely break in network loads. Unless to make it more strong there are two loops – the first loop basically determines the length of the string and the 2nd loop to get the message data part. If you use the existing route you get data and messages in smaller size.[11]

4.5 Portability alert:

UNIX is selected by working process with the files and even socket. So when compared to UNIX and windows, windows works only with socket files. Windows are often more complex for utilization of socket. In fact, on Windows usually utilizes the threads very much faster than expected with my sockets. In different enhancement the socket code has lot of difference in Unix and windows both works but they are similar. That’s the reason why we use the socket files.

4.6 Shutdown a Socket

Sockets are not directly closed strictly speaking sockets utilize to shutdown. Shutdown of sockets means in different ways it says that I may end process but still you can listen or says that am not listening. Thus we say that it is an optional way of thinking to shutdown. It depends on the argument you pass. The other way of utilizing the shutdown of socket means generally used in http protocol. When once client send a request and makes a shutdown this doesn’t mean that server can’t receive it still tells the server that client has done with sending a message and still it may receive. The server sends a eof by receiving some bytes and then server sends a reply. Whereas in case of pythons if socket is empty it sends back automatically defining it is very slow once the work is completed the socket must close immediately.

Shutdown () close () mostly includes the etiquette of closing of sockets but it reflects to bad habit. Most libraries are neglecting the utilization of threads. By this when errors are occurred sudden shutdown may needed.[11]

4.7 Blocking Sockets

Probably if suddenly one side of blocking socket is blocked what happens to the other end when suddenly its closed. Blocking socket may suddenly disappear. SOCKSTREAM is a dependable protocol which waits for a connection very long time. Socket after sometimes hanged If a socket threads are used, the entire thread is in actual fact it is dead. As long as if you are trying to lock the blocking read or entire blocking sockets it is safe, by this the assets are not really overridden. Socket streams are also able in blocking sockets which can listen to a socket. Threads are more capable of avoiding the overhead which is associated with automatic recycling with this protection you cannot try to kill the thread which are more capable of the process.

4.8 Non-blocking Sockets

Once understanding about the preceding of sockets you know what mechanics are used for non blocking of socket. You can still utilize the sockets in much similar way. It says that your application may be in the socket or outside the socket this has to been done before using a socket. set blocking(0) is used to create a non-blocking this is mostly used in pythons to utilize a socket. When compared to c language it is complex but not exactly the same.

The major difference between connect socket, sending a socket, receiving a socket and agree a socket can be returned without doing anything. And have a more option. After Verify return code and error codes. In future the application may grow higher rate, large and may reduce the CPU power.[11]

http://www.amk.ca/python/howto/sockets/

CHAPTER 5

SOCKET APIs AT CLIENT AND SERVER IN LINUX

5.1 socket API()

5.11 Client application program interface:

Socket () Application call

The socket Application call is specially used to create a socket. A socket is used to read and write from network communication.

In previous chapter it is mentioned that how to find an ip address for a host name and how we can get an port number for a service name.[5]

Following is the list of prototype for the socket () API:

int socket(int add_family,

int type,

int protocol)

This means that it agrees for three parameters and procedure to call is known as socket.

D socket 10I 0 ExtProtocol(‘socket’)

D protocol 10I 0 value

D _add 10I 0 value

D addr_family 1011 value

Protocol also precedes an integer. TCP and IP protocols can be used to realize in socket APIs. This networking protocol is very essential to utilize.TCP networking protocol uses the stream socket over internet domain. we already specified that TCP must use.

Presently we don’t know what integer values we are assigning to each socket protocol which are IPPROTO_IP which is the networking protocol for IP,AF_INET and SOCK STREAM are, socket stream are connection oriented, in some header files of C programming IBM is known for representing about some named constants, when a bit of snooping is done into system open which includes libraries these are defined as RPG in some particular case networking protocol ip is defined to 0, socket stream is defined as 1 and similarly af_net is defined to be 2. This tells us that this constants are named.[8]

D TCPPROTO-TCP C CONST(3)

D AF_INET C CONST(2

D SOCK_STREAM C CONST(1)

D IPPROTO_IP C CONST(0)

Finally socket () API look like,

s = socket(AF_INET:IPPROTO_IP:SOCK_STREAM)

2. connect () Application protocol interface call

Connect – connect means initializing a new connection on socket

SYNOPSIS

#include

#include

int connect(int sockfd, struct sockaddr *serv_addr, int

addrlen);

int SOCK_DGRAM

Returning a value to 0 or 1

Once it is bind a socket or connected the value 0 is returned

Initially if we want to work on a socket we have to connect the socket to some other this connecting of socket is done by connect().Thus connect () prototype application program interface looks like as follows

int connect(int socket_destination

struct sockaddr *discriptor_address,

int address_length)

Following is the RPG prototype:

D connect PR 10I 0 ExtProc(‘connect’)

D sock_desc 10I 0 value

D dest_addr * value

D addr_len 10I 0 value

Socket address is defined as follows:

struct sockaddr {

u_short sa_family;

char sa_data[18];

};

The main idea of this socket structure is to tell the application program interface which port number and internet protocol to connect it. socket address includes the recognizing address domain family. sockaddr structure is known as generic structure. Address domain and generic data field can be placed in format of address. In socket address the data is character string. Socket address is generally represented as 18 bit unsigned integer.[10]

Find Out How UKEssays.com Can Help You!

Our academic experts are ready and waiting to assist with any writing project you may have. From simple essay plans, through to full dissertations, you can guarantee we have a service perfectly matched to your needs.

View our services

Connect () application interface program is actually having a different socket structure called ‘sockaddr_in’. But this document is not attached in IBM’s page for connecting a network. Socket address is specially designed for internet address protocol. Each internet protocol has different format to how can a address can work. Basically c definition for sockaddr_in is found in file IPSOC which is internet protocol socket and this socket includes the socket library.

The following looks like as follows

struct sockaddr_in { /* socket address (internet) */

short struct_family; /* address family (AF_INET) */

u_short struct_port; /* port number is defined */

struct in_addr sin_addr; /* IP address */

char struct_zero[8]; /* reserved */

};

For better and more reliable utilization of the structures of RPG these structures should be made to use the same memory area. Sin family allocates the family address. This tells that we can use both the socksddr and sockaddr_in. The basic difference is the address can be moved without changing any data. Socket address is address of the socket which is determined.[8]

D p_sockaddr S

Connect () API is used for connecting a call so initially we have to approach the operating system for memory to store the socket address structure into call connect. Socketaddr_in is the structure which can be used into address

Socketaddr_in struct value 1011

3. Sending and receiving Application interface program calls

Basically we have to initiate a connection, so once we got a connection we can transfer data from sender to receiver over a network. For sending and receiving a data special sockets are designed which are send () API and receive () API.[c]

Send() API prototype is designed as

int send (int socket_destination,

char *buffer,

int buffer_length,

int flags)

This sending and receiving APIs have four parameters which are integer and parameters. Thus, the RPG prototype looks like:

D send PR 10I 0 ExtProc(‘send’)

D sock_desc 10I 0 value

D buffer * value

D buffer_len 10I 0 value

D flags 10I 0 value

‘char *’ definitions asa character string which is specific for family address, we put the ‘choices (*string)’ keyword in our D-specs. The buffer length has a values of 1010 and buffer_len is the buffer length which is defined a length of the socket buffer.flags are also used based on return values of o and 1.[17]

Consequently, we typically call the send () API like this:

D miscdata S 25A

C sin address is send

C eval miscdata = ‘The data to send goes here’

C eval rc = send (s: %addr(miscdata): 25: 0)

c if rc < 25

C* for some reason we weren’t able to send all 25 bytes!

C endif

Receive () API is utilized to receive data over the network. By the end of the discussion both send () and receive () prototype are similar only the difference is the name of the procedure.

Receive () API prototype is designed as

int receive (int socket_descriptor,

char *buffer,

int buffer_length,

int flags)

And, just like send, the RPG prototype looks like this:

D sock_desc 10I 0 value

D buffer * value

D buffer_len 10I 0 value

D flags 10I 0 value

D receive PR 10I 0 ExtProc(‘receive’)

Similar difference between the prototypes send () and receive () is the area of memory pointed to by the ‘buffer’ parameter. Send() basically writes data to the network and Receive() is the data read from the network.

receive () prototype always waits for the all the receiving bits it also waits for the even last bit if it delays because it has to wait for the previous buffer completion. If there are number of bytes to be send using a receive prototype it only takes upto the requested length and the other bytes are returned.[e]

Closing an Application Program Interface

Close () API call

The close socket indicates the disconnecting of a socket using close() API which is mostly used to disconnect a new connection and finally which may cause socket descriptor. For using a socket we have to call using call socket (). Closing an application may also indicates the connection is lost when it is in use.

The manual tells us that the prototype for close() looks like this:

int close(int files)

C eval rc = close(s)

C if rc < 0

C*** Socket didn’t close. Now what?

c endif

The parameter only has the one integer which used by close.

RPG prototype is designed as

D close PR 10I 0 ExtProc(‘close’)

D sock_desc 10I 0 value

To call it we can simply to:

D close PR 10I 0 ExtProc(‘close’)

D sock_desc 10I 0 value

If the close() application interface program is closed it looks like

C call close(s)

Close () API is used for closing the socket. We have to define only one prototype for closing a connection which is read socket or write socket in a particular program.

Before compiling a close socket there should be without any errors and it should be defined as perfectly. [e]

unlink() – delete a name and possibly the file it refers to

SYNOPSIS

#include

int unlink(const char *pathname);

DESCRIPTION

Unlink is specifically used for deleting a path name from the file system. If pathname is the last file and further process is done then the entire file is deleted and the memory used for the previous file is again re utilized.

If the name specified with a socket, FIFO FIRST IN FIRST OUT or name for it is detached but procedures which have the object open might continue to utilize it.

Return value

If error is set to -1 it is returned and if it is 0 it is set properly

Server Application APIs

To create a server application using the BSD interface, you must follow these steps:

Form a new socket by typing socket ().

Bind IP addresses and port number to the socket by typing: bind. This step recognizes the server so that the client knows where to go.

New connection requests can be identified and listen on the socket by typing listen ().

Agree to new connections by typing accept ().

http://en.wikipedia.org/wiki/Berkeley_sockets

1. Socket () – create an endpoint for communication

SYNOPSIS

#include

#include

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

DESCRIPTION

In this case a socket is created at end points for communication and then it returns a descriptor file.

Return value if -1 is occurred treated as error and retransmits if 0 it is sent to descriptor.

bind – bind a name to a socket

SYNOPSIS

#include

#include

int bind (int sockfd, struct sockaddr *my_addr, int Addrlen);

DESCRIPTION:

Bind gives the socket sockfd the local address my_addr. My_addr is addrlen. Conventionally, it’s called as “assigning a name to a socket.”

RETURN VALUE:-

On success it returns 0. On error, -1 is returned, and error o is set correctly.

3. Listen () – listen for connections on a socket

SYNOPSIS

#include

int listen(int s, int backlog);

DESCRIPTION

Initially a socket is created by a connection. Listen socket is used for new connection. Once a connection is established a new connection is agreed by a listen socket which retransmits the original protocol

Linux 2.2 changes the nature of the backlog parameter on TCP socket connection. These specifies the length of the queue which is waiting to be agree toed, as an alternative the number of in absolute connection requests. The maximum length of the backlog socket is max_tcp_systl_MSG. backlog parameter is initially agrees a connection. These all parameters comes are the socket parameters.

RETURN VALUE:-

On success it returns 0. On error, -1 is returned, and error o is set correctly.

4. Accept () – agree to a connection on a socket

SYNOPSIS

#include

#include

int agree to(int s, struct sockaddr *addr, int *addrlen);

DESCRIPTION

In this argument the accept socket accepts the connection only from the non-blocking socket. If the sockets are blocked it doesn’t agree the connection it may cause errors. The integer function s is the socket address which is designed to have a socket address. Agree of new connection is based on accept and decline status. This socket creates a new file descriptor for the socket.[17]

CHAPTER 6

NETWORK BYTE ORDERING APIs

The below list shows the different network byte order application program interface (APIs) which are utilized in the application program to convert the internet protocol address from host to the network byte order and from network byte order to host which are understandable.

Synopsis

# include

Unsigned long int htlng (unsigned long int host long);

Unsigned short int htsht (unsigned short int host short);

Unsigned long int ntlng (unsigned long int net long);

Unsigned short int ntsht (unsigned short int net short);

DESCRIPTION

htlng stands for host long this function alters the long integer host from network byte order to the host byte order.

htsht stands for short host which is used for the alteration of short integer host from network byte order to the host byte order and vice versa.

Int ntlng is a unsigned integer it stands for netlong which is aused to alter the long int net long from host byte order to network byte order.

Int ntsht is a unsigned integer net short which also alter from host byte to network byte. This is a unsigned short integer net short.

Least significant byte is classified first on 80 x 86 which is the significant host byte and wherea

 

Cite This Work

To export a reference to this article please select a referencing stye below:

Reference Copied to Clipboard.
Reference Copied to Clipboard.
Reference Copied to Clipboard.
Reference Copied to Clipboard.
Reference Copied to Clipboard.
Reference Copied to Clipboard.
Reference Copied to Clipboard.

Related Services

View all

DMCA / Removal Request

If you are the original writer of this essay and no longer wish to have your work published on UKEssays.com then please: