package socket;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
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 + ": 연결됨");
InputStream inputStram = sock.getInputStream();
OutputStream outputStreasm = sock.getOutputStream();
byte[] buf = new byte[1024];
int count;
while ((count = inputStram.read(buf)) != -1) {
outputStreasm.write(buf, 0, count);
System.out.write(buf, 0, count);
}
outputStreasm.close();
System.out.println(sock + ": 연결 종료");
} catch (IOException ex)
{
System.out.println(sock + ": 연결 종료 (" + ex + ")");
} finally {
try {
if (sock != null)
sock.close();
} catch (IOException ex) {
}
}
}
}
}
package socket;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
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(sock + ": 연결됨");
OutputStream outputStram = sock.getOutputStream();
InputStream inputStream = sock.getInputStream();
byte[] buf = new byte[1024];
int count;
while( (count = System.in.read(buf)) != -1 )
{
outputStram.write( buf, 0, count );
count = inputStream.read( buf );
System.out.write( buf, 0, count );
}
outputStram.close();
System.out.println(sock + ": 연결 종료");
} catch( IOException ex )
{
System.out.println("연결 종료 (" + ex + ")");
} finally
{
try
{
if ( sock != null )
sock.close();
} catch( IOException ex ) {}
}
}
}
#자바동영상, #자바네트워크, #JAVA동영상, #자바강좌, #자바케트워크강좌, #자바케트워크강의, #자바케트워크교육, #자바소켓, #소켓프로그래밍, #자바소켓프로그래밍, #JAVA소켓, #JAVASocket, #ServerSocket, # 자바Socket
댓글 없음:
댓글 쓰기