100% found this document useful (1 vote)
1K views6 pages

Practical No 16

1. The document describes a program written by Ayush Maroti Wadje for class CO4I to implement a chat server and client using sockets. 2. The program includes code for both the server side and client side. The server side code creates a server socket and accepts connections from clients. It then reads and writes messages to connected clients. 3. The client side code connects to the server socket and reads/writes messages in a loop, implementing a simple chat client. It also includes code for a prime number checking server that takes a number from the client and checks if it is prime or not.

Uploaded by

nstrnsdtn
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
1K views6 pages

Practical No 16

1. The document describes a program written by Ayush Maroti Wadje for class CO4I to implement a chat server and client using sockets. 2. The program includes code for both the server side and client side. The server side code creates a server socket and accepts connections from clients. It then reads and writes messages to connected clients. 3. The client side code connects to the server socket and reads/writes messages in a loop, implementing a simple chat client. It also includes code for a prime number checking server that takes a number from the client and checks if it is prime or not.

Uploaded by

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

Name:-Ayush Maroti Wadje Class:-CO4I

Roll No:-76

Practical No 16 :- Writer a Program to implement chat


Server using Server Socket and Socket Class

Q1. Write a program using socket and server socket to create chat application.

Server Side:-
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 ServerSide

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

// Client Send

BufferedReader br = new BufferedReader(

new InputStreamReader(s1.getInputStream())

);

OutputStream out = s1.getOutputStream();

PrintStream ps = new PrintStream(out);

BufferedReader br1 = new BufferedReader(


new InputStreamReader(System.in)

);

do{

String res = br.readLine()

System.out.println("Client Send: "+res)

System.out.print("Server: ");

String msg = br1.readLine();

System.out.print("\n\n");

ps.println(msg);

while(true);

ClientSide:-
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 ClientSice


{
public static void main(String[] args) throws IOException {
Socket s = new Socket("localhost",2019);

System.out.println("Client Started, waiting for server response..");

BufferedReader br = new BufferedReader(


new InputStreamReader(System.in)
);
OutputStream os = s.getOutputStream();

BufferedReader br1 = new BufferedReader(


new InputStreamReader(s.getInputStream())
);
PrintStream ps = new PrintStream(os);
do{
System.out.print("Client: ");
String msg = br.readLine();
ps.println(msg);

String res = br1.readLine();

System.out.println("Server Send:"+res+"\n\n");
}
while(true);

}
}
Output:-
Q2. Write a program to develop prime number server (client will send any number to a
server, Server will send a response to the number is prime or not.)
Ans:-
Server Side:-
import java.net.*;
import java.io.*;
import java.nio.Buffer;

public class Pra_16 {


public static void main(String[] args) {
try {
Socket s= new Socket("localhost",1000);
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter no:");
int n = Integer.parseInt(br.readLine());
OutputStream o= s.getOutputStream();
PrintStream ps =new PrintStream(o);
ps.println(n);

BufferedReader br1= new BufferedReader(new


InputStreamReader(s.getInputStream()));
int c= br1.read();
while(c != -1){
System.out.print((char)c);
c= br1.read();
}

} catch (Exception e){

}
}

Client :-
import java.net.*;
import java.io.*;
import java.nio.Buffer;

public class Pra16_main {


public static void main(String[] args) {
try {
ServerSocket ss = new ServerSocket(1000);
System.out.println("Searching");
Socket s = ss.accept();
BufferedReader br = new BufferedReader(new
InputStreamReader(s.getInputStream()));
int n = Integer.parseInt(br.readLine());
OutputStream o = s.getOutputStream();
PrintStream ps = new PrintStream(o);
System.out.println("Checking");
int num, i, count = 0;

for (i = 2; i < n; i++) {


if (n % i == 0) {
count++;
break;
}
}

if (count == 0)
ps.println("\nIt is a Prime Number.");
else
ps.println("\nIt is not a Prime Number.");
} catch (Exception e) {

}
}
Output:-

You might also like