100 likes | 230 Vues
This document provides a detailed comparison of socket programming interfaces in C and Java, focusing on connection-oriented sockets. It analyzes key classes like Socket and ServerSocket in both languages, highlighting how they handle server and client operations, including binding, listening, and accepting connections. For C, functions such as socket(), bind(), listen(), and accept() are discussed, while for Java, the equivalent methods are explored in the context of ServerSocket and Socket classes. Practical examples demonstrate both server and client creation for effective understanding.
E N D
COMPARATIVA CLIENT/SERVIDORAMB SOCKETS C vs. JAVA MANEL DOMÍNGUEZ SERRA GERARD ROCA MALLOFRÉ
Interfície de sockets orientada a la connexió en C SERVIDOR CLIENT socket() socket() bind() connect() listen() accept() close() close()
Interfície de sockets orientada a la connexió en Java SERVIDOR CLIENT ServerSocket() Socket() accept() close() close()
Classes Socket/ServerSocket • Socket : Classe que s’utilitza en les connexions a internet orientades a la connexió, i entre diferents ordinadors de la xarxa. • ServerSocket : Classe que s’encarrega de fer les crides bind, listen i accept de C. Quan es crea la classe és quan es fa el bind, passant-li el número de port sobre l’adreça de la màquina on està. Quan es fa l’accept es retorna una instància de la classe Socket per la qual es portarà a terme la comunicació amb el client.
Comparació crides del servidor C: int s = socket ( int domini, tipus, protocol ) int bind ( int s, struct sockaddr *addr, int adrlon ) int error = listen ( int s, int long_cua ) Java: public ServerSocket(int port, int long_cua)
Comparació crides del servidor C: int snou = accept ( int s, struct sockaddr *client_adr, int *client_adrlon ) Java: public Socket accept ( ) Retorna una instància de la classe Socket per on es farà la connexió amb el client.
Comparació crides del client C: int s = socket ( int domini, tipus, protocol ) int connect ( int s, struct sockaddr *addr, int addr_len ) struct sockaddr_in { short sin_family u_short sin_port struct in_addr sin_addr /*@ IP host 32 bits*/ char sin_zero[8] } Java: public Socket Socket ( InetAdress adress, int port) public Socket Socket ( String host, int port)
Exemple de servidor en Java ServerSocket serv; Socket connexio; (...) serv = new ServerSocket ( 5000, 100); //port 5000 // 100 connexions en espera com a maxim connexio = ser.accept( ); (...)
Exemple de client en Java Socket s; (...) s = new Socket ( “alaba.fib.upc.es”, 5000 ); (...) s.close( );
Bibliografia “Cómo Programar en Java” Deiteil y Deitel Prentice Hall “Col.lecció de transparències del curs” J.R.Herrero, X.Martorell i J.Torres Pàgines web: www.javasoft.com http://java.sun.com/