0% found this document useful (0 votes)
30 views8 pages

Practical No. 16

The document contains code snippets for three different Java socket programming exercises: user authentication, prime number checking, and a chat application. Each section includes client and server code demonstrating how to establish connections, send and receive messages, and handle user input. The examples illustrate basic socket operations and the use of input/output streams for communication between clients and servers.

Uploaded by

khanzainab6002
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
30 views8 pages

Practical No. 16

The document contains code snippets for three different Java socket programming exercises: user authentication, prime number checking, and a chat application. Each section includes client and server code demonstrating how to establish connections, send and receive messages, and handle user input. The examples illustrate basic socket operations and the use of input/output streams for communication between clients and servers.

Uploaded by

khanzainab6002
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

PROGRAM CODE:

Q. Write a program to check credentials of users (Client will send


user id and password to server and server will authenticate the
client using equals()).
Code:

Client Code: System.out.println("Response


import java.net.*; from server : "+newStr);
import java.io.*; br.close();
public class client s.close();
{ }}
public static void main(String
args[]) throws Exception Server Code:
{ import java.net.*;
Socket s=new import java.io.*;
Socket("localhost",777); public class server
BufferedReader kbr=new {
BufferedReader(new public static void main(String
InputStreamReader(System.in)); args[]) throws Exception
{
InputStream ServerSocket ss=new
obj=s.getInputStream(); ServerSocket(777);
Socket s=ss.accept();
BufferedReader br=new System.out.println("Connection
BufferedReader(new Established");
InputStreamReader(obj)); OutputStream
obj=s.getOutputStream();
OutputStream PrintStream ps=new
os=s.getOutputStream(); PrintStream(obj);
PrintStream ps=new InputStream
PrintStream(os); obj1=s.getInputStream();
BufferedReader br=new
System.out.println("Enter User BufferedReader(new
Id:"); InputStreamReader(obj1));
String str=kbr.readLine(); String str1=br.readLine();
ps.println(str); String str2=br.readLine();
String newstr="MHSSP";
System.out.println("Enter if(str1.equals(newstr) &&
Password:"); str2.equals(newstr))
String str3=kbr.readLine(); {
ps.println(str3); ps.println("Successful Login");
String newStr=br.readLine(); }
else
{ ss.close();
ps.println("Invalid Credentials"); s.close();
} }
ps.close();

Output:
EXRECISE:

Q1. Write a program to develop prime number server (Client will


send any number to server, Server will send the response the
number is prime or not.

Code:

Client Code: {
import java.io.*; System.out.println("It is a
import java.net.*; prime number");
public class udpclient }
{ else
public static void main(String [] {
args) throws Exception System.out.println("It is
{ not a prime number");
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.getOutputS
tream()));
BufferedReader brl = new
BufferedReader(new
InputStreamReader(s.getInputStr
eam()));
System.out.print("Enter any
number: ");
String str = br.readLine();
pw.println(str);
pw.flush();
String msg = brl.readLine();
if(msg.equals("true"))
}
--i;
}
return isPrimeNum;
}
public static void main(String []
args) throws Exception
{
Socket s;
int port = 9000;
Server Code: ServerSocket ss = new
import java.io.*; ServerSocket(port);
import java.net.*; System.out.println("Waiting
public class udpserver for client");
{ s = ss.accept();
public static boolean BufferedReader br = new
isPrime(int number){ BufferedReader(new
boolean isPrimeNum = false; InputStreamReader(s.getInputStr
int i = (int) eam()));
Math.ceil(Math.sqrt(number)); PrintWriter pw = new
while(i>1) PrintWriter(new
{ OutputStreamWriter(s.getOutputS
if((number != i) && tream()));
(number % i ==0)) int num =
{ Integer.parseInt(br.readLine());
isPrimeNum = false; System.out.println("Number
break; sent by client: " + num);
}
else if(!isPrimeNum) pw.println(udpserver.isPrime(num
{ ));
isPrimeNum = true; pw.flush(); }}

Output:
Q2. Write a program using socket and ServerSocket to create
Chat application.

Code:

Client Code: DataOutputStream dos =


import java.io.*; new
import java.net.*; DataOutputStream(s.getOutputStr
import java.util.*; eam());

public class Client


{ Thread sendMessage = new
final static int ServerPort = Thread(new Runnable()
1234; {

public static void main(String public void run() {


args[]) throws while (true) {
UnknownHostException,
IOException
{ String msg =
Scanner scn = new scn.nextLine();
Scanner(System.in);
try {

InetAddress ip =
InetAddress.getByName("localhos dos.writeUTF(msg);
t"); } catch (IOException
e) {

Socket s = new Socket(ip, e.printStackTrace();


ServerPort); }
}
}
DataInputStream dis = new });
DataInputStream(s.getInputStrea
m());
Thread readMessage = new ServerSocket ss = new
Thread(new Runnable() ServerSocket(1234);
{ Socket s;
while (true)
public void run() { {
s = ss.accept();
while (true) { System.out.println("New
try { client request received : " + s);
DataInputStream dis =
String msg = new
dis.readUTF(); DataInputStream(s.getInputStrea
m());
System.out.println(msg); DataOutputStream dos =
} catch (IOException new
e) { DataOutputStream(s.getOutputStr
eam());

e.printStackTrace(); System.out.println("Creating a
} new handler for this client...");
} ClientHandler mtch = new
} ClientHandler(s,"client " + i, dis,
}); dos);
Thread t = new
sendMessage.start(); Thread(mtch);
readMessage.start();
System.out.println("Adding this
} client to active client list");
} ar.add(mtch);
t.start();
Server Code: i++;
import java.io.*; }
import java.util.*; }
import java.net.*; }
public class Server class ClientHandler implements
{ Runnable
static Vector<ClientHandler> ar {
= new Vector<>(); Scanner scn = new
Scanner(System.in);
private String name;
static int i = 0; final DataInputStream dis;
public static void main(String[] final DataOutputStream dos;
args) throws IOException Socket s;
{ boolean isloggedin;
public ClientHandler(Socket s, String MsgToSend =
String name, st.nextToken();
String recipient =
DataInputStream dis, st.nextToken();
DataOutputStream dos) { for (ClientHandler mc :
this.dis = dis; Server.ar)
this.dos = dos; {
this.name = name; if
this.s = s; (mc.name.equals(recipient) &&
this.isloggedin=true; mc.isloggedin==true)
} {
public void run() {
String received; mc.dos.writeUTF(this.name+" :
while (true) "+MsgToSend);
{ break;
try }
{ }
received = } catch (IOException e) {
dis.readUTF();
e.printStackTrace();
System.out.println(received); }
}
if(received.equals("logout")){ try
this.isloggedin=false; {
this.s.close(); this.dis.close();
break; this.dos.close();
} }catch(IOException e){
StringTokenizer st = e.printStackTrace();
new StringTokenizer(received, }
"#"); }
}

Output:

You might also like