37-Afzal khan-Ajp-Exp 16
37-Afzal khan-Ajp-Exp 16
37-Afzal khan-Ajp-Exp 16
I. Practical Significance:
Java provides the socket programming approach for communication between the
client and server. A user can write the code for both client and server as well for UDP
& TCP datagram packets. By using java’s network communication feature we can
create interactive application to communicate within a network.
The java.net.Socket class is used to communicate between client and server.The client
can obtain object by creating its instance whereas the server obtains a Socket object
from the return value of the accept() method.
Nil
Sr.
Name of Resource Broad Specification Quantity Remarks (If any)
No.
1
Code:-
Server Side:
import java.io.*;
import java.net.*;
class MyServer {
private MyServer() {}
public static void main(String[] args) throws IOException {
ServerSocket s = new ServerSocket(2019);
System.out.println("Server Started, waiting for client");
Socket s1 = s.accept();
BufferedReader br = new BufferedReader(new InputStreamReader(s1.getInputStream()));
String user = br.readLine();
String pass = br.readLine();
OutputStream out = s1.getOutputStream();
PrintStream ps = new PrintStream(out);
if (user.equals("abc") && pass.equals("1234"))
{
ps.println("Validated authenticity");
} else {
ps.println("Validation failure");
}}}
Client Side:-
import java.io.*;
import java.net.*;
public class MyClient {
private MyClient() {}
public static void main(String[] args) throws IOException {
Socket s = new Socket("localhost", 2019);
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter Username and Password: ");
String user = br.readLine();
String pass = br.readLine();
OutputStream os = s.getOutputStream();
PrintStream ps = new PrintStream(os);
ps.println(user);
ps.println(pass);
BufferedReader br1 =
new BufferedReader(new InputStreamReader(s.getInputStream()));
String res = br1.readLine();
System.out.println(res);
}}
Output:-
XIII. Exercise:
1. Write a program using Socket and ServerSocket to create Chat Application
2. Write a program to develop prime number Server (Client will send any number to
server, Sever will send the response the number is prime or not)
(Space for Answer)
1. Code:-
Client Side::
import java.net.*;
import java.io.*;
class MyClient1 {
public static void main(String args[])throws Exception {
Socket s=new Socket("localhost",3333);
DataInputStream din=new DataInputStream(s.getInputStream());
DataOutputStream dout=new DataOutputStream(s.getOutputStream());
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
String str="",str2="";
while(!str.equals("stop")) {
str=br.readLine();
dout.writeUTF(str);
dout.flush();
str2=din.readUTF();
System.out.println("Server says: "+str2);
}
dout.close();
s.close();
}}
Server Side:
import java.net.*;
import java.io.*;
class MyServer1 {
public static void main(String args[])throws Exception {
ServerSocket ss=new ServerSocket(3333);
Socket s=ss.accept();
DataInputStream din=new DataInputStream(s.getInputStream());
DataOutputStream dout=new DataOutputStream(s.getOutputStream());
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
String str="",str2="";
while(!str.equals("stop")) {
str=din.readUTF();
System.out.println("Client says: "+str);
str2=br.readLine();
Maharashtra state Board of Technical Education 91
Advanced Java Programming (22517)
dout.writeUTF(str2);
dout.flush();
}
din.close();
s.close();
ss.close();
}}
Output:-
Client Side:
import java.io.*;
import java.net.*;
class MyClient {
private MyClient() {}
public static void main(String[] args) throws Exception {
int port = 9000;
Socket s;
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
s = new Socket(InetAddress.getLocalHost(), port);
PrintWriter pw =
new PrintWriter(new OutputStreamWriter(s.getOutputStream()));
BufferedReader brl = new BufferedReader(new InputStreamReader(s.getInputStream()));
System.out.print("Enter any number: ");
Maharashtra state Board of Technical Education 93
Advanced Java Programming (22517)
String str = br.readLine();
pw.println(str);
pw.flush();
String msg = brl.readLine();
if (msg.equals("true")) {
System.out.println("It is a prime number");
} else {
System.out.println("It is not a prime number");
}}}
Output:-
1. …………………………………..
2. …………………………………..
3. …………………………………..
Dated signature
Marks Obtained
of Teacher
Process Product
Total(50)
Related(35) Related(15)