Professional Documents
Culture Documents
Lec2 PDF
Lec2 PDF
HCM
Bi Ging 2
Socket API
c gii thiu BSD4.1 UNIX, 1981
c ng dng khi to, s dng v hy b.
Dng c ch client/server
Cung cp hai dch v chuyn d liu thng qua
socket API:
unreliable datagram
reliable, byte stream-oriented
Gi java.net
InetAddress
ServerSocket
Socket
URL
URLConnection
DatagramSocket
InetAddress class
Class m t v a ch IP (Internet Protocol)
Cc phng thc getLocalHost, getByName, hay
getAllByName to mt InetAddress instance:
public static InetAddess InetAddress.getByName(String hostname)
public static InetAddess [] InetAddress.getAllByName(String
hostname)
public static InetAddess InetAddress.getLocalHost()
ly a ch IP hay tn dng cc phng thc:
getHostAddress()
getHostName()
Socket class
Class m t v socket
To mt socket
Socket(InetAddress address, int port)
Socket(String host, int port)
Socket(InetAddress address, int port, InetAddress, localAddr, int
localPort)
Socket(String host, int port, InetAddress, localAddr, int localPort)
Socket()
ServerSocket class
Class m t v ServerSocket
To mt ServerSocket
ServerSocket(int port) throws IOException
ServerSocket(int port, int backlog) throws IOException
ServerSocket(int port, int backlog, InetAddress bindAddr) throws
IOException
ServerSocket class
Cc phng thc trong ServerSocket
Socket accept() throws IOException : Lng nghe mt kt ni n
socket ny v chp nhn n.
void close() throws IOException : ng socket.
InetAddress getInetAddress() : tr v a ch cc b ca socket
int getLocalPort() : Tr v port m server ang lng nghe.
void setSoTimeout(int timeout) throws SocketException
Enable/disable SO_TIMEOUT vi khai bo timeout (milliseconds)
V D (UDP Client)
V D (UDP Client)
V D (UDP Server)
V D (UDP Server)
V D (TCP Client)
V D (TCP Server)
BI TP
Vit chng trnh trn Java/C tng t nh
nslookup:
Cho 1 tn tm ra a ch IP.
Cho 1 a ch IP tm ra tn.
Giao din tng t nh sau: