SERVER--import [Link].
*;
import [Link].*;
import [Link].*;
public class Server {
public static void main(String[] args) throws IOException {
final int port = 444;
[Link]("Server waiting for connection on port "+port
);
ServerSocket ss = new ServerSocket(port);
Socket clientSocket = [Link]();
[Link]("Recieved connection from "+[Link]
netAddress()+" on port "+[Link]());
//create two threads to send and recieve from client
RecieveFromClientThread recieve = new RecieveFromClientThread(cl
ientSocket);
Thread thread = new Thread(recieve);
[Link]();
SendToClientThread send = new SendToClientThread(clientSocket);
Thread thread2 = new Thread(send);
[Link]();
}}
class RecieveFromClientThread implements Runnable
{
Socket clientSocket=null;
BufferedReader brBufferedReader = null;
public RecieveFromClientThread(Socket clientSocket)
{
[Link] = clientSocket;
}//end constructor
public void run() {
try{
brBufferedReader = new BufferedReader(new InputStreamReader(this
.[Link]()));
String messageString;
while(true){
while((messageString = [Link]())!= null){//as
sign message from client to messageString
if([Link]("EXIT"))
{
break;//break to close socket if EXIT
}
[Link]("From Client: " + messageString);//pr
int the message from client
[Link]("Please enter something to send back
to client..");
}
[Link]();
[Link](0);
}
}
catch(Exception ex){[Link]([Link]());}
}
}//end class RecieveFromClientThread
class SendToClientThread implements Runnable
{
PrintWriter pwPrintWriter;
Socket clientSock = null;
public SendToClientThread(Socket clientSock)
{
[Link] = clientSock;
}
public void run() {
try{
pwPrintWriter =new PrintWriter(new OutputStreamWriter([Link]
[Link]()));//get outputstream
while(true)
{
String msgToClientString = null;
BufferedReader input = new BufferedReader(new InputStrea
mReader([Link]));//get userinput
msgToClientString = [Link]();//get message to se
nd to client
[Link](msgToClientString);//send message
to client with PrintWriter
[Link]();//flush the PrintWriter
[Link]("Please enter something to send back
to client..");
}//end while
}
catch(Exception ex){[Link]([Link]());}
}//end run
}//end class SendToClientThread
CLIENT --import [Link].*;
import [Link].*;
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);[Link]();
RecieveThread recieveThread = new RecieveThread(sock);
Thread thread2 =new Thread(recieveThread);[Link](
);
} catch (Exception e) {[Link]([Link]());}
}
}
class RecieveThread implements Runnable
{
Socket sock=null;
BufferedReader recieve=null;
public RecieveThread(Socket sock) {
[Link] = sock;
}//end constructor
public void run() {
try{
recieve = new BufferedReader(new InputStreamReader([Link]
InputStream()));//get inputstream
String msgRecieved = null;
while((msgRecieved = [Link]())!= null)
{
[Link]("From Server: " + msgRecieved);
[Link]("Please enter something to send to se
rver..");
}
}catch(Exception e){[Link]([Link]());}
}//end run
}//end class recievethread
class SendThread implements Runnable
{
Socket sock=null;
PrintWriter print=null;
BufferedReader brinput=null;
public SendThread(Socket sock)
{
[Link] = sock;
}//end constructor
public void run(){
try{
if([Link]())
{
[Link]("Client connected to "+[Link]
dress() + " on port "+[Link]());
[Link] = new PrintWriter([Link](), tru
e);
while(true){
[Link]("Type your message to send to server.
.type 'EXIT' to exit");
brinput = new BufferedReader(new InputStreamReader(Syste
[Link]));
String msgtoServerString=null;
msgtoServerString = [Link]();
[Link](msgtoServerString);
[Link]();
if([Link]("EXIT"))
break;
}//end while
[Link]();}}catch(Exception e){[Link]([Link]
e());}
}//end run method
}//end class