什么是 TCP/IP?
TCP/IP 是供已连接因特网的计算机进行通信的通信协议。
TCP/IP 指传输控制协议/网际协议 (Transmission Control Protocol / Internet Protocol)。
TCP/IP 定义了电子设备(比如计算机)如何连入因特网,以及数据如何在它们之间传输的标准。
接下来进行TCP,客户端以及服务器端的案例演示:
客户端
1、连接服务器Socket
2、发送消息
//客户端
public class TcpClientDemo01 {
public static void main(String[] args) {
Socket socket=null;
OutputStream os=null;
try {
//1、要知道服务器的地址,编口号
InetAddress serverIP=InetAddress.getByName("127.0.0.1");
int port = 9999;
//2、创建一个socket连接
socket=new Socket(serverIP,port);
//3、发送消息 IO流
os=socket.getOutputStream();
os.write("你好,欢迎来到Java语言!".getBytes());
} catch (Exception e) {
e.printSt