A sentimental robot

1:1 채팅 본문

Java

1:1 채팅

GOD03219 2018. 9. 6. 19:35

package Network;

import java.io.*;
import java.net.*;
import java.util.Scanner;

class Sender extends Thread {
 Socket socket;
 DataOutputStream out;
 String identifier;

 Sender(Socket socket, String id) {

  this.socket = socket;
  identifier = "[" + id + ":" + socket.getPort() + "] says:";

  try {
   out = new DataOutputStream(socket.getOutputStream());

  } catch (IOException e) {

   e.printStackTrace();

  }

 }

 public void run() {
  Scanner sc = new Scanner(System.in);

  while (out != null) {
   System.out.print("Message :");
   String m = sc.next();
   try {
    out.writeUTF(identifier + m);
   } catch (IOException e) {
    // 접속이 끊겼을 때 out이 보낼 곳(socket.getOutputStream())을 잃었기 때문에 보낼 메세지를 입력하면 while문에
    // 의해서 예외처리 됨

    System.out.println("---DataOutputStream lost---");
    e.printStackTrace();

    try {
     out.close();
     out = null; // while문을 나가기 위해 out=null

    } catch (IOException e1) {

     e1.printStackTrace();

    }

   }

  }
 }

}

class Receiver extends Thread {
 Socket socket;
 DataInputStream in;

 Receiver(Socket socket) {
  this.socket = socket;

  try {
   in = new DataInputStream(socket.getInputStream());
  } catch (IOException e) {

   e.printStackTrace();

  }
 }

 public void run() {

  while (in != null) {
   try {
    System.out.printf("\n %s \n", in.readUTF());
   } catch (IOException e) {

    // 접속이 끊겨서 읽을 메세지가 없음
    System.out.println("---DataInputStream lost---");
    e.printStackTrace();

    try {
     in.close();
     in = null;

    } catch (IOException e1) {

     e1.printStackTrace();

    }

   }
  }
 }
}

public class Stream {

 public static void main(String[] args) {

  Scanner sc = new Scanner(System.in);
  ServerSocket server = null;
  Socket socket = null;

  try {
   server = new ServerSocket(7475);
   System.out.println(server.getInetAddress() + " Server ready.");

   socket = server.accept();

   System.out.println("Connected to a client.");
   System.out.println("Server ID :");

   String a = sc.next();

   Sender sender = new Sender(socket, a); // write
   Receiver receiver = new Receiver(socket); // read

   sender.start();
   receiver.start();

  } catch (IOException e) {

   e.printStackTrace();
  }

 }

}

 

-------------------------------------------------------------------------------

 

package Network;

import java.io.*;
import java.net.*;
import java.util.Scanner;

public class Client {

 public static void main(String[] args) {
  String address = "127.0.0.1";
  Scanner sc = new Scanner(System.in);

  try {
   System.out.println("연결요청중...");

   Socket socket = new Socket(address, 7475);
   System.out.println("연결이 되었습니다. ID를 입력하세요 :");

   String n = sc.next();

   Sender sender = new Sender(socket, n);
   Receiver receiver = new Receiver(socket);

   sender.start();
   receiver.start();

  } catch (IOException e) {

   e.printStackTrace();
  }

 }
}

'Java' 카테고리의 다른 글

다시 한번 Has~a  (0) 2018.10.03
Multi Chatting  (0) 2018.09.07
NullPointerException  (0) 2018.06.30
Platform.runLater( )  (0) 2018.02.07
Chatting UI  (0) 2018.02.07