62
EX NO: 1 DATE: CLIENT-SERVER COMMUNICATION USING TCP SOCKETS AIM: To implement a client-server application using sockets. DESCRIPTION: socket(): SYNOPSIS #include <sys/socket.h> int socket(int domain, int type, int protocol); DESCRIPTION The socket() function shall create an unbound socket in a communications domain, and return a file descriptor that can be used in later function calls that operate on sockets. The socket() function takes the following arguments: domain :Specifies the communications domain in which a socket is to be created. type :Specifies the type of socket to be created. protocol :Specifies a particular protocol to be used with the socket. Specifying a protocol of 0 causes socket() to use an unspecified default protocol appropriate for the requested socket type. The following socket types are defined; implementations may specify additional socket types: SOCK_STREAM :Provides sequenced, reliable, bidirectional, connection-mode byte streams, and may provide a transmission mechanism for out-of-band data. SOCK_DGRAM :Provides datagrams, which are connectionless-mode, unreliable messages of fixed maximum length. SOCK_SEQPACKET :Provides sequenced, reliable, bidirectional, connection- mode transmission paths for records. A record can be sent using one or more output operations and received using one or more input operations, but a single operation never transfers part of more than one record

cs Networks Rec 1 - 10

Embed Size (px)

DESCRIPTION

BE CSE 5th semester networks lab record

Citation preview

EX NO: 1

DATE:

CLIENT-SERVER COMMUNICATION USING TCP SOCKETS

AIM:

To implement a client-server application using sockets.

DESCRIPTION:

socket():

SYNOPSIS

#include <sys/socket.h>

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

DESCRIPTION

The socket() function shall create an unbound socket in a communications

domain, and return a file descriptor that can be used in later function calls that operate

on sockets.

The socket() function takes the following arguments:

domain :Specifies the communications domain in which a socket is to be created.

type :Specifies the type of socket to be created.

protocol :Specifies a particular protocol to be used with the socket. Specifying a

protocol of 0 causes socket() to use an unspecified default protocol appropriate

for the requested socket type.

The following socket types are defined; implementations may specify additional

socket types:

SOCK_STREAM :Provides sequenced, reliable, bidirectional, connection-mode

byte streams, and may provide a transmission mechanism for out-of-band data.

SOCK_DGRAM :Provides datagrams, which are connectionless-mode, unreliable

messages of fixed maximum length.

SOCK_SEQPACKET :Provides sequenced, reliable, bidirectional, connection-

mode transmission paths for records. A record can be sent using one or more

output operations and received using one or more input operations, but a single

operation never transfers part of more than one record

If the protocol argument is non-zero, it shall specify a protocol that is supported

by the address family. If the protocol argument is zero, the default protocol for this

address family and type shall be used. The protocols supported by the system are

implementation-defined.

RETURN VALUE

Upon successful completion, socket() shall return a non-negative integer, the

socket file descriptor. Otherwise, a value of -1 shall be returned and errno set to

indicate the error.

bind():

SYNOPSIS

#include <sys/socket.h>

int bind(int socket, const struct sockaddr *address, socklen_t address_len);

DESCRIPTION

The bind() function shall assign a local socket address address to a socket

identified by descriptor socket that has no local socket address assigned. Sockets

created with the socket() function are initially unnamed; they are identified only by

their address family.

The bind() function takes the following arguments:

socket :Specifies the file descriptor of the socket to be bound.

address :Points to a sockaddr structure containing the address to be bound to the

socket. The length and format of the address depend on the address family of the

socket.

address_len :Specifies the length of the sockaddr structure pointed to by the

address argument.

RETURN VALUE

Upon successful completion, bind() shall return 0; otherwise, -1 shall be returned

and errno set to indicate the error.

listen():

SYNOPSIS

#include <sys/socket.h>

int listen(int socket, int backlog);

DESCRIPTION

The listen() function shall mark a connection-mode socket, specified by the

socket argument, as accepting connections.

The backlog argument provides a hint to the implementation which the

implementation shall use to limit the number of outstanding connections in the socket's

listen queue. The implementation may include incomplete connections in its listen

queue. The limits on the number of incomplete connections and completed connections

queued may be different.

If listen() is called with a backlog argument value that is less than 0, the function

behaves as if it had been called with a backlog argument value of 0.

A backlog argument of 0 may allow the socket to accept connections, in which

case the length of the listen queue may be set to an implementation-defined minimum

value.

RETURN VALUE

Upon successful completions, listen() shall return 0; otherwise, -1 shall be

returned and errno set to indicate the error.

accept():

SYNOPSIS

#include <sys/socket.h>

int accept(int socket, struct sockaddr *restrict address, socklen_t *restrict

address_len);

DESCRIPTION

The accept() function shall extract the first connection on the queue of pending

connections, create a new socket with the same socket type protocol and address family

as the specified socket, and allocate a new file descriptor for that socket.

The accept() function takes the following arguments:

socket :Specifies a socket that was created with socket(), has been bound to an

address with bind(), and has issued a successful call to listen().

address :Either a null pointer, or a pointer to a sockaddr structure where the

address of the connecting socket shall be returned.

address_len :Points to a socklen_t structure which on input specifies the length

of the supplied sockaddr structure, and on output specifies the length of the

stored address.

If address is not a null pointer, the address of the peer for the accepted connection

shall be stored in the sockaddr structure pointed to by address, and the length of this

address shall be stored in the object pointed to by address_len.

The accepted socket cannot itself accept more connections. The original socket

remains open and can accept more connections.

RETURN VALUE

Upon successful completion, accept() shall return the non-negative file descriptor

of the accepted socket. Otherwise, -1 shall be returned and errno set to indicate

the error.

connect():

SYNOPSIS

#include <sys/socket.h>

int connect(int socket, const struct sockaddr *address,socklen_t address_len);

DESCRIPTION

The connect() function shall attempt to make a connection on a socket. The

function takes the following arguments:

socket :Specifies the file descriptor associated with the socket.

address :Points to a sockaddr structure containing the peer address. The length

and format of the address depend on the address family of the socket.

address_len :Specifies the length of the sockaddr structure pointed to by the

address argument.

If the socket has not already been bound to a local address, connect() shall bind it

to an address which, unless the socket's address family is AF_UNIX, is an unused local

address.

When the connection has been established asynchronously, select() and poll()

shall indicate that the file descriptor for the socket is ready for writing.

RETURN VALUE

Upon successful completion, connect() shall return 0; otherwise, -1 shall be

returned and errno set to indicate the error.

recv():

SYNOPSIS

#include <sys/socket.h>

ssize_t recv(int socket, void *buffer, size_t length, int flags);

DESCRIPTION

The recv() function shall receive a message from a connection-mode or

connectionless-mode socket. It is normally used with connected sockets because it does

not permit the application to retrieve the source address of received data.

The recv() function takes the following arguments:

socket :Specifies the socket file descriptor.

buffer :Points to a buffer where the message should be stored.

length :Specifies the length in bytes of the buffer pointed to by the buffer

argument.

flags :Specifies the type of message reception.

The recv() function shall return the length of the message written to the buffer

pointed to by the buffer argument. For message-based sockets, such as

SOCK_DGRAM and SOCK_SEQPACKET, the entire message shall be read in a

single operation. If a message is too long to fit in the supplied buffer, and

MSG_PEEK is not set in the flags argument, the excess bytes shall be discarded.

For stream-based sockets, such as SOCK_STREAM, message boundaries shall be

ignored. In this case, data shall be returned to the user as soon as it becomes

available, and no data shall be discarded.

RETURN VALUE

Upon successful completion, recv() shall return the length of the message in

bytes. If no messages are available to be received and the peer has performed an orderly

shutdown, recv() shall return 0. Otherwise, -1 shall be returned and errno set to indicate

the error.

send():

SYNOPSIS

#include <sys/socket.h>

ssize_t send(int socket, const void *buffer, size_t length, int flags);

DESCRIPTION

The send() function shall initiate transmission of a message from the specified

socket to its peer. The send() function shall send a message only when the socket is

connected (including when the peer of a connectionless socket has been set via

connect()).

The send() function takes the following arguments:

socket :Specifies the socket file descriptor.

buffer :Points to the buffer containing the message to send.

length :Specifies the length of the message in bytes.

flags :Specifies the type of message transmission.

The length of the message to be sent is specified by the length argument. If the

message is too long to pass through the underlying protocol, send() shall fail and no

data shall be transmitted.

RETURN VALUE

Upon successful completion, send() shall return the number of bytes sent.

Otherwise, -1 shall be returned and errno set to indicate the error.

OTHER FUNCTIONS:

SYNOPSIS

#include <arpa/inet.h>

uint32_t htonl(uint32_t hostlong);

uint16_t htons(uint16_t hostshort);

uint32_t ntohl(uint32_t netlong);

uint16_t ntohs(uint16_t netshort);

DESCRIPTION

These functions shall convert 16-bit and 32-bit quantities between network byte

order and host byte order.

On some implementations, these functions are defined as macros. The uint32_t

and uint16_t types are defined in <inttypes.h>.

RETURN VALUE

The htonl() and htons() functions shall return the argument value converted from

host to network byte order.

The ntohl() and ntohs() functions shall return the argument value converted from

network to host byte order.

SYNOPSIS

#include <arpa/inet.h>

const char *inet_ntop(int af, const void *restrict src,

char *restrict dst, socklen_t size);

int inet_pton(int af, const char *restrict src, void *restrict dst);

DESCRIPTION

The inet_ntop() function shall convert a numeric address into a text string

suitable for presentation.

The inet_pton() function shall convert an address in its standard text presentation

form into its numeric binary form.

RETURN VALUE

The inet_ntop() function shall return a pointer to the buffer containing the text

string if the conversion succeeds, and NULL otherwise, and set errno to indicate the

error.

The inet_pton() function shall return 1 if the conversion succeeds, with the

address pointed to by dst in network byte order. It shall return 0 if the input is not a

valid IPv4 dotted-decimal string or a valid IPv6 address string.

ALGORITHM:

SERVER:

1. Create a server socket and bind it to port.

2. Listen for new connection and when a connection arrives, accept it.

3. Read client’s message and display it.

4. Get a message from the user and send it to the client.

5. Close all streams.

6. Close the server and client socket.

7. Stop.

CLIENT:

1. Create a client socket and connect it to the server’s port number.

2. Get a message from the user and send it to the server.

3. Read server’s responses and display them.

4. Close all input/output streams.

5. Close the client socket.

6. Stop.

PROGRAM:

SERVER:

#include <iostream>

#include <stdio.h>

#include <sys/socket.h>

#include <netinet/in.h>

#include <stdlib.h>

#include <unistd.h>

#include <netdb.h>

#include <string.h>

#include <arpa/inet.h>

#include <sys/types.h>

#define MAX 100

#define LEN 81

int cwork(int socketclient);

int main()

{

int socketmain,socketclient,child,port=4000;

struct sockaddr_in serv,clientaddr;

socklen_t clientlen;

char str[100];

int res;

if((socketmain=socket(AF_INET,SOCK_STREAM,0))<0)

{

std::cout<<"server cannot open socket"<<std::endl;

exit(0);

}

bzero(&serv,sizeof(serv));

serv.sin_family=AF_INET;

serv.sin_addr.s_addr=htonl(INADDR_ANY);

serv.sin_port=htons(port);

if((bind(socketmain,(struct sockaddr *)&serv,sizeof(serv)))<0)

{

std::cout<<"server bind failed\n"<<std::endl;

exit(1);

}

listen(socketmain,5);

for(;;)

{

if((socketclient=accept(socketmain,(struct sockaddr *)&clientaddr,&clientlen))<0)

{

std::cout<<"client is bad\n";

exit(0);

}

char x[50];

inet_ntop(AF_INET,&clientaddr.sin_addr,x,sizeof(x));

std::cout<<"Receiving message:client IP:"<<x<<"at port no:"<<clientaddr.sin_port<<"\n";

if((child==fork())<0)

{

std::cout<<"Creation of child failed";

exit(1);

}

close(socketmain);

cwork(socketclient);

exit(0);

}

}

int cwork(int socketclient)

{

char buff[LEN];

int msglen;

bzero(buff,LEN);

if((msglen=recv(socketclient,buff,LEN,0))<0)

{

std::cout<<"error receiving";

exit(1);

}

std::cout<<"socket used"<<socketclient<<"\n";

std::cout<<"Message:"<<buff<<"\n";

strcpy(buff,"bye!");

send(socketclient,buff,strlen(buff),0);

}

CLIENT:

#include <iostream>

#include <stdio.h>

#include <sys/socket.h>

#include <netinet/in.h>

#include <stdlib.h>

#include <unistd.h>

#include <netdb.h>

#include <string.h>

#include <arpa/inet.h>

#include <sys/types.h>

int main(int argc,char *argv[])

{

int sockid,n;

struct sockaddr_in serv;

char str[100];

if(argc!=3)

{

std::cout<<"ARgument error";

exit(0);

}

if ((sockid=socket(AF_INET,SOCK_STREAM,0))<0)

{

std::cout<<"socket not created\n";

exit(0);

}

bzero(&serv,sizeof(serv));

serv.sin_family=AF_INET;

serv.sin_port=htons(atoi(argv[2]));

if((inet_pton(AF_INET,argv[1],&serv.sin_addr))<0){

std::cout<<"connect fail";

exit(0);

}

if(connect(sockid,(struct sockaddr *)&serv,sizeof(serv))<0){

std::cout<<"connect fail";

exit(0); }

std::cout<<"connected\n";

std::cout<<"Enter message\n";

std::cin>>str;

write(sockid,str,106);

std::cout<<"From server\n"<<str<<"\n";

}

OUTPUT:

./server

Receiving message:client IP:0.0.0.0at port no:96

socket used4

Message:hi

./cli 127.0.0.1 4000

Client:hi

Server:bye!

RESULT:

Client-Server communication was implemented successfully using TCP sockets.

Ex.no: 2 CLIENT-SERVER CHAT USING TCP SOCKETS

Date:

AIM:

To establish a connection between server and client using TCP and transfer data between

them.

ALGORITHM:

SERVER:

1. Start

2. Inside the main function,create an instance serv of the sockaddr_in structure.

3. Create a socket using the socket() function.Pass the domain as AF_INET to indicate

we intend to use socket to connect to the internet (IPV4 protocol).the type as

SOCK_STREAM to indicate stream socket.The handle for identifying the socket that

is returned by this function is stored in the variable,sockmain.n

4. Set the value of the address structure as zero using the bzero() function.Initialise the

family as AF_INET,the IP address using htonl() functionand the port using htons()

function.

5. Use the bind() function to find the socket to the server address.

6. Use the listen() function to define how many connections can be pending on the

socket.

7. Usethe accept() function to carry out the passive open.

8. Fork a child.Close the child server side socket and call the user defined function

cwork().

9. Then close the client side socket.Finally close the parent server side socket.

10. Stop.

CLIENT:

1. Check if the number of arguments passed is equal to three(ie ./a.out,IP address,port

no).

2. Create a socket using socket() function with family as AF_INET,type as

SOCK_STREAM.

3. Set the values of the address structure as zero using the bzero() function.Then

initialise the family as AF_INET andthe port no as that passed as argument.

4. Convert the IP address passed as argument from string to numeric using the

inet_pton() function.

5. Use the connect() function to set up a connection with the server. The address

parameter refers to the remote address.

6. Obtain a message entered bythe user with the help of gets() function and write the

message using write() function.

7. Using the read() function,read the message from the server and print it.

PROGRAM:

SERVER:

#include<iostream>

#include<stdio.h>

#include<sys/socket.h>

#include<netinet/in.h>

#include<stdlib.h>

#include<unistd.h>

#include<netdb.h>

#include<string.h>

#include<sys/types.h>

#include<arpa/inet.h>

using namespace std;

#define MAX 100

#define LEN 81

int cwork(int socketclient);

main(){

int socketmain,socketclient,child,port=4000;

struct sockaddr_in serv,clientaddr;

socklen_t clientlen;

char str[100];

int res;

if ( ( socketmain=socket( AF_INET,SOCK_STREAM,0 ) ) < 0 ){

std::cout<<"server cannot open socket"<<std::endl;

exit(1);

}

bzero(&serv,sizeof(serv));

serv.sin_family=AF_INET;

serv.sin_addr.s_addr=htonl(INADDR_ANY);

serv.sin_port=htons(port);

if((bind(socketmain,(struct sockaddr *)&serv,sizeof(serv)) ) < 0 ){

cout<<"server bind failed";

exit(1);

}

listen(socketmain,5);

for(;;){

if((socketclient=accept(socketmain,(struct sockaddr *)&clientaddr, &clientlen ) ) < 0 )

{

cout<<"client is bad"<<endl;

exit(0);

}

char x[50];

inet_ntop(AF_INET,&clientaddr.sin_addr,x,sizeof(x) );

cout<<"receiving message from client......client IP:"<<x<<"at port

number:"<<clientaddr.sin_port<<"\n";

if((child==fork())<0){

cout<<"creation of child failed";

exit(1);}

close(socketmain);

cwork(socketclient);

exit(0);

}}

int cwork(int socketclient){

char buff[LEN];

int msglen;

cout<<"socket used"<<socketclient<<"\n";

do{

bzero(buff,LEN);

if((msglen=recv(socketclient,buff,LEN,0 ) ) < 0 ){

cout<<"error recieving";

exit(1);

}

std::cout<<"client:"<<buff<<"\n";

bzero(buff,LEN);

puts("Server:");

gets(buff);

send(socketclient,buff,strlen(buff),0);

}while(strcmp(buff,"bye")!=0);

}

CLIENT:

#include<iostream>

#include<stdio.h>

#include<sys/socket.h>

#include<netinet/in.h>

#include<stdlib.h>

#include<unistd.h>

#include<netdb.h>

#include<string.h>

#include<sys/types.h>

#include<arpa/inet.h>

using namespace std;

main(int argc,char* argv[])

{

int sockid,n;

struct sockaddr_in serv;

char str[100];

if(argc!=3){

cout<<"argument error";

exit(0);

}

if((sockid=socket(AF_INET,SOCK_STREAM,0))<0){

cout<<"socket not created";

exit(0);

}

bzero(&serv,sizeof(serv));

serv.sin_family=AF_INET;

serv.sin_port=htons(atoi(argv[2]));

if((inet_pton(AF_INET,argv[1],&serv.sin_addr))<0){

cout<<"connect fail";

exit(0);

}

if(connect(sockid,(struct sockaddr *)&serv,sizeof(serv))<0){

cout<<"connect fail";

exit(0);}

cout<<"connected\n";

do{

cout<<"Client:";

gets(str);

send(sockid,str,strlen(str),0);

recv(sockid,str,strlen(str),0);

cout<<"Server"<<str<<"\n";

}while(strcmp(str,"bye")!=0); }

OUTPUT:

SERVER:

Socket used

Message from client:

Hello

Enter message:

Hi

Message from client:

Bye

CLIENT:

Connected

Enter message:

Hello

From server:

Hi

Enter message:

Bye

RESULT:

Thus the Client-Server chat application was implemented successfully using TCP sockets.

Ex.no:3

Date:

IMPLEMENTATION ON DAYTIME TCP CLIENT-SERVER

AIM:

To implement a day time TCP client server.

DESCRIPTION:

Variable :

time_t ticks: Here time_t is a datatype for storing system time values ticks=time(NULL);

Functions:

Time(NULL) is a function that returns time in the form of seconds to the variable

ticks.

Snprintf();

Protype:

Int snprintf(char * buffer,Size_t n,const char * format,…);

In this program,

Snprintf(buff,sizeof(buff),”%24s/r\n”,ctime(&ticks));

It prints the character or contents of the buffer

It returns the no of characters written ..

-1 is returned in case of errors..

Ctime()

Prototype:

Ctime(time_t );

It converts the time pointed to by clock , to local time in the form of a string….

ALGORITHM:

SERVER:

1. Create a socket in the server side and initialize the family as AF_INET.

2. Convert the port no from host byte order to network byte order and initialize the port

no with it.

3. Pass the reference toclient socket address structure and the length of client address

structureas second and third arguments to accept.

4. Obtain the IP address of the client,convert the binary network byte ordered IPV4

address into its corresponding dotted decimal string and then print it.

5. Similarly obtain the port no of the client,convert the network byte ordered port no into

host byte ordered and then print it.

6. Print the current time and date using time function and convert it to string format

using ctime function.

CLIENT:

1. Create a socket. Initialize the family as AF_INET and type as SOCK_STREAM.

2. Initialize the port number.

3. Set up a connection with the server.

4. Read the message from the server and print it.

PROGRAM:

Server:

#include<iostream>

#include<stdio.h>

#include<sys/socket.h>

#include<netinet/in.h>

#include<stdlib.h>

#include<unistd.h>

#include<netdb.h>

#include<string.h>

#include<arpa/inet.h>

#include<sys/types.h>

#define MAX 100

#define LEN 81

using namespace std;

int cwork(int socketclient);

int main()

{

int socketmain,socketclient,child,port=4000;

struct sockaddr_in serv,clientaddr;

socklen_t clientlen;

char str[100];

int res;

if((socketmain=socket(AF_INET,SOCK_STREAM,0))<0)

{

std::cout<<"server cannot open socket"<<std::endl;

exit(0);

}

bzero(&serv,sizeof(serv));

serv.sin_family=AF_INET;

serv.sin_addr.s_addr=htonl(INADDR_ANY);

serv.sin_port=htons(port);

if((bind(socketmain,(struct sockaddr *)&serv,sizeof(serv)))<0)

{

std::cout<<"server bind failed\n"<<std::endl;

exit(1);

}

listen(socketmain,5);

for(;;)

{

if((socketclient=accept(socketmain,(struct sockaddr *)&clientaddr,&clientlen))<0)

{

std::cout<<"client is bad\n";

exit(0);

}

char x[50];

inet_ntop(AF_INET,&clientaddr.sin_addr,x,sizeof(x));

std::cout<<"recieving message:client IP:"<<x<<"at port no:"<<clientaddr.sin_port<<"\n";

if((child==fork())<0)

{

std::cout<<"creation of child failed"<<std::endl;

exit(1);

}

else if(child==0)

{

close(socketmain);

cwork(socketclient);

exit(0);

}

close(socketclient);

exit(0);

}

}

int cwork(int socketclient)

{

char buff[LEN];

int msglen;

time_t ticks;

ticks=time(NULL);

bzero(buff,LEN);

if((msglen=recv(socketclient,buff,LEN,0))<0)

{

std::cout<<"error recieving";

exit(1);

}

std::cout<<"socket used"<<socketclient<<"\n";

std::cout<<"message:"<<buff<<"\n";

strcpy(buff,"bye!");

snprintf(buff,sizeof(buff),"%.24s\r\n",ctime(&ticks));

send(socketclient,buff,strlen(buff),0);

}

Client:

#include<iostream>

#include<stdio.h>

#include<netinet/in.h>

#include<unistd.h>

#include<netdb.h>

#include<string.h>

#include<arpa/inet.h>

#include<sys/types.h>

#include<stdlib.h>

using namespace std;

int main(int argc,char *argv[])

{

int sockid,n;

struct sockaddr_in serv;

char str[100];

if(argc!=3)

{

std::cout<<"argument error";

exit(0);

}

if((sockid=socket(AF_INET,SOCK_STREAM,0))<0)

{

std::cout<<"socket not created\n";

exit(0);

}

bzero(&serv,sizeof(serv));

serv.sin_family=AF_INET;

serv.sin_port=htons(atoi(argv[2]));

if((inet_pton(AF_INET,argv[1],&serv.sin_addr))<0)

{

std::cout<<"connect fail";

exit(0);

}

if(connect(sockid,(struct sockaddr *)&serv,sizeof(serv))<0)

{

std::cout<<"connect fail";

exit(0);

}

std::cout<<"connected\n";

std::cout<<"enter message\n";

std::cin>>str;

write(sockid,str,106);

int r=read(sockid,str,100);

str[r]='\0';

std::cout<<"from server"<<str<<std::endl;

}

OUTPUT:

Client:

Compile:c++ dtimeclient.cpp

Run:./a.out 127.0.0.1 4000

Connected

Enter message

hi!

From server tue jan 1 02:01:43 2002

Server:

Compile:c++ dtimeserver.cpp

Run:./a.out

receiving message client IP:60.0.0.0 at port no:57376

sockt used 4

message:hi!

RESULT:

Thus the daytime TCP client-server was implemented successfully.

Ex.no: 4

Date:

IMPLEMENTATION OF UDP CLIENT SERVER USING

UDP SOCKET

AIM:

To implement UDP client-server using udp socket.

DESCRIPTION:

1)recvfrom:-

#include<sys.socket.h>

ssize_t recvfrom(int sockfd,void*buff,size_t nbytes,int flags,struct sockaddr

*from,socklen_t *addrlen);

sockfd-descriptor

buff-pointer to buffer to read into and write from

nbytes-number of bytes to read and write

flags-set to 0

addrlen-The number of bytes stored in the socket address structure is returned to the

caller in the integer pointed to by addrlen.

2)sendto:-

#include<sys.socket.h>

ssize_t sendto(int sockfd,const void*buff,size_t nbytes,int flags,const struct sockaddr

*to,socklen_t *addrlen);

sockfd-descriptor

buff-pointer to buffer to read into and write from

nbytes-number of bytes to read and write

flags-set to 0

to-Is a socket address structur containing the protocol addrss of where the data is to

be sent.

ALGORITHM:

SERVER:

1. Create a socket with address family AF_INET,type SOCK_DGRAM and default

protocol.

2. Initialize the socket and set its attributes.Assign aspecific port number as desired.

3. Bind the server to the socket using bind() function.

4. Listen for the client request,establish a connection using accept function.

5. Display the client message.

6. Similarly send message from server to client.

7. Terminate or close the connection.

CLIENT:

1. Create a socket with address family AF_INET and default protocol.

2. Initialize the socket and set its attributes.

3. Connect the server to the socket using connect() function to initialize the request.

4. Similarly accept message from server to client.

5. Terminate or close the connection.

PROGRAM:

CLIENT:

#include<iostream>

#include<stdio.h>

#include<sys/socket.h>

#include<netinet/in.h>

#include<unistd.h>

#include<netdb.h>

#include<string.h>

#include<arpa/inet.h>

#include<sys/types.h>

#include<stdlib.h>

using namespace std;

int main(int argc,char *argv[])

{

int sockid;

struct sockaddr_in serv;

char str[100];

ssize_t n;

socklen_t len;

char sendline[20];

char recvline[20];

if(argc!=3)

{

std::cout<<"argument error";

exit(0);

}

if((sockid=socket(AF_INET,SOCK_DGRAM,0))<0)

{

std::cout<<"socket not created\n";

exit(0);

}

bzero(&serv,sizeof(serv));

serv.sin_family=AF_INET;

serv.sin_port=htons(atoi(argv[2]));

if((inet_pton(AF_INET,argv[1],&serv.sin_addr))<0)

{

std::cout<<"connect fail";

exit(0);

}

cout<<"Enter the message to server";

scanf("%s",sendline);

len=sizeof(serv);

sendto(sockid,sendline,20,0,(struct sockaddr *)&serv,len);

n=recvfrom(sockid,recvline,20,0,NULL,NULL);

recvline[n]='\0';

cout<<recvline;

}

SERVER:

#include<iostream>

#include<sys/socket.h>

#include<netinet/in.h>

#include<stdlib.h>

#include<unistd.h>

#include<netdb.h>

#include<string.h>

#include<arpa/inet.h>

#include<sys/types.h>

#define MAX 100

#define LEN 81

using namespace std;

int cwork(int socketclient);

int main()

{

int socketmain,socketclient,child,port=4000;

struct sockaddr_in serv,clientaddr;

socklen_t clientlen;

char str[100];

int res;

if((socketmain=socket(AF_INET,SOCK_DGRAM,0))<0)

{

std::cout<<"server cannot open socket"<<std::endl;

exit(0);

}

bzero(&serv,sizeof(serv));

serv.sin_family=AF_INET;

serv.sin_addr.s_addr=htonl(INADDR_ANY);

serv.sin_port=htons(port);

if((bind(socketmain,(struct sockaddr *)&serv,sizeof(serv)))<0)

{

std::cout<<"server bind failed\n"<<std::endl;

exit(1);

}

listen(socketmain,5);

for(;;)

{

ssize_t n;socklen_t len;

char msg[20];

len=sizeof(serv);

n=recvfrom(socketmain,msg,20,0,(struct sockaddr *)&serv,&len);

msg[n]='\0';

cout<<msg;

cin>>msg;

sendto(socketmain,msg,20,0,(struct sockaddr *)&serv,len);

}

}

OUTPUT:

Client:

Compile:c++ udpclient.cpp

Run:./a.out 127.0.0.1 4000

Connected

Enter message

hi!

Server:

Compile:c++ udpserver.cpp

Run:./a.out

hi!

RESULT:

Thus the UDP client server was implemented successfully using UDP sockets.

EXNO:5 CALCULATION OF ROUND TRIP TIME DELAY

DATE: IN TCP COMMUNICATION

AIM:

To calculate the round time taken for a message to be sent from the client to the

Server and theacknowledgement received.

ALGORITHM:

SERVER:

1.Create a server socket and bind it to port.

2.Listen for a new connection and when communication arrives accept it.

3.Read client’s message and display.

4.Get a message from user and send to client.

5.Close all streams.

6.Close the server and client socket.

CLIENT:

1.Create a client socket and connect it to the server’s port number.

2.Create two structure variable start and end of time val structure.

3.Get the current system time and date.

4.Read a message from user and send to server.

5.Read server’s response and display.

6.Get system time and design.

7.subtract end time from start time.

8.Display round trip time elapsed.

9.Close all streams.

10.Close client socket

PROGRAM:

SERVER:

#include <iostream>

#include <stdio.h>

#include <sys/socket.h>

#include <netinet/in.h>

#include <stdlib.h>

#include <unistd.h>

#include <netdb.h>

#include <string.h>

#include <arpa/inet.h>

#include <sys/types.h>

#define MAX 100

#define LEN 81

int cwork(int socketclient);

int main()

{

int socketmain,socketclient,child,port=4000;

struct sockaddr_in serv,clientaddr;

socklen_t clientlen;

char str[100];

int res;

if((socketmain=socket(AF_INET,SOCK_STREAM,0))<0)

{

std::cout<<"server cannot open socket"<<std::endl;

exit(0);

}

bzero(&serv,sizeof(serv));

serv.sin_family=AF_INET;

serv.sin_addr.s_addr=htonl(INADDR_ANY);

serv.sin_port=htons(port);

if((bind(socketmain,(struct sockaddr *)&serv,sizeof(serv)))<0)

{

std::cout<<"server bind failed\n"<<std::endl;

exit(1);

}

listen(socketmain,5);

for(;;)

{

if((socketclient=accept(socketmain,(struct sockaddr *)&clientaddr,&clientlen))<0)

{

std::cout<<"client is bad\n";

exit(0);

}

char x[50];

inet_ntop(AF_INET,&clientaddr.sin_addr,x,sizeof(x));

std::cout<<"Receiving message:client IP:"<<x<<"at port no:"<<clientaddr.sin_port<<"\n";

if((child==fork())<0)

{

std::cout<<"Creation of child failed";

exit(1);

}

close(socketmain);

cwork(socketclient);

exit(0);

}

}

int cwork(int socketclient)

{

char buff[LEN];

int msglen;

bzero(buff,LEN);

if((msglen=recv(socketclient,buff,LEN,0))<0)

{

std::cout<<"error receiving";

exit(1);

}

std::cout<<"socket used"<<socketclient<<"\n";

std::cout<<"Message:"<<buff<<"\n";

strcpy(buff,"bye!");

send(socketclient,buff,strlen(buff),0);

}

CLIENT:

#include<iostream>

#include<stdio.h>

#include<sys/socket.h>

#include<netinet/in.h>

#include<stdlib.h>

#include<unistd.h>

#include<netdb.h>

#include<string.h>

#include<sys/types.h>

#include<arpa/inet.h>

#include<sys/time.h>

using namespace std;

main(int argc,char* argv[])

{

int sockid,n,x,y;

struct sockaddr_in serv;

struct timeval start,end;

long int tv_usec,tv_sec;

char str[100];

if(argc!=3)

{

cout<<"argument error";

exit(0);

}

if((sockid=socket(AF_INET,SOCK_STREAM,0))<0)

{

cout<<"socket not created";

exit(0);

}

bzero(&serv,sizeof(serv));

serv.sin_family=AF_INET;

serv.sin_port=htons(atoi(argv[2]));

if((inet_pton(AF_INET,argv[1],&serv.sin_addr))<0)

{

cout<<"connect fail";

exit(0);

}

if(connect(sockid,(struct sockaddr *)&serv,sizeof(serv))<0)

{

cout<<"connect fail";

exit(0);

}

cout<<"connected\n";

// do{

cout<<"Client:";

gets(str);

gettimeofday(&start, NULL );

x=start.tv_usec+(1000000*tv_sec);

send(sockid,str,strlen(str),0);

// bzero(str,strlen(str));

recv(sockid,str,strlen(str),0);

gettimeofday(&end, NULL );

cout<<"Server"<<str<<"\n";

y=end.tv_usec+(1000000*tv_sec);

y-=x;

cout<<"microsec"<<y<<endl<<"seconds"<<y/1000000;

// }while(strcmp(str,"bye")!=0);

}

OUTPUT:

SERVER:

Receiving message

Socket used4

Message:hello

CLIENT:

Connected

Client: hello

Server:bye

Microsec:659

Seconds:0.000659

RESULT:

The round trip time taken for a message to be sent from client to server was

calculated.

EXNO:6 FILE TRANSFER USING TCP SOCKETS

DATE:

AIM:

To transfer a file from server to client using suitable FTP methods.

ALGORITHM:

SERVER:

1.Create a new server socket and bind to port.

2.Listen for a new connection and accept it.

3.Get filename from client.

4.Using sscsnf(),obtain file contents line by line and store it in buffer.

5.Send buffer string to client.

6.Repeat steps 4 and 5 till all contents are read.

7.Close all streams.

CLIENT:

1.Create a client socket and connect to servers port no.

2.Send the filename to server.

3.Receive the contents line after line and write it to output file.

4.Follow step3, till entire file is transferred from server side.

5.Close all streams.

PROGRAM:

SERVER:

#include<iostream>

#include<stdio.h>

#include<sys/socket.h>

#include<netinet/in.h>

#include<stdlib.h>

#include<unistd.h>

#include<netdb.h>

#include<string.h>

#include<sys/types.h>

#include<arpa/inet.h>

using namespace std;

#define MAX 100

#define LEN 81

int cwork(int socketclient);

main()

{

int socketmain,socketclient,child,port=4000;

struct sockaddr_in serv,clientaddr;

socklen_t clientlen;

char str[100];

int res;

if ( ( socketmain=socket( AF_INET,SOCK_STREAM,0 ) ) < 0 )

{

std::cout<<"server cannot open socket"<<std::endl;

exit(1);

}

bzero(&serv,sizeof(serv));

serv.sin_family=AF_INET;

serv.sin_addr.s_addr=htonl(INADDR_ANY);

serv.sin_port=htons(port);

if((bind(socketmain,(struct sockaddr *)&serv,sizeof(serv)) ) < 0 )

{

cout<<"server bind failed";

exit(1);

}

listen(socketmain,5);

for(;;)

{

if((socketclient=accept(socketmain,(struct sockaddr *)&clientaddr, &clientlen ) ) <

0 )

{

cout<<"client is bad"<<endl;

exit(0);

}

char x[50];

inet_ntop(AF_INET,&clientaddr.sin_addr,x,sizeof(x) );

cout<<"receiving message from client......client IP:"<<x<<"at port

number:"<<clientaddr.sin_port<<"\n";

if((child==fork())<0)

{

cout<<"creation of child failed";

exit(1);

}

close(socketmain);

cwork(socketclient);

exit(0);

}

}

int cwork(int socketclient)

{

FILE *fp1;

char buff[LEN],s[LEN];

int msglen;

size_t length;

cout<<"socket used"<<socketclient<<"\n";

bzero(buff,LEN);

if((msglen=recv(socketclient,buff,LEN,0 ) ) < 0 )

{

cout<<"error recieving";

exit(1);

}

fp1=fopen(buff,"r");

fgets(s,40,fp1);

while(!feof(fp1))

{

sscanf(s,"%s",buff);

send(socketclient,buff,strlen(buff),0);

}

}

CLIENT:

#include<iostream>

#include<stdio.h>

#include<sys/socket.h>

#include<netinet/in.h>

#include<stdlib.h>

#include<unistd.h>

#include<netdb.h>

#include<string.h>

#include<sys/types.h>

#include<arpa/inet.h>

using namespace std;

main(int argc,char* argv[])

{

FILE *fp2;

int sockid,n;

struct sockaddr_in serv;

char str[100];

if(argc!=3)

{

cout<<"argument error";

exit(0);

}

if((sockid=socket(AF_INET,SOCK_STREAM,0))<0)

{

cout<<"socket not created";

exit(0);

}

bzero(&serv,sizeof(serv));

serv.sin_family=AF_INET;

serv.sin_port=htons(atoi(argv[2]));

if((inet_pton(AF_INET,argv[1],&serv.sin_addr))<0)

{

cout<<"connect fail";

exit(0);

}

if(connect(sockid,(struct sockaddr *)&serv,sizeof(serv))<0)

{

cout<<"connect fail";

exit(0);

}

cout<<"connected\n";

cout<<"enter the file name";

gets(str);

send(sockid,str,strlen(str),0);

while(recv(sockid,str,strlen(str),0)>0)

{

str[n]='\0';

fprintf(fp2,"%s",str);

}

}

OUTPUT:

SERVER:

Message:hi

Sending file: example.txt

CLIENT:

Requesting file:example.txt

File written:output.txt

Example.txt:

Hi

Hello

Working

OUTPUT.txt:

Hi

Hello

Working

Result :

File transfer has been perfiormed from server to client using FTP methods.

EXNO:7 RECORD RETRIEVAL FROM FILE

DATE:

AIM:

To retrieve and send a record from a file from server side to client side.

ALGORITHM:

SERVER:

1.Create server socket and bind to port.

2.Listen for new connection and accept it.

3.Get record id from client.

4.Search file where records are stored.If search id,matches any of record id,go to

step6.

5.If no id matches report as not found,go to step 7.

6.Send record object to the client.

7.close all streams.

CLIENT:

1.Create a client socket and connect to servers port.

2.Send record it to server.

3.If a record was successfully sent by server go to step 5,else go to step4.

4.If no record exists, print incorrect id,go to step6.

5.Display the contents of the record.

6.Close all streams.

PROGRAM:

SERVER:

#include<iostream>

#include<stdio.h>

#include<sys/socket.h>

#include<netinet/in.h>

#include<stdlib.h>

#include<unistd.h>

#include<netdb.h>

#include<string.h>

#include<sys/types.h>

#include<arpa/inet.h>

using namespace std;

#define MAX 100

#define LEN 81

struct record

{

char name[20];

char dept[10];

int id;

}rec;

int cwork(int socketclient);

main()

{

int socketmain,socketclient,child,port=5000;

struct sockaddr_in serv,clientaddr;

socklen_t clientlen;

char str[100];

int res;

if ( ( socketmain=socket( AF_INET,SOCK_STREAM,0 ) ) < 0 )

{

std::cout<<"server cannot open socket"<<std::endl;

exit(1);

}

bzero(&serv,sizeof(serv));

serv.sin_family=AF_INET;

serv.sin_addr.s_addr=htonl(INADDR_ANY);

serv.sin_port=htons(port);

if((bind(socketmain,(struct sockaddr *)&serv,sizeof(serv)) ) < 0 )

{

cout<<"server bind failed";

exit(1);

}

listen(socketmain,5);

for(;;)

{

if((socketclient=accept(socketmain,(struct sockaddr *)&clientaddr, &clientlen

) ) < 0 )

{

cout<<"client is bad"<<endl;

exit(0);

}

char x[50];

inet_ntop(AF_INET,&clientaddr.sin_addr,x,sizeof(x) );

cout<<"receiving message from client......client IP:"<<x<<"at port

number:"<<clientaddr.sin_port<<"\n";

if((child==fork())<0)

{

cout<<"creation of child failed";

exit(1);

}

close(socketmain);

cwork(socketclient);

exit(0);

}

}

int cwork(int socketclient)

{

FILE *fp1;

char str[100];

int flag=0;

fp1=fopen("table.txt","r");

char buff[LEN];

int msglen;

bzero(buff,LEN);

if((msglen=recv(socketclient,buff,LEN,0 ) ) < 0 )

{

cout<<"error recieving";

exit(1);

}

cout<<"socket used"<<socketclient<<"\n";

std::cout<<"the id received"<<buff<<"\n";

while(!feof(fp1))

{

fread( &rec,sizeof(rec),1,fp1 );

if(rec.id==atoi(buff))

{

send(socketclient,&rec,sizeof(rec),0);

flag++;

break;

}

}

if(flag==0)

{

rec.id=0;

send(socketclient,&rec,sizeof(rec),0);

}

}

CLIENT:

#include<iostream>

#include<stdio.h>

#include<sys/socket.h>

#include<netinet/in.h>

#include<stdlib.h>

#include<unistd.h>

#include<netdb.h>

#include<string.h>

#include<sys/types.h>

#include<arpa/inet.h>

using namespace std;

struct record

{

char name[20];

char dept[10];

int id;

}rec;

main(int argc,char* argv[])

{

int sockid,n;

struct sockaddr_in serv;

char str[100];

if(argc!=3)

{

cout<<"argument error";

exit(0);

}

if((sockid=socket(AF_INET,SOCK_STREAM,0))<0)

{

cout<<"socket not created";

exit(0);

}

bzero(&serv,sizeof(serv));

serv.sin_family=AF_INET;

serv.sin_port=htons(atoi(argv[2]));

if((inet_pton(AF_INET,argv[1],&serv.sin_addr))<0)

{

cout<<"connect fail";

exit(0);

}

if(connect(sockid,(struct sockaddr *)&serv,sizeof(serv))<0)

{

cout<<"connect fail";

exit(0);

}

cout<<"connected\n";

cout<<"enter id\n";

cin>>str;

write(sockid,str,106);

recv(sockid,&rec,sizeof(rec),0);

if(rec.id!=0)

{

cout<<rec.name<<endl;

cout<<rec.dept<<endl;

}

else

{

cout<<"record does not exist";

}

}

OUTPUT:

SERVER:

Socketused4

Id received 1

Record sent

CLIENT:

Connected

Enter id: 1

Name:xyz

Dept:cs

RESULT:

Retrieval of record from server based on query from client has been implemented.

DOMAIN NAME SERVER

EX.NO:8

DATE: 14.08.2013

AIM:

To implement DNS using UDP server algorithm.

DESCRIPTION:

#include <netdb.h>

extern int h_errno;

struct hostent *gethostbyname(const char *name);

The hostent structure is used by functions to store information about a given host, such as

host name, IPv4 address, and so forth. An application should never attempt to modify this

structure or to free any of its components. Furthermore, only one copy of

the hostent structure is allocated per thread.

The gethostbyname() function returns a structure of type hostent for the given hostname. Here name is

either a hostname, or an IPv4 address in standard dot notation (as for inet_addr(3)), or an IPv6 address

in colon (and possibly dot) notation. If name is an IPv4 or IPv6 address, no lookup is performed

and gethostbyname() simply copies name into the h_name field and its struct in_addr equivalent into

the h_addr_list[0] field of the returned hostentstructure. If name doesn't end in a dot and the

environment variable HOSTALIASES is set, the alias file pointed to by HOSTALIASES will first be

searched for name. The current domain and its parents are searched unless name ends in a dot.

ALGORITHM:

SERVER:

1. Create a socket with address family AF_INET type SOCK_DGRAM and default

protocol.

2. Initialize the socket and set its attributes. Assign specific port number as desired.

3. Bind the server to the socket using bind() function.

4. Listen for the client request.

5. On request obtain host name and retrieve the IP address using gethostbyname

function.

6. Write host IP address to the client machine.

CLIENT:

1. Create a socket with address family AF_INET and default protocol.

2. Initialize the socket and set its attributes.

3. Connect the server to the socket using connect() function to initialize the request.

4. Similarly accept message from server to client.

5. Terminate or close the connection.

PROGRAM:

SERVER:

#include<iostream>

#include<stdio.h>

#include<sys/socket.h>

#include<netinet/in.h>

#include<stdlib.h>

#include<unistd.h>

#include<netdb.h>

#include<string.h>

#include<sys/types.h>

#include<arpa/inet.h>

using namespace std;

#define MAX 100

#define LEN 81

int cwork(int socketclient);

main()

{

struct hostent *hp;

int len,socketmain,socketclient,child,port=4000;

struct sockaddr_in serv,clientaddr;

socklen_t clientlen;

char str[100];

int res;

if ( ( socketmain=socket( AF_INET,SOCK_DGRAM,0 ) ) < 0 )

{

std::cout<<"server cannot open socket"<<std::endl;

exit(1);

}

bzero(&serv,sizeof(serv));

serv.sin_family=AF_INET;

serv.sin_addr.s_addr=htonl(INADDR_ANY);

serv.sin_port=htons(port);

if((bind(socketmain,(struct sockaddr *)&serv,sizeof(serv)) ) < 0 )

{

cout<<"server bind failed";

exit(1);

}

listen(socketmain,5);

for(;;)

{

char buff[100];

int msglen,n;

clientlen=sizeof(clientaddr);

n=recvfrom(socketmain,str,MAX,0,(struct sockaddr*) &clientaddr, &clientlen);

str[n]='\0';

std::cout<<"client says :"<<str<<"\n";

if(n<0)

cout<<"message not received";

if((hp=gethostbyname(str))==NULL)

{

cout<<"cannot get the address!!"<<endl;

exit(0);

}

if(inet_ntop(AF_INET,hp->h_addr,buff,sizeof(buff) )<0)

{ cout<<"host message not available"<<endl;

exit(0);

}

cout<<buff;

sendto(socketmain,buff,strlen(buff),0,(struct sockaddr*) &clientaddr,clientlen);

}

}

CLIENT:

#include<iostream>

#include<stdio.h>

#include<sys/socket.h>

#include<netinet/in.h>

#include<stdlib.h>

#include<unistd.h>

#include<netdb.h>

#include<string.h>

#include<sys/types.h>

#include<arpa/inet.h>

using namespace std;

main(int argc,char* argv[])

{

int sockid,n;

struct sockaddr_in serv;

socklen_t servlen;

char sendline[40],recvline[40];

char str[100];

if(argc!=3)

{

cout<<"argument error";

exit(0);

}

if((sockid=socket(AF_INET,SOCK_DGRAM,0))<0)

{

cout<<"socket not created";

exit(0);

}

bzero(&serv,sizeof(serv));

serv.sin_family=AF_INET;

serv.sin_port=htons(atoi(argv[2]));

if((inet_pton(AF_INET,argv[1],&serv.sin_addr))<0)

{

cout<<"connect fail";

exit(0);

}

cout<<"enter the message to send to server";

gets(str);

servlen=sizeof(serv);

sendto(sockid,str,100,0,(struct sockaddr*)&serv,servlen);

n=recvfrom(sockid,str,100,0,NULL,NULL);

str[n]='\0';

cout<<"Server"<<str<<"\n";

}

OUTPUT:

Server:

receiving message: csblade1

192.168.12.147

Client:

enter the message to send to server : csblade1

192.168.12.147

RESULT:

Thus the above program using Domain Name Server was executed successfully.

IMPLEMENTATION OF BIT STUFFING

EX.NO:9

DATE:

AIM:

To implement Bit Stuffing –an error detection technique.

ALGORITHM:

SERVER:

1. Create a socket with address family AF_INET type SOCK_DGRAM and default

protocol.

2. Initialize the socket and set its attributes. Assign specific port number as desired.

3. Bind the server to the socket using bind() function.

4. Listen for the client request.

5. Read message from user.

6. Check for the number of consecutive 1's.

7. If there are more than 5 consecutive 1's, then add a 0 next to five 1's.

8. Send the stuffed message.

CLIENT:

1. Create a socket with address family AF_INET and default protocol.

2. Initialize the socket and set its attributes.

3. Connect the server to the socket using connect() function to initialize the request.

4. On receiving the message, check for consecutive 1's.

5. If there are more than 5 consecutive 1's, then print error message.

6. If there is a 0 after 5 consecutive 1's, then remove that 0.

7. Print the original message

PROGRAM:

SERVER:

#include<iostream>

#include<stdio.h>

#include<sys/socket.h>

#include<netinet/in.h>

#include<stdlib.h>

#include<unistd.h>

#include<netdb.h>

#include<string.h>

#include<arpa/inet.h>

#include<sys/types.h>

#define MAX 100

#define LEN 81

int cwork(int socketclient);

int main()

{

int socketmain,socketclient,child,port=2000;

struct sockaddr_in serv,clientaddr;

socklen_t clientlen;

struct hostent *hp;

char str[100],s[50];

int res;

if((socketmain=socket(AF_INET,SOCK_DGRAM,0))<0)

{

std::cout<<"server cannot open socket"<<std::endl;

exit(0);

}

bzero(&serv,sizeof(serv));

serv.sin_family=AF_INET;

serv.sin_addr.s_addr=htonl(INADDR_ANY);

serv.sin_port=htons(port);

if((bind(socketmain,(struct sockaddr *)&serv,sizeof(serv)))<0)

{

std::cout<<"server bind failed\n"<<std::endl;

exit(1);

}

for(;;)

{

char buff[100];

int msglen,n,j=0;

std::cout<<"socket used"<<socketclient<<"\n";

clientlen=sizeof(clientaddr);

printf("STRING:");

scanf("%s",&s);

int cnt=0;

for(int i=0;i<strlen(s);i++)

{

if(s[i]=='1'){

cnt++;

buff[j]=s[i];

j++;

if(cnt==5){

buff[j]='0';

j++;

cnt=0;

}

}

else{

cnt=0;

buff[j]=s[i];

j++;

}

}

printf("STUFFED STRING:%s\n",buff);

n=recvfrom(socketmain,str,MAX,0,(struct sockaddr*) &clientaddr,&clientlen);

str[n]='\0';

std::cout<<"client:"<<str<<"\n";

sendto(socketmain,buff,strlen(buff),0,(struct sockaddr*) &clientaddr,clientlen);

}

}

CLIENT:

#include<iostream>

#include<stdio.h>

#include<sys/socket.h>

#include<netinet/in.h>

#include<stdlib.h>

#include<unistd.h>

#include<netdb.h>

#include<string.h>

#include<arpa/inet.h>

#include<sys/types.h>

int main(int argc,char *argv[])

{

int sockid,n,cnt,j=0;

struct sockaddr_in serv;

socklen_t servlen;

char str[100],buff[100];

char sendline[40],recvline[40];

if(argc!=3)

{

std::cout<<"Argument error";

exit(0);

}

if((sockid=socket(AF_INET,SOCK_DGRAM,0))<0)

{

std::cout<<"socket not created";

exit(0);

}

bzero(&serv,sizeof(serv));

serv.sin_family=AF_INET;

serv.sin_port=htons(atoi(argv[2]));

if((inet_pton(AF_INET,argv[1],&serv.sin_addr))<0)

{

std::cout<<"connect fail";

exit(0);

}

std::cout<<"MESSAGE:";

std::cin>>str;

servlen=sizeof(serv);

sendto(sockid,str,strlen(str),0,(struct sockaddr*)&serv,servlen);

n=recvfrom(sockid,str,sizeof(str),0,NULL,NULL);

str[n]='\0';

std::cout<<"Server:"<<str<<"\n";

int c=0;

for(int i=0;i<strlen(str);i++)

{

if(str[i]=='1'){

cnt++;

buff[j]=str[i];

j++;

if(cnt==5)

{cnt=0;

if(str[i+1]=='0'){

i++; }

else

printf("Error!");

}

}

else{

cnt=0;

buff[j]=str[i];

j++;

}

}

buff[j]='\n';

printf("OUTPUT:%s",buff);

}

OUTPUT:

Server:

STRING:11111111111

STUFFED STRING:1111101111101

Client:

MESSAGE:hi

Server: 1111101111101

OUTPUT: 11111111111

RESULT:

Bit stuffing technique was implemented successfully.

OF CYCLIC REDUNDANCY CHECK

EX.NO:9.B

DATE:

AIM:

To write a C program to perform CYCLIC REDUNDANCY CHECK (CRC).

ALGORITHM:

1. Get the input string and the key from the user.

2. Add 0’s to the end of the string based on the length of the key.

3. Perform Binary Division with the string and the key.

4. The reminder is the required CRC which replaces the 0’s in the input string.

5. Transmit the code to the receiver.

6. At the receiving end the key using binary division divides the received message.

7. If reminder is 0 then the received message is correct, otherwise, display the error

message.

PROGRAM:

#include<stdio.h>

#include<math.h>

#include<string.h>

char str[20] , key[20] , str1[20];

void func()

{

int i,j ;

char rem[20];

for(i= 0;i<strlen(key);i++)

rem[i] = str[i];

while(i<strlen(str))

{

if(rem[0] == key[0])

{

rem[0]='0';

for(j=1;j<strlen(key);j++)

{

if(rem[j]!=key[j])

rem[j]='1';

else

rem[j]='0';

}

}

for(j=0;j<strlen(key)-1;j++)

rem[j]=rem[j+1];

rem[j]=str[i];

rem[j+1]='\0';

i++;

}

if(rem[0] == key[0])

{

rem[0]='0';

for(j=1;j<strlen(key);j++)

{

if(rem[j]!=key[j])

rem[j]='1';

else

rem[j]='0';

}

}

for(j=0;j<strlen(key);j++)

rem[j]=rem[j+1];

rem[j]='\0';

strcat(str1,rem);

printf("Decoded string is : %s ",str1);

}

main()

{

int i ;

printf("Enter coded string : ");

scanf("%s",str);

printf("Enter the key : ");

scanf("%s",key);

strcpy(str1,str);

for(i = 1 ; i < strlen(key) ; i++ )

strcat(str,"0");

if(strlen(str) >= strlen(key) )

func();

else

printf("Invallid key");

}

OUTPUT:

Enter coded string : 11001101

Enter the key : 1101

Decoded string is : 11001101001

RESULT:

Cyclic Redundancy Check was implemented on the message to be transmitted.

OPEN SHORTEST PATH FIRST ROUTING PROTOCOL

EX.NO:10

DATE:

AIM:

To simulate the OPEN SHORTEST PATH FIRST routing protocol based on the cost

assigned to the path.

ALGORITHM:

1.Read the number of nodes ‘n’ from user.

2.Read the cost matrix for the path from each node to another node.

3.Initialize SOURCE to node 1 and include it.

4. Compute D of a node which is the distance from source to that corresponding

node.

5.Repeat step 6 to step 8 for (n-l) nodes:

6.Choose the node that has not been included whose distance is minimum

and include that node.

7.For every other node not included compare the distance directly from the

source with the distance to reach the node using the newly included node.

8.Take the minimum value as the new distance.

9.Print all the nodes with shortest path cost from source node.

PROGRAM: #include<iostream>

#include<stdio.h>

using namespace std;

class OSPF

{

public:

int costmatrix[10][10];

struct vertices

{

int id;

int cost;

}vertex[10],final[10];

int i,j,k,source,dest;

int size;

void getdata()

{

cout<<"enter the number of vertices;"<<endl;

cin>>size;

cout<<"enter the source and destination"<<endl;

cin>>source>>dest;

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

{

vertex[i].id=i;

vertex[i].cost=0;

}

cout<<"enter the cost matrix"<<endl;

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

{

for(j=0;j<size;j++)

{

cin>>costmatrix[i][j];

}

}

}

void calculate()

{

int small=999;

for(i=0;i<size;i++){

final[i].cost=costmatrix[source-1][i];

if(costmatrix[source-1][i]< small && costmatrix[source-1][i]>0)

{

small= costmatrix[source-1][i];

}

}

for(i=0;i<size;i++){

for(j=0;j<size;j++){

for(k=0;k<size;k++){

if(final[k].cost> final[j].cost+costmatrix[j][k])

final[k].cost=final[j].cost+costmatrix[j][k];

}

}

}

for(i=0;i<size;i++){

cout<<"\n\t";

for(j=0;j<size;j++)

cout<<costmatrix[i][j]<<"\t";

}

cout<<"the shortest path:";

cout<<final[dest-1].cost;

}

};

main()

{

OSPF o;

o.getdata();

o.calculate();

}

OUTPUT: enter the number of vertices;

4

enter the source and destination

1

4

enter the cost matrix

0 1 3 2 1 0 0 3 3 0 0 4 2 0 4 0

0 1 3 2

1 0 0 3

3 0 0 4

2 0 4 0

the shortest path:2

RESULT:

The OPEN SHORTEST PATH FIRST routing protocol has been implemented successfully