Java实现socket编程入门

package bio;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Scanner;

public class TCPserver {
    public static void main(String[] args) throws IOException {
        ServerSocket ss = new ServerSocket(9999);
        while(true){
            Socket socket = ss.accept();
            InputStream in = socket.getInputStream();
            byte[] bytes = new byte[1024];
            in.read(bytes);

            String cip = socket.getInetAddress().getHostAddress();
            System.out.println(cip + " say: " + new String(bytes).trim());

            OutputStream out = socket.getOutputStream();
            Scanner sc = new Scanner(System.in);
            System.out.print("请输入:");
            out.write(sc.nextLine().getBytes());
            socket.close();
        }
    }
}

package bio;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.util.Scanner;

public class TcpClient {
    public static void main(String[] args) throws IOException {
        while (true){
            Socket socket = new Socket("127.0.0.1",9999);
            OutputStream out = socket.getOutputStream();
            System.out.print("请输入:");
            Scanner sc = new Scanner(System.in);
            out.write(sc.nextLine().getBytes());
            InputStream in = socket.getInputStream();
            byte[] bytes = new byte[1024];
            in.read(bytes);
            System.out.println(socket.getInetAddress().getHostAddress() + " : " + new String(bytes).trim());
            socket.close();

        }
    }
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,你的问题是关于Java socket和PLC通信的。在Java中,可以使用Socket API来实现网络编程,包括与PLC通信。下面是一个简单的Java代码示例,用于向PLC发送数据: ```java import java.io.*; import java.net.*; public class PLCClient { public static void main(String[] args) throws IOException { String hostName = "localhost"; // PLC的IP地址 int portNumber = 12345; // PLC监听的端口号 try (Socket socket = new Socket(hostName, portNumber); PrintWriter out = new PrintWriter(socket.getOutputStream(), true); BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream())); BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in))) { String userInput; while ((userInput = stdIn.readLine()) != null) { out.println(userInput); // 向PLC发送数据 System.out.println("PLC返回的数据:" + in.readLine()); // 读取PLC返回的数据 } } catch (UnknownHostException e) { System.err.println("无法连接到主机:" + hostName); System.exit(1); } catch (IOException e) { System.err.println("无法在端口 " + portNumber + " 上进行连接。"); System.exit(1); } } } ``` 在这个例子中,我们使用了Socket类来建立与PLC的连接,并使用PrintWriter和BufferedReader类来读写数据。当我们向PLC发送数据时,我们可以使用out.println()方法,当我们接收到PLC返回的数据时,我们可以使用in.readLine()方法。 请注意,这只是一个简单的例子,实际上,与PLC通信可能需要使用特定的协议和数据格式。同时需要考虑到网络延迟和数据丢失等问题。因此,如果需要进行生产环境的PLC通信,请务必仔细研究PLC的通信协议和数据格式,以确保数据的正确传输和处理。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值