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

Additional Code

Uploaded by

Priti Mane
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)
6 views

Additional Code

Uploaded by

Priti Mane
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/ 7

4.

Networking Basics Additional Program


Name: Mane Priti Shrinivas Roll No:24/35-30
1)
//client side code
import java.net.*;
import java.io.*;

class clientdemo {
public static void main(String[] args)
{
try (Socket socket = new Socket("localhost", 2020);
DataOutputStream dos = new DataOutputStream(socket.getOutputStream());
DataInputStream dis = new DataInputStream(socket.getInputStream())) {
System.out.println("Client application is sending username");
dos.writeUTF("priti mane");
// Optional: Read response from server
String response = dis.readUTF();
System.out.println("Server response: " + response);
} catch (IOException e)
{
System.err.println("IOException occurred: " + e.getMessage());
}
}
}
// server side code
import java.net.*;
import java.io.*;
class Serverdemo
{
public static void main(String args[]) throws Exception
{
int f=1,i,n;
ServerSocket ss=new ServerSocket(2020);
Socket s=ss.accept();
DataOutputStream dos= new DataOutputStream(s.getOutputStream());
DataInputStream dis= new DataInputStream(s.getInputStream());
String str=(String)dis.readUTF();
System.out.println("Server says,Hello"+str);
ss.close ();
s.close();
}
}
Output:
2)
//client side code
import java.net.*;
import java.io.*;
class clientdemo1
{
public static void main(String[] args) throws Exception
{
int n1,n2,n3;
DataInputStream dis = new DataInputStream(System.in);
System.out.println("Enter 3 number ");
n1=Integer.parseInt(dis.readLine());
n2=Integer.parseInt(dis.readLine());
n3=Integer.parseInt(dis.readLine());
Socket s = new Socket("localhost", 2020);
DataInputStream dis2 = new DataInputStream(s.getInputStream());
DataOutputStream dos2 = new DataOutputStream(s.getOutputStream());
dos2.writeUTF(n1+","+n2+","+n3);
String ans=dis2.readUTF();
System.out.println("Largest element is:"+ans);
s.close();
}
}
// server side code
import java.net.*;
import java.io.*;
class Serverdemo_1
{
public static void main(String[] args) throws Exception
{
ServerSocket ss = new ServerSocket(2020);
Socket s=ss.accept();
System.out.println("Enter 3 number ");
DataInputStream dis2 = new DataInputStream(s.getInputStream());
DataOutputStream dos2 = new DataOutputStream(s.getOutputStream());
String str=dis2.readUTF();
String nums[]=str.split(",");
int lar=0;
int a=Integer.parseInt(nums[0]);
int b=Integer.parseInt(nums[1]);
int c=Integer.parseInt(nums[2]);

if(a>b && a>c)


{
lar=a;
}
if(b>a && b>c)
{
lar=b;
}
if(c>a && c>b)
{
lar=c;
}
dos2.writeUTF(""+lar);
ss.close();
s.close();
}
}
Output:
3)
//client side code
import java.net.*;
import java.io.*;
class clientdemo_2
{
public static void main(String[] args) throws Exception
{
Socket s = new Socket("localhost", 2020);
DataInputStream dis = new DataInputStream(s.getInputStream());
DataOutputStream dos = new DataOutputStream(s.getOutputStream());
System.out.println("client application is sending request value");
dos.writeUTF("5");
String ans=(String)dis.readUTF();
System.out.println("client program received result from server");
System.out.println("square of 5 is:"+ans);
s.close();
}
}
// Server side code
import java.net.*;
import java.io.*;
class Serverdemo_3
{
public static void main(String[] args) throws Exception
{
ServerSocket ss = new ServerSocket(2020);
Socket s=ss.accept();
DataInputStream dis = new DataInputStream(s.getInputStream());
DataOutputStream dos = new DataOutputStream(s.getOutputStream());
System.out.println("Server is waiting for request input from client");
String str =(String)dis.readUTF();
System.out.println("Server received request from client");
int n =Integer.parseInt(str);
int sq =n*n;
dos.writeUTF(""+sq);
System.out.println("Server sent the response");
ss.close();
s.close();
}
}
Output:

You might also like