티스토리 뷰

데이터 통신이 없습니다.

그냥 서버와 클라이언트의 연결만 하는 예제입니다.

서버먼저 실행시켜주신 후에 클라이언트에서 연결요청을 해야합니다.


SERVER

package network;


import java.io.*;

import java.net.*;


public class ServerExample {


public static void main(String[] args) {


ServerSocket serverSocket = null;

try {

serverSocket = new ServerSocket();

serverSocket.bind(new InetSocketAddress("localhost", 7121));

while(true) {

System.out.println("WATING CONNECT");

Socket socket = serverSocket.accept();

InetSocketAddress isa = (InetSocketAddress) socket.getRemoteSocketAddress();

System.out.println("ACCEPT THE CONNECT(" + isa.getHostName() + ")");

}

}catch(Exception e) {

}

if(!serverSocket.isClosed()) {

try {

serverSocket.close();

}catch(IOException e1) {

}

}

}


}


CLIENT

package network;


import java.net.*;

import java.io.*;

public class ClientExample {


public static void main(String[] args) {

Socket socket = null;

try {

socket = new Socket();

System.out.println("REQUEST THE CONNECT");

socket.connect((new InetSocketAddress("localhost", 7121)));

System.out.println("SUCCESS THE CONNECT");

}catch(Exception e) {

}

if(!socket.isClosed()) {

try {

socket.close();

}catch(IOException e1) {

}

}


}


}





댓글
댓글쓰기 폼