今天来使用JAVA实现以下简单的TCP使用
TCP是一个面向连接的,可靠的,面向字节流的传输层通信协议。
分别需要编写客户端和服务器两个
先上客户端代码:
import java.io.IOException;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.Socket;
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");
//2 端口号
int port = 9999;
//3 创建一个socket连接
socket = new Socket(serverIP, port);
//4 发送消息
os = socket.getOutputStream();
os.write("hello".getBytes());
}catch (Exception e){
e.printStackTrace();
}finally {
if (os != null){