Friday, 29 March 2013

JAVA Chat Application with Multithreading

Server Part:


import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Scanner;

/**
 *
 * @author nishant
 */
public class Server {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
     
     
        try
{
final int PORT = 6677;
ServerSocket server = new ServerSocket(PORT);
System.out.println("Waiting for clients...");

while (true)
{
Socket s = server.accept();

System.out.println("Client connected from " + s.getLocalAddress().getHostName());

Client chat1 = new Client(s,1);
                                Client chat2 =new Client(s,2);
Thread t1 = new Thread(chat1);
                                Thread t2 =new Thread(chat2);
t1.start();
                                t2.start();
}
}
catch (Exception e)
{
System.out.println("An error occured.");
e.printStackTrace();
}
    }
}
 class Client implements Runnable{

private Socket socket;
int id;
public Client(Socket s,int pid)

        {
            id=pid;
socket = s;
}

@Override
public void run()
{
try
{
Scanner chat = new Scanner(System.in);
Scanner in = new Scanner(socket.getInputStream());
PrintWriter out = new PrintWriter(socket.getOutputStream());

while (true)
{
                            if(id==1)
                            {
String input = chat.nextLine();
out.println(input);
out.flush();
                            }
                            if(id==2)
                            {
if(in.hasNext())
System.out.println(in.nextLine());
                            }
}
}
catch (Exception e)
{
e.printStackTrace();
}
}

}



Client Part:

import java.io.IOException;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.Scanner;

/**
 *
 * @author nishant
 */
public class Client {
    private final static int PORT = 6677;
private final static String HOST = "localhost";

public static void main(String[] args) throws IOException
{
try
{

Socket s = new Socket(HOST, PORT);

System.out.println("You connected to " + HOST);

Clients client = new Clients(s,1);
Clients clients =new Clients(s,2);
Thread t1 = new Thread(client);
                        Thread t2 =new Thread(clients);
t1.start();
t2.start();
}
catch (Exception noServer)
{
System.out.println("The server might not be up at this time.");
System.out.println("Please try again later.");
}
}
}
 class Clients implements Runnable {

private Socket socket;
int id;
public Clients(Socket s,int pid)
{
socket = s;
                id=pid;
}


public void run()
{
try
{
Scanner chat = new Scanner(System.in);
Scanner in = new Scanner(socket.getInputStream());
PrintWriter out = new PrintWriter(socket.getOutputStream());

while (true)
{
                            if(id==1)
                            {
String input = chat.nextLine();
out.println(input);
out.flush();
                            }
                            if(id==2)
                            {
if(in.hasNext())
System.out.println(in.nextLine());
                            }
}
}
catch (Exception e)
{
e.printStackTrace();
}
}

}

No comments:

Post a Comment