티스토리 뷰
================== 서버 ============================
public class Server {
public static void main(String[] arg){
ServerSocket server = null;
BufferedReader in = null;
try{
// 서버 포트번호 셋팅
server = new ServerSocket(12345);
System.out.println("Server Ready");
while(true){
// 클라이언트가 보내는 연결요청을 기다린다.
Socket s = server.accept();
// 클라이언트가 보낸 데이터를 읽는 객체를 생성한다.
in = new BufferedReader(new InputStreamReader(s.getInputStream()));
// 클라이언트 한면과 대화가 끝나기 전가지 다른 클라이언트가 보낸 연결 요청은 받지 않는다.
// 여러 클라이언트를 연결 시키려면 아래 while문을 thread로 바꿔야한다.
while(true){
String str = in.readLine();
if(str.equals("."))
break;
System.out.println("전송된 데이터 : " + str);
}
System.out.println("end connect");
}
}catch(Exception e){
System.out.println(e.getMessage());
}
}
}
================== 클라 ============================
public class Client {
public static void main(String[] args){
Scanner in = new Scanner(System.in);
Socket socket = null;
PrintWriter out = null;
try{
// 소켓 객체 생성
socket = new Socket(InetAddress.getByName("192.168.0.3"),12345);
// 해당 소켓으로 통신할 수 있는 출력 객체 생성
out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())));
}catch(Exception e){
System.out.println(e.getMessage());
}
while(true){
System.out.print("send text(finish '.') = ");
// enter 입력을 기다린다.
String str = in.nextLine();
// 출력객체에 대이터를 담는다
out.println(str);
// 소켓으로 읽은 데이터를 전송한다. flush() 는 필수
out.flush();
// 종료 조건
if(str.trim().equals("."))
break;
}
out.close();
try{
socket.close();
}catch(Exception e ){
}
}
}
'프로그래밍 언어 > 프로그래밍' 카테고리의 다른 글
[JAVA] 스트림 입출력 형식 (0) | 2016.09.30 |
---|---|
[유니티] 오브젝트 터치좌표로 이동하기 (0) | 2016.06.17 |
유니티 점프 (0) | 2016.06.08 |
해시 테이블 (hash table) (0) | 2009.01.18 |
트리 (0) | 2009.01.18 |
- Total
- Today
- Yesterday
- 김광석 기타 코드
- 임창정
- 공모전
- 청년
- 신청
- Linked List
- 연결 리스트
- 김광석 코드
- 버즈
- 안드로이드
- 축제
- android
- 혁오
- 지원
- 태풍발생
- 민경훈
- 지원금
- 청년정책
- 유니티
- 서울특별시
- 김광석
- 문화
- 청년몽땅정보통
- 뜻밖의 샤워
- 유머
- 공연
- 아이유
- 고양이 깜놀
- 발생후
- 서울시
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |