java_tcp连接

发送消息

server

package com.llt.lesson02;

import java.io.BufferedInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;

public class TcpServer {
    public static void main(String[] args) {
        //创建地址
        ServerSocket serverSocket = null;
        ByteArrayOutputStream bos = null;
        Socket accept = null;
        InputStream inputStream = null;
        try {
             serverSocket = new ServerSocket(9999);
             //接收消息
            accept = serverSocket.accept();
            inputStream = accept.getInputStream();
            //管道流接收
            bos = new ByteArrayOutputStream();
         byte[] by = new byte[1024];
         int len;
         while ((len=inputStream.read(by))!=-1){
             bos.write(by,0,len);
         }
            System.out.println(bos.toString());

        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            //关闭流
            try {
               if( bos != null){
                   bos.close();
               }



            } catch (IOException e) {
                e.printStackTrace();
            }
            if( inputStream != null){
                try {
                    inputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if( accept != null){
                try {
                    accept.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if( serverSocket != null){
                try {
                    serverSocket.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

        }
    }
}

client

package com.llt.lesson02;

import java.io.IOException;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;

public class TcpClient {
    public static void main(String[] args) {
        //创建连接容器
        InetAddress byName;
        Socket socket = null;
        OutputStream outputStream=null;
        try {
            byName = InetAddress.getByName("127.0.0.1");
            int prot = 9999;
            //创建socket 连接
            socket  = new Socket(byName,prot);
            //发送消息 io流
            outputStream  = socket.getOutputStream();
            outputStream.write("你好我发送消息了".getBytes());
        } catch (UnknownHostException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if (outputStream!=null){
                try {
                    outputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (socket!=null){
                try {
                    socket.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

文件上传

client

package com.llt.lesson02;

import java.io.*;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;

public class TcpClient02 {
    public static void main(String[] args) throws Exception {

            Socket socket = new Socket(InetAddress.getByName("127.0.0.1"), 9999);
            //创建输出流
            OutputStream outputStream = socket.getOutputStream();
            //读取文件
            FileInputStream fileInputStream = new FileInputStream("debug.log");
                //写出文件
            byte[] bytes = new byte[1024];
            int len;
            while ((len=fileInputStream.read(bytes))!=-1){
                outputStream.write(bytes,0,len);
            }
        socket.shutdownOutput(); //我发送完毕
           //接手服务器发送的消息
        InputStream inputStream = socket.getInputStream();
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        int len2;
        byte[] bytes2 = new byte[1024];
        while ((len2=inputStream.read(bytes2))!=-1){
            byteArrayOutputStream.write(bytes2,0,len2);
        }
        System.out.println(byteArrayOutputStream.toString());
        inputStream.close();
        byteArrayOutputStream.close();
        fileInputStream.close();
        outputStream.close();
        socket.close();

    }
}

server

package com.llt.lesson02;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;

public class TcpServer02 {
    public static void main(String[] args) throws IOException {
        //创建地址
        ServerSocket serverSocket = null;
        Socket accept = null;
        InputStream inputStream = null;
             serverSocket = new ServerSocket(9999);
            //监听客户端连接
            accept = serverSocket.accept();
             //获取输入流
        inputStream = accept.getInputStream();
            //文件输出
        FileOutputStream fos = new FileOutputStream("let.log");
         byte[] by = new byte[1024];
         int len;
         while ((len=inputStream.read(by))!=-1){
             fos.write(by,0,len);
         }
         //通知客户端我接好了
        OutputStream outputStream = accept.getOutputStream();
        outputStream.write("我接收ok".getBytes());
        fos.close();
         inputStream.close();
        accept.close();
         serverSocket.close();

    }
}

Java中,Modbus TCP是一种用于工业控制系统通信的协议,通常用于设备之间交换数据,如PLC、SCADA系统等。如果你想要通过Java修改Modbus TCP的数据,首先你需要一个支持Modbus TCP库,比如jmodbus或modbus4j。 以下是一个简单的步骤: 1. **添加依赖**:在你的项目中引入Modbus TCP相关的jar包,例如`com.pi4j:pi4j-modbus:...` (对于Pi4J库) 或 `org.modbus4j:modbus4j:...` (对于modbus4j库)。 2. **初始化连接**:创建一个ModbusSerialConnection或ModbusTcpConnection实例,并指定目标主机和端口。 ```java ModbusTcpConnection connection = new ModbusTcpConnection("localhost", 502); connection.open(); ``` 3. **解析请求**:选择相应的功能码(如读取输入寄存器或写入单个寄存器),并设置地址范围和数据。 ```java ModbusFunction function = ModbusFunction.READ_INPUT_REGISTERS; int[] address = {1}; // 要读取的第一个寄存器地址 int quantity = 1; // 需要读取的寄存器数量 InputRequest request = new InputRequest(function, address, quantity); ``` 4. **发送请求并接收响应**:发送请求到服务器并处理响应。 ```java try { Response response = connection.execute(request); int[] result = response.getValues(); // 获取读取到的数据 } catch (Exception e) { e.printStackTrace(); } ``` 5. **更新数据**:假设你已经获取了需要更新的数据,可以将其写回对应的寄存器。 ```java function = ModbusFunction.WRITE_SINGLE_REGISTER; address = {2}; // 更新的寄存器地址 int newValue = 100; // 新值 OutputRequest writeRequest = new OutputRequest(function, address, newValue); connection.execute(writeRequest); ``` 6. **关闭连接**:当完成操作后,别忘了关闭连接。 ```java connection.close(); ```
评论 1
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值