2013년 10월 18일 금요일

EchoClient, EchoServer

-------------------------
import java.io.*;
import java.net.*;

class EchoClient {
public static void main(String args[]) throws IOException {
Socket sock = null;
try{
sock = new Socket(args[0], Integer.parseInt(args[1]));
System.out.println("ok~");
}
catch (Exception e)  {
System.out.println("Error : " + e);
}
finally {
if (sock != null){
sock.close();
}
}
}
};












----------------------------------
import java.io.*;
import java.net.*;

class EchoServer
{  public static void main( String[] args )
        throws IOException 
    {
        ServerSocket serverSock =
            new ServerSocket( Integer.parseInt(args[0]) );
        System.out.println(serverSock + ": 서버 소켓 생성");
        while(true)
        {
            Socket sock = null;
            try
            {
                sock = serverSock.accept();
                System.out.println(sock + ":    OutputStream toClient = sock.getOutputStream();
                byte[] buf = new byte[1024];
                int count;
                while( (count = fromClient.read(buf)) != -1 ) {
                    toClient.write( buf, 0, count );
System.out.write(buf, 0, count);
}
                toClient.close();
                System.out.println(sock + ": 연결 종료");
            } catch( IOException ex )
            {
                System.out.println(sock + ": 연결 종료 (" + ex + ")");
            } finally 
            {
                try
        &nbs                  sock.close();
                } catch( IOException&nb 

댓글 없음:

댓글 쓰기