在说这个小程序实现之前,我们先仔细分析下ServerSocket和Socket的众多构造方法,了解各个构造方法的细节差异,有助于我们正确的构建一个准确连接的网络应用程序,先说ServerSocket的构造方法:
1)ServerSocket()
2)ServerSocket(int port)
3)ServerSocket(int port, int backlog)
4)ServerSocket(int port, int backlog, InetAddress bindAddr)
Socket的构造方法:
1)Socket()
2)Socket(InetAddress address, int port)
3)Socket(InetAddress host, int port, boolean stream)
4)Socket(Proxy proxy)
5)potected Socket(SocketImpl impl)
6)Socket(String host, int port)
7)Socket(String host, int port, boolean stream)
8)Socket(String host, int port, InetAddress localAddr, int localPort)
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
public class TcpServer {
public static void main(String[] args) throws IOException {
//建立一个端口号为8001的服务器端ServerSocket对象
ServerSocket server = new ServerSocket(8001);
//监听客户端的连接请求
Socket acceptSocket = server.accept();
//提取socket对象的输入流
InputStream serverIn = acceptSocket.getInputStream();
//提取socket对象的输出流
OutputStream serverOut = acceptSocket.getOutputStream();
//在输出流上发送文字,客户端连接上后马上能接收到
serverOut.write("welcom to visit server.".getBytes());
//建立缓冲流来显示客户端的输入
BufferedReader buf = new BufferedReader(new InputStreamReader(serverIn));
String temp = buf.readLine();
System.out.println(temp);
//注意关闭顺序
buf.close(); //会自动关闭低层输入的字节流
serverOut.close();
acceptSocket.close();
server.close();
}
}