You are on page 1of 3

Client Server

============================================

Client

import java.net.*;
import java.io.*;
import javax.swing.*;

class Client{
public static void main(String args[]){
Socket soc;
ObjectInputStream oin=null;
ObjectOutputStream oos=null;

try{
soc=new Socket("localhost",2000);
oos=new
ObjectOutputStream(soc.getOutputStream());
oin=new ObjectInputStream(soc.getInputStream());

while(true){
String s=JOptionPane.showInputDialog("Enter
msg: ");
if(s=="quit")
break;
else{
oos.writeObject(new String(s));

String str=(String)oin.readObject();
System.out.println(str);
}
}
oin.close();
oos.close();
soc.close();
}catch(Exception e){e.printStackTrace();}
}
}
Server

import java.net.*;
import java.io.*;
import javax.swing.*;

class Server{
public static void main(String args[]){
ServerSocket sersoc=null;
Socket client_soc;
ObjectInputStream ois=null;
ObjectOutputStream oos=null;

try{
sersoc=new ServerSocket(2000);
}catch(Exception e){e.printStackTrace();}

try{
System.out.println("Server is Ready for Chat!!!");
client_soc=sersoc.accept();
ois=new
ObjectInputStream(client_soc.getInputStream());
oos=new
ObjectOutputStream(client_soc.getOutputStream());

while(true){
String s=(String)ois.readObject();
if(s=="quit"){
System.out.println(s);
break;
}
else{
System.out.println(s);
String
str=JOptionPane.showInputDialog("Enter msg: ");
oos.writeObject(new String(str));
}
}

oos.close();
ois.close();
sersoc.close();
client_soc.close();
}catch(Exception e){e.printStackTrace();}
}
}

http://www.ravianeducation.blogspot.com
FARHAN: 03008855006

You might also like