본문 바로가기
프로그래밍/자바

Echo

by 카라미 2016. 1. 22.

package kr.co.smu.network;


import java.io.BufferedReader;

import java.io.InputStreamReader;

import java.io.PrintWriter;

import java.net.Socket;


public class EchoClient {


public static void main(String[] args) {

try{

Socket socket = new Socket("127.0.0.1", 10001);

BufferedReader keyboard = 

new BufferedReader(new InputStreamReader(System.in));

BufferedReader in = 

new BufferedReader(

new InputStreamReader(socket.getInputStream()));

PrintWriter out = new PrintWriter(socket.getOutputStream());

String line = null;

while((line = keyboard.readLine())!= null){

if(line.equals("quit"))

break;

out.println(line);

out.flush();

String echo = in.readLine();

System.out.println("서버가 보낸 문자열 : "+echo);

}

out.close();

in.close();

socket.close();

}catch(Exception e){

e.printStackTrace();

}

}


}



package kr.co.smu.network;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;

public class EchoServer {

public static void main(String[] args) {
try{
ServerSocket server = new ServerSocket(10001);
System.out.println("접속 대기중!!");
Socket scoket = server.accept();
String host = scoket.getInetAddress().getHostAddress();
System.out.println(host+"로 부터 접속함!!");
InputStream in = scoket.getInputStream();
OutputStream out = scoket.getOutputStream();
BufferedReader br = 
new BufferedReader(new InputStreamReader(in));
PrintWriter pw = new PrintWriter(out);
String line = null;
while((line = br.readLine()) != null){
System.out.println("클라이언트가 보낸 문자열 : "+ line);
pw.println(line);
pw.flush();
}
pw.close();
br.close();
scoket.close();
}catch(Exception e){
e.printStackTrace();
}
}

}


'프로그래밍 > 자바' 카테고리의 다른 글

ChatClient  (0) 2016.01.22
EchoThreadServer  (0) 2016.01.22
guestbook VO, DAO  (0) 2016.01.20
member  (0) 2016.01.19
BookDAOTest  (0) 2016.01.14