网络模型
TCP是传输层协议,socket建立链接是端对端的,在传输层进行数据交互
http协议是应用层协议
客户端
public class client {
public static void main(String[] args) throws IOException {
Socket socket = new Socket("localhost",10086);
OutputStream os = socket.getOutputStream();
DataOutputStream dos = new DataOutputStream(os);
dos.writeUTF("你好我是socket连接客户端");
dos.close();
os.close();
socket.close();
}
}
服务端
public static void main(String[] args) throws IOException {
ServerSocket serverSocket = new ServerSocket(10086);
Socket accept = serverSocket.accept();
InputStream is = accept.getInputStream();
DataInputStream dis = new DataInputStream(is);
String res = dis.readUTF();
System.out.println(res);
dis.close();
is.close();
serverSocket.close();
}
以上是 网络模型 的全部内容, 来源链接: utcz.com/z/518091.html