개발/Java

자바 Socket 클래스

728x90

package java.net에 선언된 클래스. 데이터를 보내는 쪽에서 객체를 생성하여 사용한다.

데이터를 보내는 쪽에서 많이 사용하고. 원격에 있는 장비와의 연결 상태를 보관하기 위한 클래스

 

 

javadocs에는 아래와 같이 기술되어 있다.

"소켓은 두개의 machine의 연결을 위한 endpoint이다."

실제 동작은 SocketImpl의 instance에 의해 동작한다.

데이터를 보내기 위한 클래스이고, 데이터를 받는 서버에서는 클라에서 접속을 하면 Socket 객체를 생성하지만, 데이터를 보내는 쪽에서는 직접 생성해야 한다.

 

 

 * This class implements client sockets (also called just
 * "sockets"). A socket is an endpoint for communication
 * between two machines.
 * <p>
 * The actual work of the socket is performed by an instance of the
 * {@code SocketImpl} class. An application, by changing
 * the socket factory that creates the socket implementation,
 * can configure itself to create sockets appropriate to the local
 * firewall.

 

 

 

ServerSocket 클래스

1.0에서 소개됨. 네트워크 상의 응답을 기다린다. 실제 동작은 SockeImpl의 인스턴스에 의해 동작한다.

애플리케이션은 소켓 팩토리를 통해 , local의 방화벽에 맞게 소켓 구현을 설정할 수 있다.

public class ServerSocket implements java.io.Closeable {
}

 

 * This class implements server sockets. A server socket waits for
 * requests to come in over the network. It performs some operation
 * based on that request, and then possibly returns a result to the requester.
 * <p>
 * The actual work of the server socket is performed by an instance
 * of the {@code SocketImpl} class. An application can
 * change the socket factory that creates the socket
 * implementation to configure itself to create sockets
 * appropriate to the local firewall.

그리고 bindAddr을 통해서, 특정 주소에서만 접근이 가능하도록 지정할 때 사용함.

 

  * Create a server with the specified port, listen backlog, and
     * local IP address to bind to.  The <i>bindAddr</i> argument
     * can be used on a multi-homed host for a ServerSocket that
     * will only accept connect requests to one of its addresses.

accept() 객체 생성후 사용자의 요청을 대기하는 메소드 SocketImpl에 구현되어 있다.

    /**
     * Accepts a connection.
     *
     * @param      s   the accepted connection.
     * @throws     IOException  if an I/O error occurs when accepting the
     *               connection.
     */
    protected abstract void accept(SocketImpl s) throws IOException;

close() 소켓 연결을 종료하는 메소드

public interface AutoCloseable {
	void close() throws Exception;
} 
    /**
     * Closes this socket.
     *
     * @throws     IOException  if an I/O error occurs when closing this socket.
     */
    protected abstract void close() throws IOException;

 위 close는 Closeable 인터페이스에 구현되어 있다.

그리고 AutoCloseable 클래스는 아래와 같다.

1.7에서 소개되었고, 

 * An object that may hold resources (such as file or socket handles)
 * until it is closed. The {@link #close()} method of an {@code AutoCloseable}
 * object is called automatically when exiting a {@code
 * try}-with-resources block for which the object has been declared in
 * the resource specification header. This construction ensures prompt
 * release, avoiding resource exhaustion exceptions and errors that
 * may otherwise occur.

public interface Closeable extends AutoCloseable {

    public void close() throws IOException;
}

 

 

 

 

 

 

 

'개발 > Java' 카테고리의 다른 글

non-reifiable 타입이란  (0) 2021.05.17
Java Datagram 관련 클래스  (0) 2021.05.17
Funcitional Programming (함수형 프로그래밍)  (0) 2021.05.17
BigInteger, BigDecimal 클래스  (0) 2021.05.17
Throwable, Exception Class  (0) 2021.05.17