server-client JAVA Chat Application menggunakan Multithreading

//untuk server
*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package tcp;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Scanner;
/**
 *
 * @author roziku
 */
public class Tcpserver {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args)  {
     
        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("client :" + in.nextLine());
                            }
   }
  }
  catch (Exception e)
  {
   e.printStackTrace();
  }
 }

}

//untuk client
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package tcp;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.Scanner;
/**
 *
 * @author roziku
 */
public class Tcpclient {
private final static int PORT = 6677;
 private final static String HOST = "localhost";
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) throws  IOException {
        // TODO code application logic here
        // args give message contents and server hostname
 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("server :" +in.nextLine());
                            }
   }
  }
  catch (Exception e)
  {
   e.printStackTrace();
   
  }
 }

}

Komentar

Postingan Populer