SOURCE CODE
SERVER
#include “stdio.h “
#include “sys/types.h “
#include “sys/socket.h “
#include “netinet/in.h “
#define MAXSIZE 25
main()
{
system("clear");
struct sockaddr_in ss_addr, c_addr,c1_addr, c2_addr ;
int sockfd, rval;
socklen_t addr_len, addr_len1;
char buff[MAXSIZE], buff1[MAXSIZE];
sockfd = socket(AF_INET, SOCK_DGRAM, 0);
if (sockfd == -1 )
printf("socket(logical port) creation error");
ss_addr.sin_family = AF_INET;
ss_addr.sin_port = htons(6919);
ss_addr.sin_addr.s_addr = htonl(INADDR_ANY);
rval = bind(sockfd, (struct sockaddr*) &ss_addr, sizeof(ss_addr));
if (rval == -1)
{
printf( "Binding error");
close(sockfd);
}
else
{
addr_len = sizeof(c_addr);
addr_len1 = sizeof(c1_addr);
rval=recvfrom(sockfd,buff,sizeof(buff), 0, (struct sockaddr*)&c_addr,&addr_len );
if (rval == -1)
printf("while recving from 1st problem.");
puts(buff);
rval = recvfrom(sockfd, buff1, sizeof(buff1), 0, (struct sockaddr*)&c1_addr,&addr_len1 );
puts(buff1);
while(1)
{
rval= recvfrom(sockfd,buff1,sizeof(buff1), 0, (struct sockaddr*)&c1_addr, &addr_len1 );
if (rval == -1)
{
printf("while recving from 2nd problem.");
close(sockfd);
}
puts(buff1);
rval = sendto(sockfd, buff1, sizeof(buff1), 0, (struct sockaddr*)&c_addr, addr_len );
if (rval == -1)
printf("while sending from server to 1st client problem.");
// For Client 1
rval = recvfrom(sockfd, buff, sizeof(buff), 0, (struct sockaddr*)&c_addr, &addr_len );
if (rval == -1)
{
printf("while recving from 1st problem.");
close(sockfd);
}
puts(buff);
rval = sendto(sockfd, buff, sizeof(buff), 0, (struct sockaddr*)&c1_addr, addr_len1 );
if (rval == -1)
{
printf("while sending from server to 2nd client problem.");close(sockfd);
}
if (rval == -1)
{
printf("breaking from loop ");
close(sockfd);
break;
}
} // while end
}
close(sockfd);
}
CLIENT1
#include “sys/types.h “
#include “sys/socket.h “
#include “netinet/in.h “
#include “stdio.h “
#include “string.h “
#define MAXSIZE 25
main()
{
system("clear");
struct sockaddr_in c_addr, s1_addr;
int sockfd, rval;
socklen_t addr_len;
char buff[MAXSIZE];
sockfd = socket(AF_INET, SOCK_DGRAM, 0);
if (sockfd == -1)
printf("Error while socket creation");
s1_addr.sin_family = AF_INET;
s1_addr.sin_port = htons(6919);
s1_addr.sin_addr.s_addr = inet_addr("127.0.0.1") ;
read(0,buff,sizeof(buff));
rval = sendto(sockfd, buff, sizeof(buff), 0, (struct sockaddr *)&s1_addr, sizeof(s1_addr));
if (rval == -1)
{
printf("While sending problem");
close(sockfd);
}
while(1)
{
addr_len = sizeof(s1_addr);
rval=recvfrom(sockfd,buff,sizeof(buff),0,(struct sockaddr *)&s1_addr,&addr_len);
if (rval == -1)
{
printf("While receving problem");
close(sockfd);
}
else
{
puts("from server received ");
puts(buff);
}
read(0,buff,sizeof(buff));
rval = sendto(sockfd, buff, sizeof(buff), 0, (struct sockaddr *)&s1_addr,sizeof(s1_addr) );
if (rval == -1)
{
printf("While sending problem");
close(sockfd);
}
} // while end
close(sockfd);
}
CLIENT2
#include “stdio.h “
#include “sys/types.h “
#include “sys/socket.h “
#include “netinet/in.h “
#include “string.h “
#define MAXSIZE 25
main()
{
system("clear");
struct sockaddr_in c_addr, s1_addr;
int sockfd, rval;
socklen_t addr_len;
char buff[MAXSIZE];
sockfd = socket(AF_INET, SOCK_DGRAM, 0);
if (sockfd == -1)
printf("Error while socket creation");
s1_addr.sin_family = AF_INET;
s1_addr.sin_port = htons(6919);
s1_addr.sin_addr.s_addr = inet_addr("127.0.0.1") ;
read(0,buff,sizeof(buff));
rval=sendto(sockfd,buff,sizeof(buff),0,(struct sockaddr *)&s1_addr ,sizeof(s1_addr));
if (rval==-1)
{
printf("While sending problem");
close(sockfd);
}
while(1)
{
read(0,buff,sizeof(buff));
rval=sendto(sockfd,buff,sizeof(buff),0,(struct sockaddr *)&s1_addr,sizeof(s1_addr) );
if (rval == -1)
{
printf("While sending problem");
close(sockfd);
}
addr_len = sizeof(s1_addr);
rval = recvfrom(sockfd,buff,sizeof(buff),0,(struct sockaddr *)&s1_addr,&addr_len );
if (rval == -1)
{
printf("While receving problem");
close(sockfd);
}
else
{
puts("from server received ");
puts(buff);
}
} // while end
close(sockfd);
}
OUTPUT
SERVER
[NPLab@localhost ~]$ cc multis.c
[NPLab@localhost ~]$ ./a.out
hi
hello
CLIENT1
[NPLab@localhost ~]$ cc multic1.c
[NPLab@localhost ~]$ ./a.out
hi
CLIENT2
[NPLab@localhost ~]$ cc multic2.c
[NPLab@localhost ~]$ ./a.out
hello
SOURCE CODE
SERVER
#include “stdio.h “
#include “sys/types.h “
#include “sys/socket.h “
#include “netinet/in.h “
#include “arpa/inet.h “
#include “string.h “
#include “stdlib.h “
#include “arpa/inet.h “
#include “unistd.h “
#define SIZE 4
main()
{
int id,lfd,i,j,status,n;
char str[20],frame[20],temp[20],ack[20];
socklen_t len;
struct sockaddr_in server,client;
id=socket(AF_INET,SOCK_STREAM,0);
if(id < sin_family="AF_INET;" sin_port="htons(8565);" s_addr="htonl(INADDR_ANY);" len="sizeof(&client);" lfd="accept(id,(struct" i="0;" n="strlen(frame);" j="0;j" status="="-1)" j="0;;)" 4="="0)" n="strlen(frame);" j="0;j" id="socket(AF_INET,SOCK_STREAM,0))="="-1)" sin_family="AF_INET;" sin_port="htons(8565);" c="connect(id,(struct"> y 0-- > n)",str);
scanf("%d",&choice);
if(!choice)
write(id,"-1",sizeof("-1"));
else
{
printf("Enter The Sequence Of Frame Where Error Has Occured: ");
scanf("%s",err);
write(id,err,sizeof(err));
read(id,str,20);
printf("\n\n Received The Retransmitted Frames %s\n\n",str);
}
}
close(id);
return 0;
}
OUTPUT:
SERVER:
[NPLab@localhost ~]$ cc slides21.c
[NPLab@localhost ~]$ ./a.out
Enter The Text: HELLO
Transmitting Frames
0123
Transmission Is Successful
Transmitting Frames
4
Transmission Is Successful
EXITING
CLIENT:
[NPLab@localhost ~]$ cc slidec21.c
[NPLab@localhost ~]$ ./a.out
Received HELL0123
Want To Report An Error? (1-- > y 0-- > n)0
Received O4
Want To Report An Error? (1-- > y 0-- > n)0
EXITING
Output
REMOTE PROCEDURE CALL
ADD.X
struct add_arg
{
int first;
int second;
};
program ADDPROG {
version ADDVER {
int add(add_arg)=1;
}=1;
}=0x20000199;
ADDCLIENT21.C
#include “ stdio.h “
#include “ ctype.h “
#include “ rpc/rpc.h “
#include "add.h"
add_arg arg;
main(argc,argv)
int argc;
char *argv[];
{
CLIENT *clnt;
int *result;
if(argc!=4)
{
printf("USAGE= %s HOST NUM1 NUM2 \n",argv[0]);
exit(1);
}
clnt=clnt_create(argv[1],ADDPROG,ADDVER,"udp");
if(clnt==NULL)
{
clnt_pcreateerror(argv[1]);
exit(1);
}
arg.first=atoi(argv[2]);
arg.second=atoi(argv[3]);
result=add_1(&arg,clnt);
if(result==(int *)NULL)
{
clnt_perror(clnt,"call failed");
exit(1);
}
else
{
printf("SUCCESS: %d + %d = %d\n",arg.first,arg.second,*result);
}
exit(0);
}
ADDSERVER21.C
#include "add.h"
int *add_1_svc(argp,rqstp)
add_arg *argp;
struct svc_req *rqstp;
{
static int result;
result=argp- > first+argp- > second;
return(&result);
}
OUTPUT
[NPLab@localhost rpc]$ ls
addclient21.c addserver21.c add.x
[NPLab@localhost rpc]$ rpcgen add.x
[NPLab@localhost rpc]$ ls
addclient21.c add_clnt.c add.h addserver21.c add_svc.c add.x add_xdr.c
[NPLab@localhost rpc]$ cc -c addclient21.c
[NPLab@localhost rpc]$ ls
addclient21.c add_clnt.c addserver21.c add.x
addclient21.o add.h add_svc.c add_xdr.c
[NPLab@localhost rpc]$ cc -c add_clnt.c
[NPLab@localhost rpc]$ ls
addclient21.c add_clnt.c add.h add_svc.c add_xdr.c
addclient21.o add_clnt.o addserver21.c add.x
[NPLab@localhost rpc]$ cc -c add_xdr.c
[NPLab@localhost rpc]$ ls
addclient21.c add_clnt.c add.h add_svc.c add_xdr.c
addclient21.o add_clnt.o addserver21.c add.x add_xdr.o
[NPLab@localhost rpc]$ cc -o addc21 addclient21.o add_clnt.o add_xdr.c
[NPLab@localhost rpc]$ ls
addc21 addclient21.o add_clnt.o addserver21.c add.x add_xdr.o addclient21.c add_clnt.c add.h add_svc.c add_xdr.c
[NPLab@localhost rpc]$ cc -c addserver21.c
[NPLab@localhost rpc]$ ls
addc21 addclient21.o add_clnt.o addserver21.c add_svc.c add_xdr.c addclient21.c add_clnt.c add.h add.x addserver21.o add_xdr.o
[NPLab@localhost rpc]$ cc -c add_svc.c
[NPLab@localhost rpc]$ ls
addc21 add_clnt.c addserver21.c add_svc.o add_xdr.o
addclient21.c add_clnt.o addserver21.o add.x addclient21.o add.h add_svc.c add_xdr.c
[NPLab@localhost rpc]$ cc -o adds21 addserver21.o add_svc.o add_xdr.c
[NPLab@localhost rpc]$ ls
addc21 add_clnt.c adds21 add_svc.c add_xdr.c
addclient21.c add_clnt.o addserver21.c add_svc.o add_xdr.o addclient21.o add.h addserver21.o add.x
SERVER:
[NPLab@localhost ~]$ ./adds21
CLIENT:
[NPLab@localhost ~]$ ./addc21 localhost 3 7
SUCCESS: 3 + 7 = 10
#include ” stdio.h “
#include “ winsock.h “
void main()
{
WSADATA wsaData;
char buf1[256];
int iResult = WSAStartup(0x101,&wsaData);
SOCKET m_socket;
m_socket = socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
if(m_socket == -1)
{
printf("Error in Socket Creation\n");
closesocket(m_socket);
return;
}
else
{
printf("Socket Successfully Created\n");
}
sockaddr_in saddr;
saddr.sin_family = AF_INET;
saddr.sin_addr.S_un.S_addr = inet_addr("170.100.40.83");
saddr.sin_port = htons(8101);
printf("Initilised\n");
if(connect(m_socket,(SOCKADDR*)&saddr,sizeof(saddr)) == -1)
{
printf("Error in Connection\n");
WSACleanup();
return;
}
printf("Connected Successfully\n");
memset(buf1,0,256);
printf("Enter your Message\n");
scanf("%s",buf1);
while(1)
{
send(m_socket,buf1,256,0);
memset(buf1,0,256);
if(recv(m_socket,buf1,256,0) > 0)
{
printf("Message From Server\t%s\n",buf1);
printf("Enter ur Message");
scanf("%s",buf1);
}
}
closesocket(m_socket);
}
OUTPUT