TCP et java |
Les sockets Java (prises) sont des mecanismes de communication
de plus bas niveau que les URLs bien que toujours independantes de la configuration
materielle et logicielle utilisee. Une socket est une liaison point a point
entre un serveur et un client (le code des programmes est donc legerement
different). La communication est full-duplex (en fait, les communications
sont probablement bufferisees avant d'etre envoyees, d'ou l'apparence du
full duplex la ou TCP/IP ne sait faire que du half-duplex) et le protocole
d'echange des informations est laissee a la charge du programmeur.
Pour creer la socket du cote du programme client, le programmeur doit
donner le nom de la machine ou se trouve
le serveur et le port ou l'on doit
se connecter :
ma_socket = new Socket("machine.fr", 1999) De son cote, le serveur doit creer une serverSocket en indiquant son numero de port, puis attendre qu'un client demande une connexion qui peut alors etre acceptee. Un port mis a 0 indique que c'est le systeme qui choisit lui-meme son port. Des flux d'entree/sortie sont ensuite definis et les echanges peuvent commencer selon le protocole choisi. En fin de programme, les sockets doivent etre liberees. |
![]() |
L'utilisation de sockets avec des processus legers et une applet comme dans le cas suivant pose un certain nombre de problemes particuliers :
Methodes :
Methodes du serveur* :
ServerSocket(int) Creates a server socket on a specified port. ServerSocket(int, int) Creates a server socket and binds it to the specified local port number. ServerSocket(int, int, InetAddress) Create a server with the specified port, listen backlog, and local IP address to bind to. accept() Listens for a connection to be made to this socket and accepts it. close() Closes this socket. getInetAddress() Returns the local address of this server socket. getLocalPort() Returns the port on which this socket is listening. getSoTimeout() Retrive setting for SO_TIMEOUT. implAccept(Socket) Subclasses of ServerSocket use this method to override accept() to return their own subclass of socket. setSocketFactory(SocketImplFactory) Sets the server socket implementation factory for the application. setSoTimeout(int) Enable/disable SO_TIMEOUT with the specified timeout, in milliseconds. toString() Returns the implementation address and implementation
port of this socket as a String.
Methodes du client *: Socket() Creates an unconnected socket, with the system-default type of SocketImpl. Socket(InetAddress, int) Creates a stream socket and connects it to the specified port number at the specified IP address. Socket(InetAddress, int, InetAddress, int) Creates a socket and connects it to the specified remote address on the specified remote port. Socket(SocketImpl) Creates an unconnected Socket with a user-specified SocketImpl. Socket(String, int) Creates a stream socket and connects it to the specified port number on the named host. Socket(String, int, InetAddress, int) Creates a socket and connects it to the specified remote host on the specified remote port. close() Closes this socket. getInetAddress() Returns the address to which the socket is connected. getInputStream() Returns an input stream for this socket. getLocalAddress() Gets the local address to which the socket is bound. getLocalPort() Returns the local port to which this socket is bound. getOutputStream() Returns an output stream for this socket. getPort() Returns the remote port to which this socket is connected. getSoLinger() Returns setting for SO_LINGER. getSoTimeout() Returns setting for SO_TIMEOUT. getTcpNoDelay() Tests if TCP_NODELAY is enabled. setSocketImplFactory(SocketImplFactory) Sets the client socket implementation factory for the application. setSoLinger(boolean, int) Enable/disable SO_LINGER with the specified linger time. setSoTimeout(int) Enable/disable SO_TIMEOUT with the specified timeout, in milliseconds. setTcpNoDelay(boolean) Enable/disable TCP_NODELAY (disable/enable Nagle's algorithm). toString() Converts this socket to a String. |
* Extrait de la doc SUN.