Java 8 Alok

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

APEX Institute of Technology

Experiment Number: 8
Student Name: ALOK KUMAR UID: 21BCS6438

Branch: AIML CSE Section & Group: 21AML8-A

Semester: 03 Date: 11-11-22

Course Name: Programming in Java Course Code: 21CSH-244

1. Aim/Overview of the practical: Write a program to create Client/Server socket to


establish communication in bi-directional.

2. Task to be done: a) To create streams at both client and server side.

b) To send data from the client and read and write.

c) To establish two way communication between client and server.

3. Algorithm (For Programming):

1) Start.

2) Create server program

3) In the main method,create server socket

4)Connect server socket to client socket.

5)Send data to the client and read it.

6)Read data from the client and send it to him.

7)Close the connection and terminate the application.

8)Similarily create client program.

9)End 4.

Code

import java.io.*; import

java.net.*; class Server2

{ public static void


APEX Institute of Technology

main(String args[])

throws Exception

ServerSocket ss = new ServerSocket(888);

Socket s = ss.accept();

System.out.println("Connection established");

PrintStream ps= new PrintStream(s.getOutputStream());

BufferedReader br= new BufferedReader( new

InputStreamReader(s.getInputStream()));

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

while (true) { String str, str1; while

((str = br.readLine()) != null) {

System.out.println(str); str1 =

kb.readLine(); ps.println(str1);

ps.close(); br.close();

kb.close(); ss.close();

s.close();

System.exit(0);

}}

import java.io.*; import

java.net.*;

class Client2 { public static void

main(String args[]) throws Exception

{
APEX Institute of Technology

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

DataOutputStream dos= new DataOutputStream(s.getOutputStream());

BufferedReader br= new BufferedReader(new InputStreamReader(s.getInputStream()));

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

String str, str1;

System.out.println(str1);

dos.close(); br.close();

kb.close();

s.close();

}}

5. OUTPUT

6. Learning Outcomes :-
APEX Institute of Technology

1) Learnt about client


2) Learnt about synchronizing multithreads

3) 3) Learnt about exception handling.

You might also like