1.获取服务器的地址,端口号
            //1.获取服务器的地址,端口号
            InetAddress serverIP = InetAddress.getByName("127.0.0.1");
            int port = 666;
2.创建一个Socket连接,参数为:地址和端口号
            //2.创建一个Socket连接    
	       Socket socket= new Socket(serverIP,port);
3.往服务器发送内容
            //3.发送消息
	       OutputStream os = socket.getOutputStream();
            os.write("hello,world!".getBytes());
4.关闭流(先开后关)
           //4.关闭流
	      if(os != null){
                try {
                    os.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(socket != null){
                try {
                    socket.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
1.创建一个IP地址
             //1、新建一个ip地址
             ServerSocket serverSocket  = new ServerSocket(666);
2. 等待客户端连接过来
             //2.等待客户端连接过来
             Socket socket = serverSocket.accept();
3.读取客户端的信息
             //3.读取客户端的消息
             is = socket.getInputStream();
             //管道流
             baos = new ByteArrayOutputStream();
             byte[] buffer = new byte[1024];
             int len;
             while ((len = is.read(buffer))!= -1){
                 baos.write(buffer,0,len);
             }
             System.out.println(baos.toString());
4.关闭流
           //4.关闭流
	   if(baos != null){
                try {
                     baos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
             }
            if(is != null){
                try {
                    is.close();
                } catch (IOException e) {
                    e.printStackTrace();
            }
            if(socket != null){
                try {
                    socket.close();
                } catch (IOException e) {
                    e.printStackTrace();
            }
            if(serverSocket != null){
                try {
                    serverSocket.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
             }
package com.gitee.luee.network;
import java.io.IOException;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;
//客户端
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 = 666;
            //2.创建一个socket连接
             socket = new Socket(serverIP,port);
            //3.发送消息
             os = socket.getOutputStream();
            os.write("hello,world!".getBytes());
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            if(os != null){
                try {
                    os.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(socket != null){
                try {
                    socket.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
package com.gitee.luee.network;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;
//服务器
public class TcpServerDemo01 {
    public static void main(String[] args) {
        ServerSocket serverSocket = null;
        Socket socket = null;
        InputStream is = null;
        ByteArrayOutputStream baos = null;
        
        try {
            //1、新建一个ip地址
             serverSocket = new ServerSocket(666);
            while (true){
                //2.等待客户端连接过来
                socket = serverSocket.accept();
                //3.读取客户端的消息
                is = socket.getInputStream();
                //管道流
                baos = new ByteArrayOutputStream();
                byte[] buffer = new byte[1024];
                int len;
                while ((len = is.read(buffer))!= -1){
                    baos.write(buffer,0,len);
                }
                System.out.println(baos.toString());
            }
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if(baos != null){
                try {
                    baos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(is != null){
                try {
                    is.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(socket != null){
                try {
                    socket.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(serverSocket != null){
                try {
                    serverSocket.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

至此,实现一个简单的tcp传递信息的例子就完成了。
原文:https://www.cnblogs.com/lyj-tec-blog/p/14265080.html