0% found this document useful (0 votes)
2 views5 pages

PR AJPA16

The document contains practical programming assignments for DKTE’s Yashwantrao Chavan Polytechnic students, specifically focusing on user authentication and a chat application using Java sockets. It includes code examples for both server and client implementations for validating user credentials and enabling chat functionality. The assignments demonstrate the use of ServerSocket and Socket classes in Java for network communication.

Uploaded by

gxyz8837
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)
2 views5 pages

PR AJPA16

The document contains practical programming assignments for DKTE’s Yashwantrao Chavan Polytechnic students, specifically focusing on user authentication and a chat application using Java sockets. It includes code examples for both server and client implementations for validating user credentials and enabling chat functionality. The assignments demonstrate the use of ServerSocket and Socket classes in Java for network communication.

Uploaded by

gxyz8837
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/ 5

DKTE’s Yashwantrao Chavan Polytechnic , Ichalkaranji

PRACTICAL NO : 16

ENROLLMENT NO : 2215770092
NAME: Vedraj Sanjay jagdale

1. 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 for Server: import
java.net.ServerSocket; import
java.net.Socket; import
java.io.BufferedReader; import
java.io.IOException; import
java.io.OutputStream; import
java.io.PrintStream; import
java.io.InputStreamReader;
public class Servervalid
{ 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("arati") &&
pass.equals("4321")) {
System.out.println("Validated Successfully");
} else
{
System.out.println("In-Validated");}}}
Output:

Vedraj Sanjay jagdale


DKTE’s Yashwantrao Chavan Polytechnic , Ichalkaranji

Code for Client:


import java.net.Socket; import
java.io.BufferedReader; import
java.io.IOException; import
java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintStream; public
class ClientValid
{ 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("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:

Final Output:

Vedraj Sanjay jagdale


DKTE’s Yashwantrao Chavan Polytechnic , Ichalkaranji

2. Write a program using Socket and ServerSocket to create Chat Application.


Code for Server:
import java.net.*;
import java.io.*;
class SChat

public static void main(String a[]) throws Exception

ServerSocket ss = new ServerSocket(2910);

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();

dout.writeUTF(str2); dout.flush();}

din.close();

Vedraj Sanjay jagdale


DKTE’s Yashwantrao Chavan Polytechnic , Ichalkaranji

s.close();
ss.close();}}

Output:

Code for Client:


import

java.net.*;

import java.io.*;

class CChat

public static void main(String args[]) throws Exception

Socket s = new Socket("localhost",2910);

DataInputStream din = new DataInputStream (s.getInputStream());

DataOutputStream dout = new DataOutputStream


(s.getOutputStream ());

Vedraj Sanjay jagdale


DKTE’s Yashwantrao Chavan Polytechnic , Ichalkaranji

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();}}
Output:

Vedraj Sanjay jagdale

You might also like