You are on page 1of 32

Socket Programming Lab 15MCA407L

Shashank Jain 17MCAR0016_


CIA-1
Q) Creation of Date server and printing clients address or server.
DateServer
1. Start .
2. Create a ServerSocket, Socket, PrintStream, String Inet and
DataInputStrem.
3. Socket accepts ServerSocket and get output or
4. Date creates object d and print the server date.
5. Wait for server's client system address and accept it.
6. Create a datagram packet and send message through server port.
7. Read Client's system address.
8. Close the server socket.
9. Stop.
DateClient
1. Start
2. Create server, DataInputStream , String sdate and PrintStream.
3. Get localHost and pass the IP address.
4. Create a datagram socket and bind it to client port.
5. Wait for client's data and accept it.
7.client read the sdate and print the server date.
8. Close the client socket.
9. Stop.

Coding:
DateServer.java:
import java.net.*;
import java.io.*;
import java.util.*;
public class DateServer {
public static void main(String[] args) {
ServerSocket ss; Socket s;

1|Page
Department of MCA-JGI Signature of Faculty
Socket Programming Lab 15MCA407L
Shashank Jain 17MCAR0016_
PrintStream ps; DataInputStream dis;
String inet;
try
{
ss=new ServerSocket(8020);
while(true)
{
s=ss.accept();
ps= new PrintStream(s.getOutputStream());
Date d=new Date();
ps.println(d);
dis=new DataInputStream(s.getInputStream());
inet=dis.readLine();
System.out.println("THE CLIENT SYSTEM ADDRESS"+inet);
ps.close();
} }
catch(Exception e)
{
System.out.println("the exception is"+e);
} }}

Output:

2|Page
Department of MCA-JGI Signature of Faculty
Socket Programming Lab 15MCA407L
Shashank Jain 17MCAR0016_

DateClient.java:
import java.net.*;
import java.io.*;
public class DateClient {
public static void main(String[] args) {
Socket soc;
DataInputStream dis;
String sdate;
PrintStream ps;
try
{
InetAddress ia=InetAddress.getLocalHost();
soc=new Socket(ia,8020);
dis=new DataInputStream(soc.getInputStream());
sdate=dis.readLine();
System.out.println("THE DATE IN THE SERVER IS "+sdate);
ps=new PrintStream(soc.getOutputStream());
ps.println(ia);
}

3|Page
Department of MCA-JGI Signature of Faculty
Socket Programming Lab 15MCA407L
Shashank Jain 17MCAR0016_
catch(Exception e)
{
System.out.println(e);
}} }

Output:

4|Page
Department of MCA-JGI Signature of Faculty
Socket Programming Lab 15MCA407L
Shashank Jain 17MCAR0016_
CIA-2
Q) Program for Creation of Web Browser Cookies.
Algorithm:
1. Start the program.
2. A simple HTML form is designed and javascript is used to demonstrate the
simple browser cookie
3. The set cookie button prompts WriteCookie() method, which gets a cookie
value from the user and stores it as plain text in the browser, which is
remembered till the cookie expires.
4. The name value is obtained from the user in “Enter the name” text box.
5. JavaScript escape() function encodes the value before storing it in cookie.
6. The cookie is created using the function: document.cookie = "value"; Here
string value is assigned to document.cookie object.
7. The cookies are set and thus obtained using name-value pairs.
8 End the program.
Code:-
<html>
<head>
<script type="text/javascript">

function WriteCookies()
{
if(document.myform.customer.value=="")
{
alert("enter some value");
return;
}
cookiesvalue=escape(document.myform.customer.value)+";";
document.cookies="name="+cookiesvalue;
document.write("Setting Cookies:"+"name="+cookiesvalue);
5|Page
Department of MCA-JGI Signature of Faculty
Socket Programming Lab 15MCA407L
Shashank Jain 17MCAR0016_
}
</script>
</head>
<body>

<form name="myform" action="">


Enter name:<input type="text" name="customer"/>
<input type="button" value="set Cookies" onclick="WriteCookies();"/>
</form>
</body>
</html>

OUTPUT:-

6|Page
Department of MCA-JGI Signature of Faculty
Socket Programming Lab 15MCA407L
Shashank Jain 17MCAR0016_
CIA-3
Q) Program for Creation of Distributed application using RMI.
Algorithm:
1. Start a program.
2. Including the header libraries.
3. Create the remote interface(RMI) and provide the implementation of the RMI.
4. Compiler the implementation class and create the stub and skeleton objects using
the RMI tools.
5. Start the registry service by misregistery tools.
6. Create and start the remote application.
7. Create and start the both server or client application.
8. Run the three command prompt i.e., rmiregistry, server or client.
9. Execute it and stop the program.
Coding:
ArithImpl.java:
import java.rmi.*;
import java.rmi.server.*;
public class ArithImpl extends UnicastRemoteObject implements ArithIntf
{
public ArithImpl() throws RemoteException
{
}
public int add(int a, int b) throws RemoteException
{
return a+b;
} }
ArithIntrf.java:
import java.rmi.*;

7|Page
Department of MCA-JGI Signature of Faculty
Socket Programming Lab 15MCA407L
Shashank Jain 17MCAR0016_
public interface ArithIntf extends Remote
{
public int add(int a, int b) throws RemoteException;
}

ArithServer.java:
import java.rmi.*;
public class ArithServer
{
public static void main(String[] args) throws Exception
{
ArithImpl server = new ArithImpl();
Naming.rebind("arith", server);
}}
ArithClient.java:
import java.rmi.*;
import java.io.*;
import java.util.Scanner;
public class ArithClient
{
public static void main(String[] args) throws Exception
{
ArithIntf client=(ArithIntf)Naming.lookup("rmi://127.0.0.1/arith");
System.out.println("enter the numbers");
Scanner in = new Scanner(System.in);
8|Page
Department of MCA-JGI Signature of Faculty
Socket Programming Lab 15MCA407L
Shashank Jain 17MCAR0016_
int r=client.add(10,20);
System.out.println(r);}}
Output:

9|Page
Department of MCA-JGI Signature of Faculty
Socket Programming Lab 15MCA407L
Shashank Jain 17MCAR0016_
CIA-4
Q): To implement a chat server and client in java using UDP sockets.
Algorithm:

Server
1. Create two ports, server port and client port.
2. Create a datagram socket and bind it to client port.
3. Create a datagram packet to receive client message.
4. Wait for client's data and accept it.
5. Read Client's message.
6. Get data from user.
7. Create a datagram packet and send message through server port.
8. Repeat steps 3-7 until the client has something to send.
9. Close the server socket.
10. Stop.
Client
1. Create two ports, server port and client port.
2. Create a datagram socket and bind it to server port.
3. Get data from user.
4. Create a datagram packet and send data with server ip address and client port.
5. Create a datagram packet to receive server message.
6. Read server's response and display it.
7. Repeat steps 3-6 until there is some text to send.
8. Close the client socket.
9. Stop.

Coding:
(Server. Java)
import java.io.*;
import java.net.*;
importjava.util.*;
public class UDPSocketServer
{
public static void main(String[] args)
{
DatagramSocket socket=null;
10 | P a g e
Department of MCA-JGI Signature of Faculty
Socket Programming Lab 15MCA407L
Shashank Jain 17MCAR0016_
Scanner sc = new Scanner(System.in);
try
{
//Assigning the Port...
socket = new DatagramSocket(9800);

//getting the data in byte format..


byte[] incomingData = new byte[10];

while(true)
{
DatagramPacketincomingPacket = new
DatagramPacket(incomingData,incomingData.length);
socket.receive(incomingPacket);

//Print thr Message from Client;


String msg = new
String(incomingPacket.getData());
System.out.print("Client: " + msg);

InetAddressIPAdd =
incomingPacket.getAddress();
int port =incomingPacket.getPort();

System.out.print("\n");
System.out.print("Server:- ");
String reply=sc.nextLine();
byte[] data = reply.getBytes();

DatagramPacketreplyPacket = new
DatagramPacket(data,data.length,IPAdd,port);
socket.send(replyPacket);
Thread.sleep(2000);

11 | P a g e
Department of MCA-JGI Signature of Faculty
Socket Programming Lab 15MCA407L
Shashank Jain 17MCAR0016_
}
//socket.close();
}
catch (SocketException e)
{
e.printStackTrace();
}
catch (IOExceptioni)
{
i.printStackTrace();
}
catch(InterruptedExceptionie)
{
ie.printStackTrace();
}
}
}

(Client.java)
import java.io.*;
import java.net.*;
importjava.util.*;
public class UDPSocketClient
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);

try
{
while(true)
{
DatagramSocket socket=null;
12 | P a g e
Department of MCA-JGI Signature of Faculty
Socket Programming Lab 15MCA407L
Shashank Jain 17MCAR0016_
socket = new DatagramSocket();
InetAddressIPAdd = InetAddress.getLocalHost();
byte[] incomingdata = new byte[10];

System.out.print("Client:- ");
String msg=sc.nextLine();
byte[] data = msg.getBytes();

DatagramPacketsendpacket = new
DatagramPacket(data,data.length,IPAdd,9800);
socket.send(sendpacket);

DatagramPacketincomingPacket = new
DatagramPacket(incomingdata,incomingdata.length);
socket.receive(incomingPacket);

String response = new


String(incomingPacket.getData());
System.out.print("Server :- " + response);
System.out.print("\n");
}}
catch(Exception e)
{
e.printStackTrace();
}
}}

Output:

13 | P a g e
Department of MCA-JGI Signature of Faculty
Socket Programming Lab 15MCA407L
Shashank Jain 17MCAR0016_

14 | P a g e
Department of MCA-JGI Signature of Faculty
Socket Programming Lab 15MCA407L
Shashank Jain 17MCAR0016_
CIA-5
Q) Program for Creation of Simple Telnet Protocol.
Algorithm:

1. Start the program.


2. This program Telnet to the server system by logging into the server
system.
3. Whenever the server gets request from the client, it responds back to
the client to input both ‘user name’ and ‘Password’.
4. Thus the feedback message from is: “Login” and “Password:”
5. Telnet being a bidirectional protocol, it takes few milliseconds for the client
to receive feedback from the server.
6. End the Program.

Coding:
TelnetServer.java:
import java.io.*;
import java.net.*;
import java.util.*;
class TelnetServer {
public static void main(String args[]) throws Exception {
ServerSocket ss=new ServerSocket(8020);
Socket s=ss.accept();
ServiceClient(s);
}
public static void ServiceClient(Socket s)throws Exception {
DataInputStream dis=null;
PrintStream ps=null;
try
{ dis=new DataInputStream(s.getInputStream());
15 | P a g e
Department of MCA-JGI Signature of Faculty
Socket Programming Lab 15MCA407L
Shashank Jain 17MCAR0016_
ps=new PrintStream(s.getOutputStream());
String tel=dis.readLine();
System.out.println(tel);
ps.println("Hello Telnet Started");
ps.println("Login :");
String uname=dis.readLine();
System.out.println(uname);
ps.println("password :");
String pass=dis.readLine();
System.out.println(pass);

if((pass.equals("admin")) && (uname.equals("admin")))


ps.println("Ok Server Accepted");
while(true)
{ String cmd=dis.readLine();
if(cmd.equals("quit")) {
ps.println("bye");
break;
}
else if(cmd.equals("dir")) {
File f=new File("\\");
String files[]=f.list();
for(int i=0;i<files.length;i++) {
ps.println(files[i]);
}
16 | P a g e
Department of MCA-JGI Signature of Faculty
Socket Programming Lab 15MCA407L
Shashank Jain 17MCAR0016_
ps.println("end");
break;
} } }
finally {
ps.close();
dis.close();
s.close();
} } }

Output:

TelnetClent.java:
import java.io.*;
import java.net.*;
import java.util.*;
class TelnetClient {
public static void main(String args[]) throws Exception
{
Socket s=new Socket("localhost",8020);

17 | P a g e
Department of MCA-JGI Signature of Faculty
Socket Programming Lab 15MCA407L
Shashank Jain 17MCAR0016_
DataInputStream din=new DataInputStream(s.getInputStream());
PrintStream ps=new PrintStream(s.getOutputStream());
DataInputStream in = new DataInputStream(System.in);
ps.println("Telnet");
String str=din.readLine();
System.out.println(str);
String str1=din.readLine();
System.out.println(str1);
String uname=in.readLine();
ps.println(uname);
String str2=din.readLine();
System.out.println(str2);

String pass=in.readLine();
ps.println(pass);
String acc=din.readLine();
System.out.println(acc);
String cmd=in.readLine();
ps.println(cmd);
if(cmd.equals("dir"))
{ while(true)
{
String rec=din.readLine();
if(rec.equals("end"))
break;
18 | P a g e
Department of MCA-JGI Signature of Faculty
Socket Programming Lab 15MCA407L
Shashank Jain 17MCAR0016_
System.out.println(rec);
} }
else {
String quit=din.readLine();
System.out.println(quit);
} } }

Output:

19 | P a g e
Department of MCA-JGI Signature of Faculty
Socket Programming Lab 15MCA407L
Shashank Jain 17MCAR0016_
CIA-6
Q) Calculating Checksum for Packet data and file.
Algorithm:
CRCServer
1. Start the program.
2. Declare the variables for the socket and specify the family, protocol, IPaddress and
port number.
3. Create a socket using socket() methods and bind the ipaddress and accept the
client’s request for the connection.
4. Display the client’s request message and CRC value and execute it.
5. Stop the program.
CRCClient
1. Start the program.
2. Declare the variables for the socket and specify the family, protocol, Ipaddress and
portnumber.
3. Create the socket using socket() function.
4. Call the connect and read the server response and execute it.
5. End the program.

Program
CRCserver.java:
package crcserver;
import java.util.zip.*;
import java.util.*;
import java.io.*;
import java.net.*;
public class CRCserver {
public static void main(String args[ ]) {
try {
CRC32 c=new CRC32();
20 | P a g e
Department of MCA-JGI Signature of Faculty
Socket Programming Lab 15MCA407L
Shashank Jain 17MCAR0016_
ServerSocket ss=new ServerSocket(8000);
Socket s;
String str=" ";
String sis[]=new String[500];
while(true) {
s=ss.accept();
DataInputStream dis=new DataInputStream(s.getInputStream());
str=dis.readLine();
System.out.println("the message with the Crc value received is \n");
System.out.println(str);
StringTokenizer st=new StringTokenizer(str,"//");
int n=st.countTokens();
System.out.println("number of tokens"+n);

System.out.println("the tokens received are");


for(int i=0;i<n;i++) {
sis[i]=st.nextToken();
System.out.println(sis[i]);
}
long val1=Long.parseLong(sis[n-1]);
String s1=Long.toString(val1);
System.out.println("The calculate CRC value is");
System.out.println(val1);
c.update(sis[0].getBytes());
long val=c.getValue();
21 | P a g e
Department of MCA-JGI Signature of Faculty
Socket Programming Lab 15MCA407L
Shashank Jain 17MCAR0016_
String s2=Long.toString(val);
if(s1.equals(s2)) {
String s3="Hello good morning Swapna";
PrintStream ps=new PrintStream(s.getOutputStream());
ps.println(s3);
}
System.out.println("the message from client is acepted");
} }
catch(Exception e) {
System.out.println(e); } }}

Output:

CRCclient.java:
22 | P a g e
Department of MCA-JGI Signature of Faculty
Socket Programming Lab 15MCA407L
Shashank Jain 17MCAR0016_
package crccclient;
import java.io.*;
import java.util.zip.*;
import java.net.*;
public class CRCCclient {
public static void main(String args[]) {
try {
InetAddress ia=InetAddress.getLocalHost();
CRC32 cc=new CRC32();
System.out.println("PLEASE ENTER THE MESAGE");
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));

String s=br.readLine();
PrintStream ps;
String m;
Socket ss=new Socket(ia,8000);
cc.update(s.getBytes());
long val=cc.getValue();
ps=new PrintStream(ss.getOutputStream());
m=s+"//"+val;
System.out.println("THE MESSAGE AND THE CRC VALUE IS");
System.out.println(m);
ps.println(m);
DataInputStream dis=new DataInputStream(ss.getInputStream());
String str=dis.readLine();
23 | P a g e
Department of MCA-JGI Signature of Faculty
Socket Programming Lab 15MCA407L
Shashank Jain 17MCAR0016_
System.out.println("THE ACKNOWLEDGEMENT RECEIVED FROM SERVER IS \n");
System.out.println(str);
}
catch(Exception e)
{
System.out.println(e);
}
}
}

Output:

24 | P a g e
Department of MCA-JGI Signature of Faculty
Socket Programming Lab 15MCA407L
Shashank Jain 17MCAR0016_
CIA-7
Q) Program for Creation of Simple Web Server.
Algorithm:
WebServer
1. Start the program
2. Create server socket object.
3. Server will s.accpt() the client request the response it.
4. Call the server service methods to provide a server to client.
5. End the program.
WebClient
1. Start the program
2. Create a socket with localhost = 8020 parameter.
3. Call the get page method with socket object as parameter.
4. Create DataInputStream or DataOutputStream object of classes.
5. Call the function and client request passing for server.
6. End the program.

Coding:
WebServer.java:
import java.io.*;
import java.net.*;
class WebServer {
public static void main(String args[ ]) {
int connects=0;
try {
ServerSocket ss=new ServerSocket(8020,5);
while (connects < 5) {
Socket s=ss.accept();
ServiceClient(s);
25 | P a g e
Department of MCA-JGI Signature of Faculty
Socket Programming Lab 15MCA407L
Shashank Jain 17MCAR0016_
connects++;
}
ss.close();
}
catch (Exception e) {
System.out.println(e);
} }
public static void ServiceClient(Socket client) throws IOException {
try {
DataInputStream dis = new DataInputStream(client.getInputStream());
DataOutputStream dos = new DataOutputStream(client.getOutputStream());

StringBuffer buf = new StringBuffer("<html><body><p>Welcome to Simple Web Page


</body></html>");

dos.writeBytes(buf.toString());
}
finally {
System.out.println("Cleaning up Connection : " +client);
client.close();
} } }

Output:

26 | P a g e
Department of MCA-JGI Signature of Faculty
Socket Programming Lab 15MCA407L
Shashank Jain 17MCAR0016_

WebClent.java:
import java.io.*;
import java.net.*;
class WebClient {
public static void main (String args[]) {
try {
Socket s1 = new Socket ("localhost",8020);
System.out.println("Client1 : " +s1);
getPage(s1);
}
catch (UnknownHostException e) {
System.out.println(e);
}
catch (IOException e) {
System.out.println(e);
} }
public static void getPage(Socket s) {
try {

27 | P a g e
Department of MCA-JGI Signature of Faculty
Socket Programming Lab 15MCA407L
Shashank Jain 17MCAR0016_
DataOutputStream dos = new DataOutputStream(s.getOutputStream());
DataInputStream dis = new DataInputStream(s.getInputStream());
dos.writeBytes("GET/HTTP/1.0\r\n\r\n");
String responseLine;
responseLine=dis.readLine();
System.out.println(responseLine);
dos.close();
dis.close();
s.close();
}
catch(IOException e) {
System.out.println(e);
} } }

Output:

28 | P a g e
Department of MCA-JGI Signature of Faculty
Socket Programming Lab 15MCA407L
Shashank Jain 17MCAR0016_
CIA-8
Q) Program for Creation of simple Mail Client.
Algorithm:
SMTPServer
1. Start the program.
2. Create server socket class object i.e., ss with parameter passes port
number i.e., 8050.
3. Create again object ,it will accept the request of client.
4. Call the method or function of service client.
5. To implement filewrite object to create a mail file upto write inside.
6. Execute it and stop the program.
SMTPClient
1. Start the program.
2. Create socket s and datastream class object.
3. Create printstream class object.
4. When client enter the quit command then it wioo over the writing.
5. Execute it and stop the program.

Coding:
SMTPserver.java:
import java.io.*;
import java.net.*;
public class SMTPserver {
public static void main(String args[]) {
try {
ServerSocket ss=new ServerSocket(8050);
Socket s=ss.accept();
ServiceClient(s);
}
catch(Exception e)
29 | P a g e
Department of MCA-JGI Signature of Faculty
Socket Programming Lab 15MCA407L
Shashank Jain 17MCAR0016_
{ System.out.println("Exception caught in main:"+e); }
}
public static void ServiceClient(Socket s)throws Exception
{
try
{ DataInputStream dis=null;
PrintStream ps=null;
dis=new DataInputStream(s.getInputStream());
ps=new PrintStream(s.getOutputStream());
FileWriter f=new FileWriter("abc.eml");
String tel=dis.readLine();
if(tel.equals("Ready"))
System.out.println("Ready Signal received from client:Client Accepted");
ps.println("Enter the from address:");
String from=dis.readLine();
f.write("from:"+from+"\n");
ps.println("Enter the To address:");
String to=dis.readLine();
f.write("to:"+to+"\n");
ps.println("Enter the Message");
String msg=dis.readLine();
System.out.println(msg);
f.write("\n");
f.write("Message:"+msg+"\n");
f.close();}
30 | P a g e
Department of MCA-JGI Signature of Faculty
Socket Programming Lab 15MCA407L
Shashank Jain 17MCAR0016_
catch(Exception e)
{ System.out.println("Exception caught:"+e);
} } }

Output:

SMTPClent.java:
import java.io.*;
import java.net.*;
public class SMTPclient {
public static void main(String args[ ])throws Exception
{
try {
Socket s=new Socket("localhost",8050);
DataInputStream dis=new DataInputStream(s.getInputStream());
DataInputStream in=new DataInputStream(System.in);
PrintStream ps=new PrintStream(s.getOutputStream());
ps.println("Ready");
String resp=dis.readLine();
System.out.println("Enter the from address:");
String strf=in.readLine();
ps.println(strf);

31 | P a g e
Department of MCA-JGI Signature of Faculty
Socket Programming Lab 15MCA407L
Shashank Jain 17MCAR0016_
System.out.println("Enter the to Address:");
String strt=in.readLine();
ps.println(strt);
System.out.println("Enter the msg");
while(true) {
String msg=in.readLine();
ps.println(msg);
if(msg.equals("quit"))
{System.out.println("message is delivered to server and clients quits");
break;
} } }
catch(Exception e) {
System.out.println("Exception caught:"+e);
} } }

Output:

32 | P a g e
Department of MCA-JGI Signature of Faculty

You might also like