Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package im;
- import java.io.IOException;
- import java.io.ObjectInputStream;
- import java.io.ObjectOutputStream;
- import java.net.InetAddress;
- import java.net.Socket;
- import java.util.Scanner;
- public class ClientDebug {
- static class MsgHandler implements Runnable {
- static public ObjectInputStream in;
- MsgHandler(Socket connection) {
- try {
- System.out.println("Initializing input stream...");
- in = new ObjectInputStream(connection.getInputStream());
- System.out.println("Input stream initialized, now chatting.");
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- public void run() {
- try {
- while (true) {
- Message msg = (Message) in.readObject();
- System.out.println("[" + msg.username + "]: " + msg.text);
- }
- } catch (ClassNotFoundException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- }
- public Socket connection;
- public ObjectOutputStream out;
- public String ip;
- public int port;
- public ClientDebug(String ip, int port) {
- this.ip = ip;
- this.port = port;
- try {
- System.out.println("Looking for server...");
- connection = new Socket(InetAddress.getByName(ip), port);
- System.out.println("Connected to server " + InetAddress.getByName(ip));
- System.out.println("Initializing output stream...");
- out = new ObjectOutputStream(connection.getOutputStream());
- System.out.println("Output stream has been initialized.");
- new Thread(new MsgHandler(connection)).start();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- public void run() {
- System.out.println("Creating thread to handle the sending of the messages");
- // Runnable send = () -> {
- //
- // System.out.println("Inside the send thread");
- // try (Scanner in = new Scanner(System.in)) {
- // while (true) {
- // out.writeObject(new Message(in.nextLine()));
- // System.out.println("Message hs been recognized and sent");
- // }
- // } catch (IOException e) {
- // e.printStackTrace();
- // }
- // };
- //
- // System.out.println("Send thread created, making a thread to read in
- // input");
- // Runnable read = () -> {
- //
- // System.out.println("Inside the read thread");
- // try {
- // while (true) {
- // System.out.println((Message) in.readObject());
- // System.out.println("Message has been read and displayed");
- // }
- // } catch (ClassNotFoundException e) {
- // e.printStackTrace();
- // } catch (IOException e) {
- // e.printStackTrace();
- // }
- // };
- // System.out.println("Sending and reading thread have been created, now
- // running the threads");
- // send.run();
- // read.run();
- try (Scanner scan = new Scanner(System.in)) {
- Message msg;
- while (true) {
- System.out.println("Enter message: ");
- String text = scan.nextLine();
- msg = new Message(text);
- msg.username = "Pipitone";
- out.writeObject(msg);
- }
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- public static void main(String[] args) {
- Scanner in = new Scanner("192.168.1.24 6789");
- new ClientDebug(in.next(), in.nextInt()).run();
- in.close();
- }
- public static void debug(Object o) {
- System.out.println("\tDEBUG: " + o.toString().replaceAll("\n", "\n\tDEBUG: "));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement