0% found this document useful (0 votes)
16 views4 pages

Ajp 14-16

The document contains Java code for two practical exercises involving client-server communication. The first exercise demonstrates a TCP client and server that handle user login, while the second exercise showcases a UDP client and server that determine if a number is even or odd. Each section includes client and server code along with expected outputs.

Uploaded by

Nawaz Wariya
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)
16 views4 pages

Ajp 14-16

The document contains Java code for two practical exercises involving client-server communication. The first exercise demonstrates a TCP client and server that handle user login, while the second exercise showcases a UDP client and server that determine if a number is even or odd. Each section includes client and server code along with expected outputs.

Uploaded by

Nawaz Wariya
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/ 4

Advance Java (AJP) Code Practical 16

Practical No.16

Code:

Client Code:
import java.net.*;

import java.io.*;

public class client {

public static void main(String args[])

throws Exception {

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

BufferedReader kbr=new BufferedReader(new InputStreamReader(System.in));

InputStream obj=s.getInputStream();

BufferedReader br=new BufferedReader(new InputStreamReader(obj));

OutputStream os=s.getOutputStream();

PrintStream ps=new PrintStream(os);

System.out.println("Enter User Id:"); String str=kbr.readLine(); ps.println(str)

System.out.println("Enter Password:");

String str3=kbr.readLine(); ps.println(str3);

String newStr=br.readLine();

System.out.println("Response from server : "+newStr);

br.close();

s.close();

}}

1 | Page
Advance Java (AJP) Code Practical 16

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

Output:

2 | Page
Advance Java (AJP) Code Practical 16

Code:
Client Code:
import java.io.*;
import java.net.*;
public class udpclient
{
public static void main(String [] args) throws Exception
{
int port = 9000; BufferedReader br = new
BufferedReader(new InputStreamReader(System.in));
Socket s = new Socket(InetAddress.getLocalHost(), port);
PrintWriter pw = new PrintWriter(new OutputStreamWriter(s.getOutputStre
am()));
BufferedReader brl = new BufferedReader(new
InputStreamReader(s.getInputStream ()));
System.out.print("Enter any number: ");
String str = br.readLine();
pw.println(str); pw.flush(); String msg =
brl.readLine();
if(msg.equals("Even"))
{
System.out.println("It is an even number");
}
else
{
System.out.println("It is an odd number");
}
}
}

Output:

3 | Page
Advance Java (AJP) Code Practical 16

Server Code:
import java.io.*;
import java.net.*;
public class udpserve {
public static String isEvenOrOdd(int number) {
if (number % 2 == 0) { return "Even";
} else {
return "Odd";
}
}
public static void main(String[] args) throws Exception {
Socket s;
int port = 9000; ServerSocket ss = new
ServerSocket(port);

System.out.println("Waiting for client");


s = ss.accept(); BufferedReader br = new
BufferedReader(new InputStreamReader(s.getInputStream ()));
PrintWriter pw = new PrintWriter(new OutputStreamWriter(s.getOutputStre
am()));

int num = Integer.parseInt(br.readLine());


System.out.println("Number sent by client: " + num);

String result = udpserver.isEvenOrOdd(num);


pw.println(result); pw.flush();
}
}
Output:

4 | Page

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy