import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Scanner;
class RecvThread extends Thread {
Socket s;
public RecvThread(Socket s) {
this.s = s;
}
@Override
public void run() {
// 받는 부분
BufferedReader is;
try {
is = new BufferedReader(new InputStreamReader(s.getInputStream()));
String recvMsg;
while ((recvMsg = is.readLine()) != null) {
System.out.println(recvMsg);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
class SendThread extends Thread {
Socket s;
public SendThread(Socket s) {
this.s = s;
}
@Override
public void run() {
PrintWriter os;
try {
os = new PrintWriter(s.getOutputStream(), true);
// 보내는 부분
Scanner sc = new Scanner(System.in);
while (true) {
String msg = sc.next();
os.println(msg);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public class ClientTest {
public static void main(String[] args) {
try {
Socket s = new Socket("115.20.247.142", 4000);
SendThread st = new SendThread(s);
RecvThread rt = new RecvThread(s);
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
'Programming > JAVA,JSP' 카테고리의 다른 글
Java ScrollPane 활용 (0) | 2014.07.12 |
---|---|
java (0) | 2014.07.12 |
Java 선택 정렬 알고리즘 구현 (0) | 2014.06.14 |
String to int, int to String (0) | 2014.06.07 |
이클립스 라인넘버 표시 (1) | 2014.03.07 |