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

Server Class: Name:Pandya Rohankumar Roll No:18mca020 Practical No:3

This document contains code for a server class and client class that communicate over a socket connection. The server class creates a server socket listening on port 6666, accepts a connection from a client, reads data from the client, and writes the data back. The client class connects to the server socket on localhost port 6666, reads user input, writes it to the server, reads the response from the server, and prints it out.

Uploaded by

rohan pandya
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)
25 views4 pages

Server Class: Name:Pandya Rohankumar Roll No:18mca020 Practical No:3

This document contains code for a server class and client class that communicate over a socket connection. The server class creates a server socket listening on port 6666, accepts a connection from a client, reads data from the client, and writes the data back. The client class connects to the server socket on localhost port 6666, reads user input, writes it to the server, reads the response from the server, and prints it out.

Uploaded by

rohan pandya
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/ 4

Name:Pandya Rohankumar

Roll no:18mca020

Practical no:3

Server Class

import java.io.DataInputStream;

import java.io.IOException;

import java.net.ServerSocket;

import java.net.Socket;

import java.io.*;

import java.net.*;

public class Server {

public static void main(String[] args){

try{

System.out.println("Waititng for Connection");

ServerSocket ss=new ServerSocket(6666);

Socket s=ss.accept();//establishes connection

System.out.println("Connection Established");
//DataInputStream dis=new DataInputStream(s.getInputStream());

//String str=(String)dis.readUTF();

BufferedReader br = new BufferedReader(new


InputStreamReader(s.getInputStream()));

String str = br.readLine();

PrintWriter pw = new PrintWriter(s.getOutputStream(),true);

pw.println(str);

//System.out.println("message: "+str);

ss.close();

}catch(Exception e){System.out.println(e);}

}
}

Client Class

import java.io.*;

import java.net.*;

public class Client

public static void main(String[] args)

try{
Socket ss=new Socket("localhost",6666);

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

System.out.println("Enter String");

String s,msg;

s = br.readLine();

PrintWriter pw = new PrintWriter(ss.getOutputStream(),true);

pw.println(s);

BufferedReader br1 = new BufferedReader(new


InputStreamReader(ss.getInputStream()));

msg = br1.readLine();

System.out.println("Server Says :"+msg);

catch(IOException e)

System.out.println(e);

}
Output of Server Class

Output of Client Class

You might also like