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

ClientServer Test

The document describes a Java client-server program that allows two-way communication between a server and client. The server code creates threads to listen for incoming connections and to send/receive data. The client code also creates threads, one to send data to the server and one to receive data from the server. The threads use PrintWriters and BufferedReaders to send and receive strings over the socket connection between the client and server.

Uploaded by

rishabh125
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
103 views

ClientServer Test

The document describes a Java client-server program that allows two-way communication between a server and client. The server code creates threads to listen for incoming connections and to send/receive data. The client code also creates threads, one to send data to the server and one to receive data from the server. The threads use PrintWriters and BufferedReaders to send and receive strings over the socket connection between the client and server.

Uploaded by

rishabh125
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

SERVER--import java.io.

*;
import java.net.*;
import java.lang.*;
public class Server {
public static void main(String[] args) throws IOException {
final int port = 444;
System.out.println("Server waiting for connection on port "+port
);
ServerSocket ss = new ServerSocket(port);
Socket clientSocket = ss.accept();
System.out.println("Recieved connection from "+clientSocket.getI
netAddress()+" on port "+clientSocket.getPort());
//create two threads to send and recieve from client
RecieveFromClientThread recieve = new RecieveFromClientThread(cl
ientSocket);
Thread thread = new Thread(recieve);
thread.start();
SendToClientThread send = new SendToClientThread(clientSocket);
Thread thread2 = new Thread(send);
thread2.start();
}}
class RecieveFromClientThread implements Runnable
{
Socket clientSocket=null;
BufferedReader brBufferedReader = null;
public RecieveFromClientThread(Socket clientSocket)
{
this.clientSocket = clientSocket;
}//end constructor
public void run() {
try{
brBufferedReader = new BufferedReader(new InputStreamReader(this
.clientSocket.getInputStream()));
String messageString;
while(true){
while((messageString = brBufferedReader.readLine())!= null){//as
sign message from client to messageString
if(messageString.equals("EXIT"))
{
break;//break to close socket if EXIT
}
System.out.println("From Client: " + messageString);//pr
int the message from client
System.out.println("Please enter something to send back
to client..");
}
this.clientSocket.close();
System.exit(0);
}
}
catch(Exception ex){System.out.println(ex.getMessage());}
}
}//end class RecieveFromClientThread
class SendToClientThread implements Runnable
{
PrintWriter pwPrintWriter;

Socket clientSock = null;


public SendToClientThread(Socket clientSock)
{
this.clientSock = clientSock;
}
public void run() {
try{
pwPrintWriter =new PrintWriter(new OutputStreamWriter(this.clien
tSock.getOutputStream()));//get outputstream
while(true)
{
String msgToClientString = null;
BufferedReader input = new BufferedReader(new InputStrea
mReader(System.in));//get userinput
msgToClientString = input.readLine();//get message to se
nd to client
pwPrintWriter.println(msgToClientString);//send message
to client with PrintWriter
pwPrintWriter.flush();//flush the PrintWriter
System.out.println("Please enter something to send back
to client..");
}//end while
}
catch(Exception ex){System.out.println(ex.getMessage());}
}//end run
}//end class SendToClientThread
CLIENT --import java.io.*;
import java.net.*;
public class Client {
public static void main(String[] args)
{
try {
Socket sock = new Socket("localhost",444);
SendThread sendThread = new SendThread(sock);
Thread thread = new Thread(sendThread);thread.start();
RecieveThread recieveThread = new RecieveThread(sock);
Thread thread2 =new Thread(recieveThread);thread2.start(
);
} catch (Exception e) {System.out.println(e.getMessage());}
}
}
class RecieveThread implements Runnable
{
Socket sock=null;
BufferedReader recieve=null;
public RecieveThread(Socket sock) {
this.sock = sock;
}//end constructor
public void run() {
try{
recieve = new BufferedReader(new InputStreamReader(this.sock.get
InputStream()));//get inputstream
String msgRecieved = null;
while((msgRecieved = recieve.readLine())!= null)

{
System.out.println("From Server: " + msgRecieved);
System.out.println("Please enter something to send to se
rver..");
}
}catch(Exception e){System.out.println(e.getMessage());}
}//end run
}//end class recievethread
class SendThread implements Runnable
{
Socket sock=null;
PrintWriter print=null;
BufferedReader brinput=null;
public SendThread(Socket sock)
{
this.sock = sock;
}//end constructor
public void run(){
try{
if(sock.isConnected())
{
System.out.println("Client connected to "+sock.getInetAd
dress() + " on port "+sock.getPort());
this.print = new PrintWriter(sock.getOutputStream(), tru
e);
while(true){
System.out.println("Type your message to send to server.
.type 'EXIT' to exit");
brinput = new BufferedReader(new InputStreamReader(Syste
m.in));
String msgtoServerString=null;
msgtoServerString = brinput.readLine();
this.print.println(msgtoServerString);
this.print.flush();
if(msgtoServerString.equals("EXIT"))
break;
}//end while
sock.close();}}catch(Exception e){System.out.println(e.getMessag
e());}
}//end run method
}//end class

You might also like