ONE TO ONE CHAT APP
//Code for Friend With Server
import java.net.*;
import java.io.*;
import java.util.Scanner;
class clientUnit extends Thread{
PrintWriter out=null;
BufferedReader in=null;
String keyboardinput;
Socket conn=null;
String me="Roshan";
String NAME;
public clientUnit(Socket conn){
try{
this.conn=conn;
this.out = new PrintWriter(conn.getOutputStream(),true);
this.in=new BufferedReader(new InputStreamReader(conn.getInputStream()));
}
catch (IOException ioe){
System.err.println("couldn't communicate with client");
}
}
public void run(){
try{
NAME=in.readLine();
System.out.println("Connected:"+conn.getLocalAddress()+":"+conn.getLocalPort()+" ~ "+NAME);
out.println(me);
while((keyboardinput=in.readLine())!=null){
System.out.println(NAME+":"+keyboardinput);
}
}catch(IOException ex){
try{
out.close();
in.close();
conn.close();
}
catch(IOException ioe){System.out.println(ioe);}
System.err.println("couldn't communicate with Your Friend");
System.exit(-1);
}
System.out.println("Your Friend Left");
System.exit(-1);
}
public void sendMsg(){
Scanner scan= new Scanner(System.in);
try{
while(true){
keyboardinput=scan.nextLine();
if(keyboardinput==null || keyboardinput=="" || keyboardinput.length()==0){
break;
}
out.println(keyboardinput);
}
}
catch(Exception e){
System.out.println(e);
}
finally{
scan.close();
System.out.println("You Left");
System.exit(-1);
}
}
}
public class ChatApp {
public static void main(String[] args) {
ServerSocket serverSocket = null;
try {
serverSocket =new ServerSocket(4660);
System.out.println(serverSocket);
System.out.println("Wating For Connection:");
Socket clientSocket=null;
clientSocket=serverSocket.accept();
clientUnit client_Unit= new clientUnit(clientSocket);
client_Unit.start();
client_Unit.sendMsg();
}catch (IOException ioe){
System.out.println("Accept to accept connection");
System.exit(-1);
}
finally
{
try {
serverSocket.close();
} catch (IOException ex) { }
}
}
}
//Code For Friend With Server Ends Here
---------------------------------------------------------------->
//code For Normal Friend Without server
import java.io.*;
import java.net.*;
class getmsg extends Thread{
BufferedReader in;
String name;
public getmsg(BufferedReader addr,String name){
this.in=addr;
this.name=name;
}
public void run(){
while(true){
try{
String msg=in.readLine();
if(msg==null || msg=="" || msg.length()==0){
break;
}
System.out.println(name+":"+msg);
} catch (IOException ioe){}
}
System.out.println("Your Friend Left");
System.exit(-1);
}
}
public class chatApp2{
public static void main(String[] args) throws IOException{
String me="Rinku";
Socket echoSocket = null;
PrintWriter out = null;
BufferedReader in = null;
try {
String hostName = "localhost";
echoSocket = new Socket(hostName, 4660);
out = new PrintWriter(echoSocket.getOutputStream(), true);
in = new BufferedReader(new InputStreamReader(echoSocket.getInputStream()));
}catch (UnknownHostException e)
{
System.err.println("unknown host");
System.exit (1);
}
catch (IOException e){
System.err.println("Couldn't get I/0 for the connection to host "+e);
System.exit(1) ;
}
BufferedReader keyboard=new BufferedReader(new InputStreamReader(System.in));
String userInput;
out.println(me);
System.out.println("Send Message :");
String name=in.readLine();
getmsg gm=new getmsg(in,name);
gm.start();
while(true)
{
userInput=keyboard.readLine();
if(userInput==null || userInput=="" || userInput.length()==0){
System.out.println("You Left");
break;
}
out.println(userInput);
}
System.exit(-1);
out.close();
in.close();
keyboard.close();
echoSocket.close();
}
}
//code for Normal friend without server Ends here
Comments
Post a Comment