package com.socket;//채팅방 클라이언트

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.Socket;


public class EchoClient {
	private String ip;
	private int port;
	private String str;
	BufferedReader file;
	public EchoClient(String ip, int port)throws IOException{
		this.ip =ip;
		this.port = port;
		Socket tcpSocket = getSocket();
		OutputStream os_socket = tcpSocket.getOutputStream();
		InputStream is_socket = tcpSocket.getInputStream();
		
		//서버로 전송할 데이터를 처리하기 위한 객체
		BufferedWriter bufferW = new BufferedWriter(new OutputStreamWriter(os_socket));
		//서버에서 전송된 데이터 를 처리하기 위한 객체
		BufferedReader bufferR = new BufferedReader(new InputStreamReader(is_socket));
		
		System.out.print("입력 : ");
		file = new BufferedReader(new InputStreamReader(System.in));
		str = file.readLine();
		str += System.getProperty("line.separator");
		
		//서버로 데이터 전송 작업 수행
		bufferW.write(str);
		bufferW.flush();
		
		//서버로 부터 전송된 데이터를 읽는 작업 수행
		str = bufferR.readLine();
		System.out.println("Echo Result : "+str);
		
		file.close();
		
		bufferW.close();
		bufferR.close();
		tcpSocket.close();
	}
	public Socket getSocket(){
		Socket tcpSocket = null;
		try{
			tcpSocket = new Socket(ip,port); 
		}catch (IOException ioe){
			ioe.printStackTrace();
			System.exit(0);
		}
		return tcpSocket;
	}
	public static void main(String[] args) throws IOException{
		new EchoClient("211.183.2.73",3000);
	}
}




package com.socket;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.ServerSocket;
import java.net.Socket;

public class  EchoServer{
    private BufferedReader bufferR; 
    private BufferedWriter bufferW;
    private InputStream is;
    private OutputStream os;
    private ServerSocket serverS;
    public EchoServer(int port){
        try{
            serverS = new ServerSocket(port);
        }catch(IOException ioe){
            ioe.printStackTrace();
            System.exit(0);
        }
        while(true){
            try{
                System.out.println("클라이언트를 요청을 기다리는 중");
                Socket tcpSocket = serverS.accept();
                System.out.println("클라이언트의 IP 주소 : "+
                    tcpSocket.getInetAddress().getHostAddress());
                is = tcpSocket.getInputStream();
                os = tcpSocket.getOutputStream();
                bufferR = new BufferedReader(
                        new InputStreamReader(is));
                bufferW = new BufferedWriter(
                        new OutputStreamWriter(os));                
                String message = bufferR.readLine();
                System.out.println("수신메시지 : "+ message);
                message = message + 
                    System.getProperty("line.separator");
                bufferW.write(message);
                bufferW.flush();
                bufferR.close();
                bufferW.close();
                tcpSocket.close();
            }catch(IOException ioe){
                ioe.printStackTrace();
            }
        }    
    }
    public static void main(String[] args){
        new EchoServer(3000);
    }
}


'Java > 네트워크' 카테고리의 다른 글

채팅방2  (0) 2012.04.13
네트워크  (0) 2012.04.13

+ Recent posts