Networking Lab Assignment: Name: Univ. Roll: Year: 3 Stream: It
Networking Lab Assignment: Name: Univ. Roll: Year: 3 Stream: It
Networking Lab Assignment: Name: Univ. Roll: Year: 3 Stream: It
ASSIGNMENT
NAME:
UNIV. ROLL:
YEAR: 3RD
STREAM : IT
IP configuration
In Windows:
Steps:
1.Start-> Connect to-> Show all network->select
connection(lan/wireless)
2. Rt click on that -> go to Properties
3. Click on tcp/ip4 -> click on properties
4. New window will pop up , Change ip,subnet,gateway,
server address accordingly
5. click on OK-> click on CLOSE
IN LINUX:
Command mode:
# ifconfig eth0 <ip address> netmask <subnet address> up
Eg. # ifconfig eth0 192.168.1.5 netmask 255.255.255.0 up
OR
GUI Mode:
$ system-config-network
New window pop up –
Steps:
1. Select the connection(eth0/eth1/wireless etc)
2.Change the IP ,Subnet,Gateway , server address as requied
3. Click on OK.
Socket Theory
Socket: An interface between an application process and
transport layer
The application process can send/receive messages
to/from another application
process (local or remote)via a socket.
In Unix jargon, a socket is a file descriptor an integer
associated with an open
file
Types of Sockets: Internet Sockets, unix sockets, X.25
sockets etc
Internet sockets characterized by IP Address (4 bytes) and
port number (2
bytes)
Types of Internet Sockets
Stream Sockets (SOCK_STREAM) - Connection oriented,
Rely on TCP to
provide reliable two-way connected communication
Datagram Sockets (SOCK_DGRAM) - Rely on UDP ,
Connection is unreliable
bind() - what port am I on?
Used to associate a socket with a port on the local
machine. The port number
is used by the kernel to match an incoming packet to a
process.
int bind(int sockfd, struct sockaddr *my_addr, int addrlen)
sockfd is the socket descriptor returned by socket()
my_addr is pointer to struct sockaddr that contains
information about
your IP address and port
addrlen is set to sizeof(struct sockaddr)
returns -1 on error
my_addr.sin_port = 0; //choose an unused port at random
my_addr.sin_addr.s_addr = INADDR_ANY; //use my IP addr
connect() - Hello! Connects to a remote host
int connect(int sockfd, struct sockaddr *serv_addr, int
addrlen)
sockfd is the socket descriptor returned by socket()
serv_addr is pointer to struct sockaddr that contains
information on
destination IP address and port
addrlen is set to sizeof(struct sockaddr)
returns -1 on error
At times, you don't have to bind() when you are using
connect()
send() and recv() - Let's talk!
The two functions are for communicating over stream
sockets or connected
datagram sockets.
int send(int sockfd, const void *msg, int len, int flags);
sockfd is the socket descriptor you want to send data to
(returned by
socket() or got with accept())
msg is a pointer to the data you want to send
len is the length of that data in bytes
set flags to 0 for now
sent() returns the number of bytes actually sent (may be
less than the
number you told it to send) or -1 on error
send() and recv() - Let's talk!
int recv(int sockfd, void *buf, int len, int flags);
sockfd is the socket descriptor to read from
buf is the buffer to read the information len is the
maximum length of the buffer
set flags to 0 for now
recv() returns the number of bytes actually read into the
buffer or -1 on
error
If recv() returns 0, the remote side has closed connection
on you
close() - Bye Bye!
int close(int sockfd);
Closes connection corresponding to the socket descriptor
and frees the
socket descriptor
Will prevent any more sends and recvs
socket() -- Get the file descriptor
int socket(int domain, int type, int protocol);
domain should be set to AF_INET
type can be SOCK_STREAM or SOCK_DGRAM
set protocol to 0 to have socket choose the correct
protocol based on type
socket() returns a socket descriptor for use in later system
calls or -1 on error.
Q. WAP to add 2 nos. in server passed by client and then
print the result on client.
Server side
#include <sys/types.h>
#include <sys/socket.h>
#include <stdio.h>
#include <sys/un.h>
#include <unistd.h>
int main()
{
int server_sockfd,client_sockfd;
int server_len,client_len;
struct sockaddr_un server_address;
struct sockaddr_un client_address;
unlink("server_socket");
server_sockfd=socket(AF_UNIX,SOCK_STREAM,0);
server_address.sun_family=AF_UNIX;
strcpy(server_address.sun_path,"server_socket");
server_len=sizeof(server_address);
bind(server_sockfd, (struct sockaddr
*)&server_address,server_len);
listen(server_sockfd,5);
while(1)
{
char ch;
int n1,n2,result;
system("clear");
printf("Server Waiting...");
client_len=sizeof(client_address);
client_sockfd=accept(server_sockfd,(struct sockaddr
*)&client_address,
&client_len);
read(client_sockfd,&n1,1);
read(client_sockfd,&n2,1);
/* read(client_sockfd,&ch,1);*/
result = n1 + n2;
write(client_sockfd,&result,1);
close(client_sockfd);
}
}
Output:
$ cc addserver.c -o ads.out
$ ./ads.out
Client Side
#include<sys/types.h>
#include<sys/socket.h>
#include<stdio.h>
#include<sys/un.h>
#include<unistd.h>
int main()
{
int sockfd;
int len;
struct sockaddr_un address;
int result,n1,n2;
char ch;
sockfd=socket(AF_UNIX,SOCK_STREAM,0);
address.sun_family=AF_UNIX;
strcpy(address.sun_path,"server_socket");
len=sizeof(address);
result=connect(sockfd, (struct sockaddr
*)&address,len);
if(result==-1)
{
perror("oops:client1");
exit(1);
}
printf("\nEnter A Number:");
scanf("%d",&n1);
printf("\nEnter Another Number:");
scanf("%d",&n2);
fflush(stdin);
/*printf("\nEnter Operator(+,-,*,/ etc.):");
scanf("%c",&ch);*/
write(sockfd,&n1,1);
write(sockfd,&n2,1);
/*write(sockfd,&ch,1);*/
read(sockfd,&result,1);
printf("\nResult, Addition of two numbers ::
%d",result);
close(sockfd);
exit(0);
}
output:
$ cc addclient.c -o adc.out
$ ./adc.out
Enter A number: 12
Enter Another number:13
Result , Addition of two numbers:: 25
$
Q. WAP in C (socket programming) to make a chat
application in which two Host(server & client) can pass
message to each other.
#include<stdio.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<stdlib.h>
#include<string.h>
#include<arpa/inet.h>
int main()
{
int server_sd,server_len,client_len,b,a,client_sd;
struct sockaddr_in server_addr,client_addr;
char ipadd[]={"10.10.76.116"};
server_len=sizeof(server_addr);
int port=5499;
char recvmessg[20],sendmessg[20];
server_sd=socket(AF_INET,SOCK_STREAM,0);
if(server_sd>=0)
printf("socket created successfully\n");
//server_len=sizeof(server_addr);
server_addr.sin_family=AF_INET;
server_addr.sin_port=htons(port);
inet_aton(ipadd,(&server_addr.sin_addr));
server_len=sizeof(server_addr);
b=bind(server_sd,(struct sockaddr
*)&server_addr,server_len);
if(b>=0)
printf("\nbinded successfully\n\n");
listen(server_sd,5);
client_len=sizeof(client_addr);
a=accept(server_sd,(struct sockaddr
*)&client_addr,&client_len);
while(1)
{
read(a,&recvmessg,20);
printf("%s",recvmessg);
printf("\nEnter message for reply: ");
gets(sendmessg);
write(a,&sendmessg,20);
if(strcmp(recvmessg,"bye")==0)
{
close(server_sd);
exit(0);
}
}
}
Out put:
$cc chat_serv.c –o chts.out
$./chts.out
socket created successfully
binded successfully
hi
Enter message for reply: hello
Wats up
Enter message for reply: bye
$
Client Side:
#include<stdio.h>
#include<unistd.h>
#include<sys/types.h>
#include<sys/un.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<arpa/inet.h>
#include<string.h>
#include<stdlib.h>
int main()
{
int client_sd,client_len;
char ipadd[]={"10.10.76.116"};
int port=5499;
char recvmssg[20],sendmssg[20];
struct sockaddr_in client_addr;
client_sd=socket(AF_INET,SOCK_STREAM,0);
if(client_sd>0)
printf("socket created successfully");
client_len=sizeof(client_addr);
client_addr.sin_family=AF_INET;
client_addr.sin_port=htons(port);
inet_aton(ipadd,(&client_addr.sin_addr));
connect(client_sd,(struct sockaddr
*)&client_addr,client_len);
while(1)
{
Output:
$ cc chat_cl.c –o chtc.out
$ ./chtc.out
socket created successfully
enter the msg : hi
hello
enter the msg : wats up
$
Server Side:
#include<sys/types.h>
#include<sys/socket.h>
#include<stdio.h>
#include<sys/un.h>
#include<unistd.h>
#include<sys/socket.h>
#include<arpa/inet.h>
#include<stdlib.h>
#include<netinet/in.h>
#include<string.h>
#include<sys/time.h>
#include<time.h>
#include<stdlib.h>
struct sockaddr_in serv_addr,cli_addr;
int sockfd,cli_addr_len,connfd,bb;
char buff[1024];
time_t ticks;
char serv_ip[]={"10.10.76.116"};
unsigned short serv_port=5045;
int main()
{
bzero(&serv_addr,sizeof(serv_addr));
sockfd = socket(AF_INET,SOCK_STREAM,0);
serv_addr.sin_family=AF_INET;
serv_addr.sin_port=htons(serv_port);
inet_aton(serv_ip,(&serv_addr.sin_addr));
bind(sockfd,(struct sockaddr
*)&serv_addr,sizeof(serv_addr));
listen(sockfd,5);
cli_addr_len=sizeof(cli_addr);
while(1)
{
connfd= accept(sockfd,(struct
sockaddr*)&cli_addr,&cli_addr_len);
ticks=time(NULL);
snprintf(buff,(int)
(sizeof(buff)),"%.24s\r\n",ctime(&ticks));
write(connfd,buff,(int)(sizeof(buff)));
close(connfd);
}
}
Output:
$ cc date_serv.c –o dts.out
$ ./ dts.out
Client Side:
#include<stdio.h>
#include<unistd.h>
#include<sys/un.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<arpa/inet.h>
#include<string.h>
#include<stdlib.h>
#include<sys/time.h>
#include<time.h>
serv_addr.sin_family=AF_INET;
serv_addr.sin_port=htons(serv_port);
if(inet_pton(AF_INET,server_ip,&serv_addr.sin_addr)
<0);
printf("not successful");
printf("\nTCP ECHO CLINT");
if((connect(client_sd,(struct sockaddr
*)&serv_addr,sizeof(serv_addr)))<0)
{
printf("\n cilent error");
//close(client_sd);