1. 前言
网络上的两个程序通过一个双向的通讯连接实现数据的交换,这个双向链路的一端称为一个Socket。Socket通常用来实现客户方和服务方的连接。Socket是TCP/IP协议的一个十分流行的编程界面,一个Socket由一个IP地址和一个端口号唯一确定。
网络编程需要解决两个问题,一个是如何准确的定位网络上一台或多台主机,另一个就是找到主机后如何可靠高效的进行数据传输。
在TCP/IP协议中IP层主要负责网络主机的定位,数据传输的路由,由IP地址可以唯一地确定Internet上的一台主机。
而TCP层则提供面向应用的可靠(TCP)的或非可靠(UDP)的数据传输机制,这是网络编程的主要对象,一般不需要关心IP层是如何处理数据的。
2. Socket通信过程
Server端Listen(监听)某个端口是否有连接请求,Client端向Server 端发出Connect(连接)请求,Server端向Client端发回Accept(接受)消息。一个连接就建立起来了。Server端和Client 端都可以通过Send,Write等方法与对方通信。其工作过程包含以下四个基本的步骤:
(1) 创建Socket;
(2) 打开连接到Socket的输入/出流;
(3) 按照一定的协议对Socket进行读/写操作;
(4) 关闭Socket,用于释放资源。
3. 客户端Socket
客户端创建socket的例子:
public class Client {
public static void main(String[] args) throws IOException {
//需要连接的主机
String host = "10.108.149.5";
//需要连接的端口号
int port = 55533;
//创建面向连接的Socket对象
Socket socket = new Socket(host, port);
//得到SocketOutputStream对象,该对象继承FileOutputStream
OutputStream os = socket.getOutputStream();
String message = "发送message";
//将消息写入至输出流
os.write(message.getBytes("UTF-8"));
os.close();
socket.close();
}
}
首先创建一个Socket对象:
Socket clientSocket = new Socket(host, port);
需要给定一个主机名和一个端口号,这是一个面向连接的创建Socket对象方法,主机名可以是IP地址。
public Socket(String host, int port)
throws UnknownHostException, IOException
{
this(host != null ? new InetSocketAddress(host, port) :
new InetSocketAddress(InetAddress.getByName(null), port),
(SocketAddress) null, true);
}
由以上源码可知,该构造方法实际上是调用Socket(SocketAddress address, SocketAddress localAddr, boolean stream),该构造方法源码如下:
private Socket(SocketAddress address, SocketAddress localAddr,
boolean stream) throws IOException {
setImpl();
// backward compatibility
if (address == null)
throw new NullPointerException();
try {
createImpl(stream);
if (localAddr != null)
bind(localAddr);
//实际上是面向连接的,连接该Socket到具体的主机与端口号
connect(address);
} catch (IOException | IllegalArgumentException | SecurityException e) {
try {
close();
} catch (IOException ce) {
e.addSuppressed(ce);
}
throw e;
}
}
下面是获取输出流的源码,底层调用一个native方法
public OutputStream getOutputStream() throws IOException {
if (isClosed())
throw new SocketException("Socket is closed");
if (!isConnected())
throw new SocketException("Socket is not connected");
if (isOutputShutdown())
throw new SocketException("Socket output is shutdown");
final Socket s = this;
OutputStream os = null;
try {
os = AccessController.doPrivileged(
new PrivilegedExceptionAction<OutputStream>() {
public OutputStream run() throws IOException {
return impl.getOutputStream();
}
});
} catch (java.security.PrivilegedActionException e) {
throw (IOException) e.getException();
}
return os;
}
4. 服务器ServerSocket
服务器创建socket的例子:
public class Server {
public static void main(String[] args) throws IOException {
//监听端口号
int port = 55533;
//为指定端口号创建ServerSocket
ServerSocket serverSocket = new ServerSocket(port);
System.out.println("等待中...");
Socket socket = null;
InputStream is = null;
while (true) {
//阻塞方法,直到获取连接,得到相应的Socket
socket = serverSocket.accept();
//获取相应socket的InputStream流,读取输入流的数据
is = socket.getInputStream();
byte[] bytes = new byte[10240];
int len;
StringBuffer sb = new StringBuffer();
while ((len = is.read(bytes)) != -1) {
sb.append(new String(bytes, 0, len,"UTF-8"));
}
System.out.println(sb);
}
}
}