Professional Documents
Culture Documents
Bao Cao KT Qu Bai TP LN D An Mon H
Bao Cao KT Qu Bai TP LN D An Mon H
HCM
Bài Gi ng 2
KHÁI NI M V SOCKET
Socket API
– Ð c gii thi
u BSD4.1 UNIX, 1981
– Ð c ng dng khi t
o, s
dng và h
yb
.
– Dùng cơ ch client/server
– Cung cp hai dch v chuyn d ᄉ li
u thông qua socket API:
• unreliable datagram
• reliable, byte stream-oriented
KHÁI NI M V SOCKET
Gói java.net
– InetAddress
– ServerSocket
– Socket
– URL
– URLConnection
– DatagramSocket
InetAddress class
– Class mô t ᄃ v a ch IP (Internet Protocol)
– Các phương th c getLocalHost, getByName,
hay getAllByName t
o m-t 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 tên dùng các phương th c:
getHostAddress()
getHostName()
In a ch IP c a localhost
import java.net.*;
public class HostInfo {
public static void main(String args[]) {
HostInfo host = new HostInfo();
host.init();
}
public void init() {
try {
InetAddress myHost = InetAddress.getLocalHost();
System.out.println(myHost.getHostAddress());
System.out.println(myHost.getHostName());
} catch (UnknownHostException ex)
{ System.err.println("Cannot find local
host");
}
}
}
In a ch IP c a proxy.hcmut.edu.vn
import java.net.*;
class kku{
public static void main (String args[]) {
try {
InetAddress[] addresses =
InetAddress.getAllByName(“proxy.hcmut.edu.vn");
for (int i = 0; i < addresses.length; i++) {
System.out.println(addresses[i]);
}
}
catch (UnknownHostException e) {
System.out.println("Could not find proxy.hcmut.edu.vn");
}
}
}
Khoa Khoa H c và K Thut Máy Tính Trang 15
Tröôøng Ñaïi Hoïc Baùch Khoa Tp.HCM
Socket class
– Class mô t ᄃ v socket
– T
o m-t 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()
Khoa Khoa H c và K Thut Máy Tính Trang 18
Tröôøng Ñaïi Hoïc Baùch Khoa Tp.HCM
ServerSocket class
– Class mô t ᄃ v ServerSocket
– T
o m-t ServerSocket
ServerSocket(int port) throws IOException
ServerSocket(int port, int backlog) throws IOException
ServerSocket(int port, int backlog, InetAddress bindAddr) throws
IOException
Khoa Khoa H c và K Thut Máy Tính Trang 22
Tröôøng Ñaïi Hoïc Baùch Khoa Tp.HCM
ServerSocket class
– Các phương th c trong ServerSocket
Socket accept() throws IOException : L!ng nghe m-t kt ni n
socket này và chp nhn nó.
void close() throws IOException : óng socket.
InetAddress getInetAddress() : tr ᄃ v a ch cc b- c
a socket
int getLocalPort() : Tr ᄃ v port mà server ang l!ng nghe.
void setSoTimeout(int timeout) throws SocketException
Enable/disable SO_TIMEOUT vi khai báo timeout (milliseconds)
DateTime Server
import java.net.*;
import java.io.*;
import java.util.Date;
public class DayTimeServer {
public final static int daytimePort = 5000;
public static void main(String[] args) {
ServerSocket theServer;
Socket theConnection;
PrintStream p;
try {
theServer = new ServerSocket(daytimePort);
VÍ D (UDP Client)
VÍ D (UDP Client)
VÍ D (UDP Server)
VÍ D (UDP Server)
Client
– Khi t
o TCP socket.
– Xác nh IP address, port number c
a server.
– Thit lp kt ni n server.
Khi server nhn yêu c"u kt ni, nó s# chp nhn yêu c"u và khi t
o socket mi giao tip vi client.
– Có th server chp nhn nhiu client t
i m-t thi im.
VÍ D (TCP Client)
VÍ D (TCP Server)
BÀI T P
Vi t chương trình trên Java/C tương t
như nslookup:
– Cho 1 tên tìm ra a ch IP.
– Cho 1 a ch IP tìm ra tên.
– Giao di
n tương t$ như sau:
Khoa Khoa H c và K Thut Máy Tính Trang 38