CN Lab
CN Lab
Understanding and using of commands like ifconfig, netstat, ping, arp, telnet, ftp, finger,
traceroute, whois etc. Usage of elementary socket system calls (socket (), bind(),
listen()accept(),connect(),send(),recv(),sendto(),recvfrom()).
REGD.N0:-21K61A6101
Program Objective:
Understanding and using of commands like ifconfig, netstat, ping, arp, telnet, ftp, finger,
traceroute, whois
Program Description:
UNIX utilities are commands that, generally, perform a single task. It may be as simple as
printing the date and time, or a complex as finding files that match many criteria throughout a
directory hierarchy
IFCONFIG
The Unix command ifconfig (short for interface configurator) serves to configure and control
TCP/IP network interfaces from a command line interface (CLI). Common uses for ifconfig
include setting an interface's IP address and netmask, and disabling or enabling a given
interface. Ipconfig is an MS-DOS utility that can be used from MS-DOS and an MS-DOS shell to
display the network settings currently assigned and given by a network. This command can be
utilized to verify a network connection as well as to verify your network settings. Syntax:
ipconfig [/allcompartments] [/? | /all | /renew [adapter] | /release [adapter] | /renew6
[adapter] | /release6 [adapter] | /flushdns | /displaydns | /registerdns | /showclassid adapter
| /setclassid adapter [classid] | /showclassid6 adapter | /setclassid6 adapter [classid] ] Example:
ipconfig /all
REGD.N0:-21K61A6101
Parameters used with this command must be prefixed with a hyphen (-) rather than a slash (/).
-a : Displays all active TCP connections and the TCP and UDP ports on which the computer is
listening.
-e : Displays ethernet statistics, such as the number of bytes and packets sent and received.
This parameter can be combined with -s.
-f : Displays fully qualified domain names for foreign addresses.
-i : Displays network interfaces and their statistics (not available under Windows) -n : Displays
active TCP connections, however, addresses and port numbers are expressed numerically and
no attempt is made to determine names.
-o : Displays active TCP connections and includes the process ID (PID) for each connection.
-p Linux: Process : Show which processes are using which sockets
Syntax: NETSTAT [-a] [-b] [-e] [-f] [-n] [-o] [-p proto] [-r] [-s] [-x] [-t] [interval]
OUTPUT
PING
Ping is a computer network tool used to test whether a particular host is reachable across an IP
network; it is also used to self test the network interface card of the computer, or as a speed
test. It works by sending ICMP “echo request” packets to the target host and listening for ICMP
REGD.N0:-21K61A6101
ARP
In computer networking, the Address Resolution Protocol (ARP) is the method for finding a
host's link layer (hardware) address when only its Internet Layer (IP) or some other Network
Layer address is known. ARP has been implemented in many types of networks; it is not an IP-
only or Ethernet-only protocol. It can be used to resolve many different network layer protocol
addresses to interface hardware addresses, although, due to the overwhelming prevalence of
IPv4 and Ethernet, ARP is primarily used to translate IP addresses to Ethernet MAC addresses.
Arp syntax:
ARP -s inet_addr eth_adr [if_addr]
ARP -d inet_addr [if_addr]
ARP -a [inet_addr] [-N if_addr]
Example: arp -a
OUTPUT
REGD.N0:-21K61A6101
In computer networking, the Name/Finger protocol and the Finger user information protocol
are simple network protocols for the exchange of human-oriented status and user information.
finger looks up and displays information about system users.
Syntax: finger [-lmsp] [user ...] [user@host ...]
Example: finger -p ch
TRACEROUTE:
traceroute is a computer network tool used to determine the route taken by packets across an
IP network . An IPv6 variant, traceroute6, is also widely available.Traceroute is often used for
network troubleshooting. By showing a list of routers traversed, it allows the user to identify
the path taken to reach a particular destination on the network. This can help identify routing
problems or firewalls that may be blocking access to a site. Traceroute is also used by
penetration testers to gather information about network infrastructure and IP ranges around a
given host. It can also be used when downloading data, and if there are multiple mirrors
available for the same piece of data, one can trace each mirror to get a good idea of which
mirror would be the fastest to use.
Syntax: traceroute [-46dFITUnreAV] [-f first_ttl] [-g gate,...] [-i device]
[-m max_ttl] [-p port] [-s src_addr] [-q nqueries]
-N squeries] [-t tos] [-l flow_label] [-w waittime]
[-z sendwait] [-UL] [-D] [-P proto] [--sport=port] [-M method]
[-O mod_options] [--mtu] [--back] host [packet_len]
Example: traceroute www.google.com
WHO IS:
WHOIS (pronounced "who is"; not an acronym) is a query/response protocol which is widely
used for querying an official database in order to determine the owner of a domain name, an
IP address, or an autonomous system number on the Internet. WHOIS lookups were
traditionally made using a command line interface, but a number of simplified web-based tools
now exist for looking up domain ownership details from different databases. WHOIS normally
runs on TCP port 43.
REGD.N0:-21K61A6101
Socket
To do network I/O, the first thing a process must do is to call the socket system call, specifying
the type of communication protocol desired.
#include<sys/types.h>
#include<sys/socket.h>
int socket(int family, int type, int protocol);
The family is one of
AF_UNIX -- Unix internal protocols
AF_INET -- Internet protocols
AF_NS -- Xerox NS Protocols
AF_IMPLINK-- IMP link layer
The AF_ prefix stands for "address family." In the first project, we are going to use AF_INET.
The socket type is one of the following:
SOCK_STREAM stream socket
SOCK_DGRAM datagram socket
SOCK_RAW raw socket
SOCK_SEQPACKETsequenced packet socket
SOCK_RDM reliably delivered message socket (not implemented yet)
The protocol argument to the socket system call is typically set to 0 for most user applications.
The valid combinations are shown as follows
REGD.N0:-21K61A6101
#include<stdlib.h>
int main() {
int sfd; struct sockaddr_in serv_addr;
if((sfd=socket(AF_INET,SOCK_STREAM,0))<0)
{ perror("socket
error"); exit(-1);
}
serv_addr.sin_family=AF_INET; serv_addr.sin_port=htons(4890);
serv_addr.sin_addr.s_addr=inet_addr("172.16.0.1");
if((bind(sfd,(struct sockaddr *) & serv_addr,sizeof(serv_addr)))<0)
{ perror("bind
error"); exit(-1);
}
printf("address binded...."); printf("\nserver ip address
is %s",inet_ntoa(serv_addr.sin_addr)); printf("\n port
number=%d\n",ntohs(serv_addr.sin_port)); close(sfd);
return 0; } O/P:
address binded.... server ip address
is 172.16.0.1
portnumber=4890
REGD.N0:-21K61A6101
address and any messages received for this address are to be given to me." Both
connectionoriented and connectionless servers need to do this before accepting client
requests.
2. A client can register a specific address for itself.
3. A connectionless client needs to assure that the system assigns it some unique address, so
that the other end (the server) has a valid return address to send its responses to. This
corresponds to making certain an envelope has a valid return address, if we expect to get a
reply from the person we sent the letter to
#include<stdio.h>
#include<sys/socket.h>
#include<sys/types.h>
#include<netinet/in.h>
#include<stdlib.h>
int main() {
int sfd; struct sockaddr_in serv_addr;
if((sfd=socket(AF_INET,SOCK_STREAM,0))<0)
{ perror("socket
error"); exit(-1);
}
serv_addr.sin_family=AF_INET; serv_addr.sin_port=htons(4890);
serv_addr.sin_addr.s_addr=inet_addr("172.16.0.1"); if((bind(sfd,(struct
sockaddr *) & serv_addr,sizeof(serv_addr)))<0)
{ perror("bind
error"); exit(-1);
}
printf("address binded...."); printf("\nserver ip address is
%s",inet_ntoa(serv_addr.sin_addr)); printf("\n port
REGD.N0:-21K61A6101
connect
A client process connects a socket descriptor following the socket system call to establish a
connection with a server.
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/socket.h>
int send(int sockfd, char *buff, int nbytes, int flags); int sendto(int sockfd, char *buff, int
nbytes, int flags, struct sockaddr *to, int addrlen); int recv(int sockfd, char *buff, int nbytes,
REGD.N0:-21K61A6101
int flags); int recvfrom(int sockfd, char *buff, int nbytes, int flags, struct sockaddr *from, int
*addrlen);
The first three arguments, sockfd, buff, and nbytes, to the four system calls are similar to the first
three arguments for read and write. The flags argument can be safely set to zero ignoring the
details for it. The to argument for sendto specifies the protocol-specific address of where the data
is to be sent. Since this address is protocol-specific, its length must be specified by addrlen.
Therecvfrom system call fills in the protocol-specific address of who sent the data into from. The
length of this address is also returned to the caller in addrlen. Note that the final argument to
sendtois an integer value, while the final argument to recvfrom is a pointer to an integer value.
close
The normal Unix close system call is also used to close a socket.
int close(int fd);
If the socket being closed is associated with a protocol that promises reliable delivery (e.g., TCP
or SPP), the system must assure that any data within the kernel that still has to be transmitted or
acknowledged, is sent. Normally, the system returns from the close immediately, but the kernel
still tries to send any data already queued.
RESULT:
REGD.N0:-21K61A6101
Description:
The Transmission Control Protocol (TCP) is one of the core protocols of the Internet
protocol suite. It is responsible for providing reliable, ordered, and error-checked delivery
of data between applications running on different hosts. TCP is a connection-oriented
protocol, which means that it establishes a connection between two hosts before any data
is transmitted. This connection allows TCP to keep track of the order of the data packets
and to retransmit any packets that are lost or corrupted.
Server Program:
#include<stdio.h>
#include<stdlib.h>
#include<errno.h>
#include<string.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h> int
main(int argc,char * argv[])
{
int s,s2,t,len,cp;
struct sockaddr_in
local,rem;
char str[100];
s=socket(AF_INET,SOCK_STREAM,0);
if(s==-1) {
perror("socket");
exit(1); }
bzero((char *)&local,sizeof(local));
local.sin_family=AF_INET;
local.sin_port=htons(atoi(argv[1]));
local.sin_addr.s_addr=htonl(INADDR_ANY);
if(bind(s,(struct sockaddr *)&local,sizeof(local))==-1)
{ perror(
"bind");
Sasi institute of technology and engineering 11
exit(1); }
if(listen(s,
5)==-1)
{
perror("
listen");
exit(1); }
for(;;)
REGD.N0:-21K61A6101
{ int
done,n;
printf("waiting for a connection...");
t=sizeof(rem); s2=accept(s,(struct sockaddr *)&rem,&t);
if(s2==-1) {
perror("accept");
exit(1); } printf("connecte
d");
if((cp=fork())<0 ) { perror("fork error"); } else
if(cp==0) {
close(s); done=0; do
{
n=recv(s2,str,100,0); printf("%s",str); if(n<=0)
{ if(n<0) perror("recv"); done=1; }
if(!done)
if(send(s2,str,n,0)<0) { perror("send"); done=1; }
}while(!done); exit(0); }
close(s2);
} return 0;
}
Client Program:
#include<stdio.h>
#include<stdlib.h>
#include<errno.h>
#include<string.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h> int main(int argc,char *argv[])
{
int s,t,len,a; struct
REGD.N0:-21K61A6101
if(s==-1) {
perror("socket");
exit(1);
}
printf("connected\n");
while(printf(">"),fgets(str,100,stdin),!feof(stdin))
{ if(send(s,str,strlen(str),0)==-1)
{ perror("send");
exit(1); } if((t=recv(s,str,100,0))>0)
{
str[t]='\0';
printf("echo>%s",str);
} else { if(t<0)
perror("recv");
else
printf("server closed connection\n"); exit(1);
} } close(s);
return 0;
}
RESULT:
REGD.N0:-21K61A6101
DESCRIPTION:
The User Datagram Protocol (UDP) is one of the core protocols of the Internet protocol
suite. It is a connectionless protocol, meaning that it does not establish a connection
between two hosts before data is transmitted. This makes UDP faster and more
lightweight than TCP, but it also means that UDP is less reliable. UDP does not guarantee
that data will be delivered in order or that it will be delivered at all.
UDPserver.c
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
int main() {
int sockfd;
char buffer[MAXLINE];
exit(EXIT_FAILURE);
REGD.N0:-21K61A6101
}
memset(&servaddr, 0, sizeof(servaddr));
memset(&cliaddr, 0, sizeof(cliaddr));
servaddr.sin_addr.s_addr = INADDR_ANY;
servaddr.sin_port = htons(PORT);
perror("bind failed");
exit(EXIT_FAILURE);
int len, n;
sockaddr *) &cliaddr,&len);
buffer[n] = '\0';
return 0;
UDPclient:
REGD.N0:-21K61A6101
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
int main() {
int sockfd;
char buffer[MAXLINE];
exit(EXIT_FAILURE);
servaddr.sin_family = AF_INET;
servaddr.sin_port = htons(PORT);
servaddr.sin_addr.s_addr = INADDR_ANY;
REGD.N0:-21K61A6101
int n, len;
sizeof(servaddr));
buffer[n] = '\0';
close(sockfd);
return 0;
RESULT:
REGD.N0:-21K61A6101
To implement system call.
DESCRIPTION:
System calls are typically implemented using a system call table (SCT), which is an array
of pointers to functions that implement the system calls. When an application makes a
system call, it provides an identifier that corresponds to the index of the function in the
system call table. The kernel then calls the appropriate function to handle the request.
PROGRAM:
#include <stdio.h>
#include <sys/time.h>
#include <sys/types.h>
#include <unistd.h>
int main(void) {
fd_set rfds;
int retval;
FD_ZERO(&rfds);
FD_SET(0, &rfds);
tv.tv_sec = 5;
tv.tv_usec = 0;
if (retval == -1)
perror("select()");
else if (retval)
REGD.N0:-21K61A6101
printf("Data is available now.\n");
else
return 0;
RESULT:
REGD.N0:-21K61A6101
To implement getsockopt(), setsockopt() system calls
DESCRIPTION:
Getsockopt and setsockopt manipulate the options associated with a socket. Options may exist at
multiple protocol levels; they are always present at the uppermost socket level. When
manipulating socket options the level at which the option resides and the name of the option must
be specified. To manipulate options at the socket level, level is specified as SOL_SOCKET. To
manipulate options at any other level the protocol number of the appropriate protocol controlling
the option is supplied
Server.c
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <netdb.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
int main() {
socklen_t clen;
else {
REGD.N0:-21K61A6101
else {
saddr.sin_family = AF_INET;
saddr.sin_addr.s_addr = inet_addr("127.0.0.1");
saddr.sin_port = htons(45454);
else {
else {
clen = sizeof(caddr);
accept");
else {
tout.tv_sec=0;
tout.tv_usec=10000;
else {
REGD.N0:-21K61A6101
printf ("all ok so far in server\n");
sleep(1);
return ret;
Client.c:
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <netdb.h>
#include <netinet/in.h>
#include <arpa/inet.h>
int main() {
REGD.N0:-21K61A6101
struct sockaddr_in addr;
int sock = 0;
else {
addr.sin_family = AF_INET;
addr.sin_port = htons(45454);
socket");
else {
server");
else {
sleep(3);
printf("done sleeping\n");
close(sock);
REGD.N0:-21K61A6101
return 0;
RESULT:
REGD.N0:-21K61A6101
DESCRIPTION:
Getpeername returns the name of the peer connected to socket s. The namelen parameter should
be initialized to indicate the amount of space pointed to by name. On return it contains the actual
size of the name returned(in bytes). The name is truncated if the buffer provided is too small.
Server.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <sys/socket.h>
void error_handling(char *message);
int main(int argc, char *argv[])
{
int serv_sock;
int clnt_sock;
struct sockaddr_in serv_addr;
struct sockaddr_in clnt_addr;
socklen_t clnt_addr_size;
char message[]="Hello World!";
if(argc!=2){
printf("Usage : %s <port>\n", argv[0]);
exit(1);
}
serv_sock=socket(PF_INET, SOCK_STREAM, 0);
REGD.N0:-21K61A6101
serv_addr.sin_port=htons(atoi(argv[1]));
if(bind(serv_sock, (struct sockaddr*) &serv_addr, sizeof(serv_addr))==-1 )
error_handling("bind() error");
if(listen(serv_sock, 5)==-1)
error_handling("listen() error");
clnt_addr_size=sizeof(clnt_addr);
clnt_sock=accept(serv_sock, (struct sockaddr*)&clnt_addr,&clnt_addr_size);
if(clnt_sock==-1)
error_handling("accept() error");
write(clnt_sock, message, sizeof(message));
struct sockaddr_in addr1;
struct sockaddr_in addr2;
struct sockaddr_in addr3;
struct sockaddr_in addr4;
socklen_t serv_len = sizeof(serv_addr);
int serv_peer_err = getpeername(serv_sock, (struct sockaddr *)&addr1, &serv_len);
int clnt_peer_err = getpeername(clnt_sock, (struct sockaddr *)&addr2, &clnt_addr_size);
int serv_sock_err = getsockname(serv_sock, (struct sockaddr *)&addr3, &serv_len);
int clnt_sock_err = getsockname(clnt_sock, (struct sockaddr *)&addr4, &clnt_addr_size);
printf("Server socket's ip : %s\n", inet_ntoa(addr3.sin_addr));
printf("Server socket's port : %d\n", ntohs(addr3.sin_port));
printf("Server socket's peer ip : %s\n", inet_ntoa(addr1.sin_addr));
printf("Server socket's peer port : %d\n\n\n\n\n\n", ntohs(addr1.sin_port));
printf("Client socket's ip : %s\n", inet_ntoa(addr4.sin_addr));
printf("Client socket's port : %d\n", ntohs(addr4.sin_port));
REGD.N0:-21K61A6101
return 0;
}
void error_handling(char *message)
{
fputs(message, stderr);
fputc('\n', stderr);
exit(1);
}
Client.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <sys/socket.h>
void error_handling(char *message);
int main(int argc, char* argv[])
{
int sock;
struct sockaddr_in serv_addr;
char message[30];
int str_len;
if(argc!=3){
printf("Usage : %s <IP> <port>\n", argv[0]);
exit(1);
REGD.N0:-21K61A6101
serv_addr.sin_family=AF_INET;
serv_addr.sin_addr.s_addr=inet_addr(argv[1]);
serv_addr.sin_port=htons(atoi(argv[2]));
if(connect(sock, (struct sockaddr*)&serv_addr, sizeof(serv_addr))==-1)
error_handling("connect() error!");
str_len=read(sock, message, sizeof(message)-1);
if(str_len==-1)
error_handling("read() error!");
struct sockaddr_in addr1;
struct sockaddr_in addr2;
int clnt_sock_err, clnt_peer_err;
socklen_t serv_len = sizeof(serv_addr);
clnt_peer_err = getpeername(sock, (struct sockaddr *)&addr1, &serv_len);
clnt_sock_err = getsockname(sock, (struct sockaddr *)&addr2, &serv_len);
printf("Client socket's ip : %s\n", inet_ntoa(addr2.sin_addr));
printf("client socket's port %d\n", ntohs(addr2.sin_port));
printf("Client socket's peer ip : %s\n", inet_ntoa(addr1.sin_addr));
printf("Client socket's peer port : %d\n", ntohs(addr1.sin_port));
printf("%d %d\n", clnt_sock_err, clnt_peer_err);
printf("Message from server: %s \n", message);
close(sock);
return 0;
}
void error_handling(char *message)
{
REGD.N0:-21K61A6101
RESULT:
REGD.N0:-21K61A6101
DESCRIPTION:
Remote command execution (RCE) is a security vulnerability that allows an attacker to
execute arbitrary commands on a remote system. This vulnerability can be exploited to
gain unauthorized access to the system, steal data, or install malware.One common way
to implement RCE is using socket system calls. Sockets are a mechanism for inter-
process communication (IPC) that allows processes to communicate with each other over
a network. Socket system calls can be used to create a socket, bind it to an address, listen
for incoming connections, accept connections, and send and receive data
Server.c:
#include <sys/types.h>
#include <sys/socket.h>
#include <stdio.h>
#include <stdlib.h>
#include <netdb.h>
#include <netinet/in.h>
#include <string.h>
#include <sys/stat.h>
#include <arpa/inet.h>
#include <unistd.h>
int main()
int size;
REGD.N0:-21K61A6101
socklen_t clientLength = sizeof(clientAddress);
bzero(&serverAddress, sizeof(serverAddress));
serverAddress.sin_family = AF_INET;
serverAddress.sin_addr.s_addr = htonl(INADDR_ANY);
serverAddress.sin_port = htons(9976);
while (1)
bzero(buffer, sizeof(buffer));
&clientLength);
system(buffer);
clientLength);
close(serverDescriptor);
return 0;
#include <sys/types.h>
#include <sys/socket.h>
#include <stdio.h>
REGD.N0:-21K61A6101
#include <unistd.h>
#include <netdb.h>
#include <netinet/in.h>
#include <string.h>
#include <arpa/inet.h>
int main()
bzero(&serverAddress, sizeof(serverAddress));
serverAddress.sin_family = AF_INET;
serverAddress.sin_addr.s_addr = inet_addr("127.0.0.1");
serverAddress.sin_port = htons(9976);
while (1)
serverLength);
REGD.N0:-21K61A6101
printf("\nData Sent !");
&serverLength);
return 0;
RESULT:
REGD.N0:-21K61A6101
To implement of Distance vector routing algorithm
DESCRIPTION:
The Distance Vector Routing Algorithm (DVRA), also known as the Bellman-Ford algorithm,
is a routing protocol used in computer networks to determine the best path for data
packets to travel. It is a distributed algorithm, meaning that each router in the network
maintains its own routing table and periodically exchanges this information with its
neighbors.
PROGRAM:
import java.io.*;
static int v;
static int e;
v = Integer.parseInt(br.readLine());
Sasi institute of technology and engineering 34
System.out.println("Please enter the number of Edges: ");
e = Integer.parseInt(br.readLine());
REGD.N0:-21K61A6101
rt = new int[v][v];
if(i == j)
graph[i][j] = 0;
else
graph[i][j] = 9999;
System.out.print("Source: ");
int s = Integer.parseInt(br.readLine());
s--;
System.out.print("Destination: ");
int d = Integer.parseInt(br.readLine());
d--;
System.out.print("Cost: ");
graph[s][d] = c;
graph[d][s] = c;
REGD.N0:-21K61A6101
dvr_calc_disp("The initial Routing Tables are: ");
System.out.print("Please enter the Source Node for the edge whose cost has changed: ");
int s = Integer.parseInt(br.readLine());
s--;
System.out.print("Please enter the Destination Node for the edge whose cost has
changed: ");
int d = Integer.parseInt(br.readLine());
d--;
int c = Integer.parseInt(br.readLine());
graph[s][d] = c;
graph[d][s] = c;
System.out.println();
init_tables();
update_tables();
System.out.println(message);
Sasi institute of technology and engineering 36
print_tables();
System.out.println();
REGD.N0:-21K61A6101
{
if(graph[source][i] != 9999)
if(via[i][j] == source)
inter_dist = 9999;
via[source][j] = i;
int k = 0;
REGD.N0:-21K61A6101
for(int i = 0; i < 4*v; i++)
update_table(k);
k++;
if(k == v)
k = 0;
if(i == j)
rt[i][j] = 0;
via[i][j] = i;
rt[i][j] = 9999;
via[i][j] = 100;
REGD.N0:-21K61A6101
}
System.out.println();
RESULT:
REGD.N0:-21K61A6101
To implement of SMTP.
ALGORITHM:
InternetAddress("XXXXXX", "XXXXX");
2008", "text/plain");
PROGRAM:
import java.util.Properties;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
REGD.N0:-21K61A6101
private String smtpHost = "localhost";
public void sendMessage(String from, String[] recipients, String subject, String message)
throws MessagingException {
props.put("mail.smtp.host", smtpHost);
session.setDebug(debug);
// create a message
msg.setFrom(addressFrom);
msg.setSubject(subject);
msg.setContent(message, "text/html");
REGD.N0:-21K61A6101
Transport.send(msg);
return smtpHost;
this.smtpHost = smtpHost;
RESULT:
REGD.N0:-21K61A6101
To implementation of FTP
ALGORITHM:
3. The server receives filename. If file is present, server starts reading file and continues to
send
FTPclient.java:
import java.net.*;
import java.io.*;
import java.util.*;
class FTPClient
class transferfileClient
REGD.N0:-21K61A6101
{
Socket ClientSoc;
DataInputStream din;
DataOutputStream dout;
BufferedReader br;
transferfileClient(Socket soc)
try
ClientSoc=soc;
din=new DataInputStream(ClientSoc.getInputStream());
dout=new DataOutputStream(ClientSoc.getOutputStream());
catch(Exception ex)
String filename;
filename=br.readLine();
REGD.N0:-21K61A6101
File f=new File(filename);
if(!f.exists())
return;
dout.writeUTF(filename);
String msgFromServer=din.readUTF();
String Option;
Option=br.readLine();
if(Option=="Y")
dout.writeUTF("Y");
dout.writeUTF("N");
return;
REGD.N0:-21K61A6101
}
int ch;
do
ch=fin.read();
dout.writeUTF(String.valueOf(ch));
while(ch!=-1);
fin.close();
System.out.println(din.readUTF());
String fileName;
fileName=br.readLine();
String msgFromServer=din.readUTF();
REGD.N0:-21K61A6101
System.out.println("File not found on Server ...");
return;
elseif(msgFromServer.compareTo("READY")==0)
if(f.exists())
String Option;
Option=br.readLine();
if(Option=="N")
dout.flush();
return;
String temp;
do
REGD.N0:-21K61A6101
temp=din.readUTF();
ch=Integer.parseInt(temp);
if(ch!=-1)
fout.write(ch);
}while(ch!=-1);
fout.close();
System.out.println(din.readUTF());
while(true)
System.out.println("3. Exit");
int choice;
choice=Integer.parseInt(br.readLine());
if(choice==1)
REGD.N0:-21K61A6101
{
dout.writeUTF("SEND");
SendFile();
elseif(choice==2)
dout.writeUTF("GET");
ReceiveFile();
else
dout.writeUTF("DISCONNECT");
System.exit(1);
FTPserver.java:
import java.net.*;
import java.util.*;
class FTPClient
REGD.N0:-21K61A6101
publicstaticvoid main(String args[]) throws Exception
t.displayMenu();
class transferfileClient
Socket ClientSoc;
DataInputStream din;
DataOutputStream dout;
BufferedReader br;
transferfileClient(Socket soc)
try
ClientSoc=soc;
din=new DataInputStream(ClientSoc.getInputStream());
catch(Exception ex)
REGD.N0:-21K61A6101
{
String filename;
filename=br.readLine();
if(!f.exists())
return;
dout.writeUTF(filename);
String msgFromServer=din.readUTF();
Option=br.readLine();
if(Option=="Y")
REGD.N0:-21K61A6101
{
dout.writeUTF("Y");
else
dout.writeUTF("N");
return;
int ch;
do
ch=fin.read();
dout.writeUTF(String.valueOf(ch));
while(ch!=-1);
fin.close();
REGD.N0:-21K61A6101
String fileName;
fileName=br.readLine();
dout.writeUTF(fileName);
String msgFromServer=din.readUTF();
return;
elseif(msgFromServer.compareTo("READY")==0)
if(f.exists())
String Option;
Option=br.readLine();
dout.flush();
return;
REGD.N0:-21K61A6101
}
int ch;
String temp;
do
temp=din.readUTF();
ch=Integer.parseInt(temp);
if(ch!=-1)
fout.write(ch);
}while(ch!=-1);
fout.close();
System.out.println(din.readUTF());
while(true)
REGD.N0:-21K61A6101
System.out.println("1. Send File");
System.out.println("3. Exit");
int choice;
choice=Integer.parseInt(br.readLine());
if(choice==1)
dout.writeUTF("SEND");
SendFile();
elseif(choice==2)
dout.writeUTF("GET");
ReceiveFile();
else
dout.writeUTF("DISCONNECT");
REGD.N0:-21K61A6101
}
RESULT:
REGD.N0:-21K61A6101
To implementation of HTTP
ALGORITHM:
5. Close socket:
client.close();
server.java:
import java.net.*;
import java.io.*;
Socket s1=s.accept();
REGD.N0:-21K61A6101
socket OutputStream s1out = s1.getOutputStream();
dos.writeUTF( Hi there ); // Close the connection, but not the server socket
dos.close();
s1out.close();
s1.close(); } }
client.java:
import java.net.*;
import java.io.*;
// Get an input file handle from the socket and read the input
System.out.println(st);
dis.close();
s1In.close();
s1.close();
REGD.N0:-21K61A6101
}
RESULT:
REGD.N0:-21K61A6101
ALGORITHM:
repeat
p ← genprime(k/2)
until (pmode)≠1(pmode)≠1
repeat
q ← genprime(k - k/2)
until (qmode)≠1(qmode)≠1
N ← pq
L ← (p-1)(q-1)
d ← modinv(e, L)
return (N,e,d)
PROGRAM:
import java.io.DataInputStream;
import java.io.IOException;
import java.math.BigInteger;
Sasi institute of technology and engineering 60
import java.util.Random;
private BigInteger p;
REGD.N0:-21K61A6101
private BigInteger q;
private BigInteger N;
private BigInteger e;
private BigInteger d;
private Random r;
public RSA()
r = new Random();
p = BigInteger.probablePrime(bitlength, r);
q = BigInteger.probablePrime(bitlength, r);
N = p.multiply(q);
phi = p.subtract(BigInteger.ONE).multiply(q.subtract(BigInteger.ONE));
e = BigInteger.probablePrime(bitlength / 2, r);
e.add(BigInteger.ONE);
d = e.modInverse(phi);
REGD.N0:-21K61A6101
{
this.e = e;
this.d = d;
this.N = N;
@SuppressWarnings("deprecation")
String teststring;
teststring = in.readLine();
REGD.N0:-21K61A6101
for (byte b : encrypted){
test += Byte.toString(b);
return test;
RESULT: