일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | 6 | 7 |
8 | 9 | 10 | 11 | 12 | 13 | 14 |
15 | 16 | 17 | 18 | 19 | 20 | 21 |
22 | 23 | 24 | 25 | 26 | 27 | 28 |
29 | 30 | 31 |
- docker example
- html id
- #C++ has~a
- 토큰경제
- #자바상속#자바이즈어#is~a
- border-box
- #1차원배열
- #C++ 연산자함수오버로딩
- git flow
- #CallByAddress
- #binary
- #성적관리프로그램
- #3차원배열
- html code
- relative path
- #JAVASCRIPT
- hyperledger transaction
- html plug-in
- html video
- #다차원포인터
- html5 new tag
- 하이퍼레저패브릭
- html multimedia
- html youtube
- html object
- #android activity
- #2차원배열
- mac terminal command
- #bubbleSort
- html charset
- Today
- Total
A sentimental robot
1:1 채팅 본문
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 |