0% found this document useful (0 votes)
22 views

Practical 16 P

The document describes a program to create a chat application using sockets and server sockets in Java. It includes code for both the server and client. The server code creates a server socket that listens for client connections, then allows sending and receiving of messages. The client code connects to the server socket and also implements sending and receiving of messages. A second program is described to create a prime number checker service, with the client sending a number to the server and the server determining if it is prime and sending back the response.

Uploaded by

minakshibhavar91
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
0% found this document useful (0 votes)
22 views

Practical 16 P

The document describes a program to create a chat application using sockets and server sockets in Java. It includes code for both the server and client. The server code creates a server socket that listens for client connections, then allows sending and receiving of messages. The client code connects to the server socket and also implements sending and receiving of messages. A second program is described to create a prime number checker service, with the client sending a number to the server and the server determining if it is prime and sending back the response.

Uploaded by

minakshibhavar91
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

Practical 16

Roll no : 13

Write a Program using Socket and ServerSocket to create Chat Application.

//Server

import java.net.*;
import java.io.*;

class serverprg
{
public static void main(String args[])throws

UnknownHostException,IOException
{
ServerSocket serverobj = new ServerSocket(1000);
System.out.println("Service of Server has been Started");
Socket clientobj=serverobj.accept();
System.out.println("Client is Connected");

InputStream inputobj = clientobj.getInputStream();


DataInputStream disobj = new DataInputStream(inputobj);

OutputStream osobj = clientobj.getOutputStream();


DataOutputStream dosobj = new DataOutputStream(osobj);

BufferedReader br = new BufferedReader(new


InputStreamReader(System.in));
String msg_data ="";

while(!msg_data.equals("Stop"))
{
System.out.println("\n Enter data to send : ");
msg_data = br.readLine();
dosobj.writeUTF(msg_data);

msg_data =disobj.readUTF();
System.out.println("Client says :"+msg_data);
}
clientobj.close();
}
}

//Client

import java.net.*;
import java.io.*;

class Clientprg
{
public static void main(String args[])throws

UnknownHostException,IOException
{
Socket clientobj = new Socket("localhost",1000);
BufferedReader br = new BufferedReader(new
InputStreamReader(System.in));
OutputStream osobj = clientobj.getOutputStream();
DataOutputStream dosobj = new DataOutputStream(osobj);
InputStream isobj = clientobj.getInputStream();
DataInputStream disobj = new DataInputStream(isobj);

String smsg_data ="";

while(!smsg_data.equals("Stop"))
{
smsg_data = disobj.readUTF();
System.out.println("Sever says :"+smsg_data);
System.out.println("\n Enter data to send : ");
String msg_data = br.readLine();
dosobj.writeUTF(msg_data);
}
clientobj.close();
}
}
Output:-

Server

Client
2. Write a Program to develop prime number Server (Client will send any
Number to server, Server will send the response the number is rime or not)

//Client

import java.net.Socket;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintStream;
public class PrimeUsingAakash
{
public static void main(String args[]) throws Exception
{
Socket sk=new Socket("localhost",2000);
BufferedReader sin=new BufferedReader(new
InputStreamReader(sk.getInputStream()));
PrintStream sout=new PrintStream(sk.getOutputStream());
BufferedReader stdin=new BufferedReader(new
InputStreamReader(System.in));
String s;
do
{
System.out.print("Request Number : ");
s=stdin.readLine();
sout.println(s);
s=sin.readLine();
System.out.print("Response : "+s+"\n");
System.out.println("Do you want to request another number: ");
s=stdin.readLine();
}while(s.equalsIgnoreCase("Y"));
sk.close();
sin.close();
sout.close();
stdin.close();
}
}
//Server

import java.net.ServerSocket;
import java.net.Socket;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintStream;
public class PrimeUsingServer {
public static void main(String args[]) throws Exception {
ServerSocket ss = new ServerSocket(2000);
Socket sk = ss.accept();
BufferedReader cin = new BufferedReader(new
InputStreamReader(sk.getInputStream()));
PrintStream cout = new PrintStream(sk.getOutputStream());
BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
String s;
while (true) {
s = cin.readLine();
if (s.equalsIgnoreCase("N"))
break;
System.out.println("Client Number: " + s);
int num = Integer.parseInt(s);//cin.readLine());
boolean p = true;
if (num == 2 || num == 3 || num == 1)
p = true;
else
for (int i = 2; i < num; i++) {
if (num % i == 0) {
p = false;
break;
}
}
if (p) {
cout.println("Number is Prime\n");
}else
{
cout.println("Number is Not Prime\n");
}
ss.close();
sk.close();
cin.close();
cout.close();
stdin.close();
}
}
}

Output:-

You might also like