• serverSocket编程DEMO


    目录

    0、jar包依赖

    1、ServerSocket服务端主程序:

    2、任务执行类

    3、tomcatJdbcPool:

    4、客户端:


    0、jar包依赖

    1、ServerSocket服务端主程序:

    1. package socket;
    2. import java.net.ServerSocket;
    3. import java.net.Socket;
    4. import java.util.concurrent.ExecutorService;
    5. import java.util.concurrent.Executors;
    6. /**
    7. * 服务端,主程序。
    8. * @author Administrator
    9. *
    10. */
    11. public class M {
    12. public static void main(String[] args) throws Exception {
    13. @SuppressWarnings("resource")
    14. ServerSocket serverSocket = new ServerSocket(2001);
    15. ExecutorService pool = Executors.newFixedThreadPool(10);
    16. while(true) {
    17. Socket socket = serverSocket.accept();
    18. SocketRunnable socketRunnable = new SocketRunnable(socket);
    19. pool.submit(socketRunnable);
    20. }
    21. }
    22. }

    2、任务执行类

    1. package socket;
    2. import java.io.IOException;
    3. import java.io.InputStream;
    4. import java.io.OutputStream;
    5. import java.net.Socket;
    6. import java.sql.Connection;
    7. import java.sql.PreparedStatement;
    8. import java.sql.SQLException;
    9. /**
    10. * 任务执行类
    11. * @author Administrator
    12. *
    13. */
    14. public class SocketRunnable implements Runnable {
    15. Socket socket;
    16. public SocketRunnable(Socket socket) {
    17. super();
    18. this.socket = socket;
    19. }
    20. @Override
    21. public void run() {
    22. InputStream inputStreamServer;
    23. try {
    24. //InputStream相当于从客户端获得数据,OutputStream相当于向客户端发送数据
    25. inputStreamServer = socket.getInputStream();
    26. byte[] data = new byte[1024];
    27. //将读取的内容存储到字节数组中,返回值是存储的字节数
    28. int length = inputStreamServer.read(data);
    29. String plcStr = new String(data, 0, length);
    30. System.out.println("Client:" + plcStr);
    31. // 添加数据入库的逻辑
    32. try {
    33. Connection connection = JdbcPool.getConnection();
    34. String sql = "insert into plc_source_string(str,createTime) values (?,now())";
    35. PreparedStatement statement = connection.prepareStatement(sql);
    36. statement.setString(1, plcStr);
    37. statement.execute();
    38. statement.close();
    39. // connection.close();
    40. } catch (SQLException e) {
    41. e.printStackTrace();
    42. }
    43. socket.shutdownInput();
    44. OutputStream outputStream = socket.getOutputStream();
    45. outputStream.write("OK".getBytes());
    46. socket.shutdownOutput();
    47. socket.close();
    48. } catch (IOException e) {
    49. e.printStackTrace();
    50. }
    51. }
    52. }

    3、tomcatJdbcPool:

    1. package socket;
    2. import java.sql.Connection;
    3. import java.sql.SQLException;
    4. import org.apache.tomcat.jdbc.pool.DataSource;
    5. /**
    6. * jdbc工具类
    7. * @author Administrator
    8. *
    9. */
    10. public class JdbcPool {
    11. private static volatile DataSource dataSource;
    12. /**
    13. * 懒汉式(双重检查)
    14. * @return
    15. * @throws SQLException
    16. */
    17. public static Connection getConnection() throws SQLException {
    18. if(dataSource == null) {
    19. synchronized (DataSource.class) {
    20. if(dataSource == null) {
    21. dataSource = new DataSource();
    22. dataSource.setUrl("jdbc:mysql://127.0.0.1:3306/plc?characterEncoding=UTF8");
    23. dataSource.setUsername("root");
    24. dataSource.setPassword("111111");
    25. dataSource.setDriverClassName("com.mysql.cj.jdbc.Driver");
    26. }
    27. }
    28. }
    29. return dataSource.getConnection();
    30. }
    31. }

    4、客户端

    1. package socket;
    2. import java.io.InputStream;
    3. import java.io.OutputStream;
    4. import java.net.Socket;
    5. /**
    6. * 客户端请求
    7. * @author Administrator
    8. *
    9. */
    10. public class Client {
    11. public static void main(String[] args) throws Exception {
    12. String ip ="127.0.0.1";
    13. Socket client = new Socket(ip, 2001);
    14. OutputStream outputStream = client.getOutputStream();
    15. outputStream.write("347257538620230731103452|1|1,6,7,2|0|4.0|25|OK".getBytes());
    16. InputStream inputStream = client.getInputStream();
    17. byte[] b = new byte[1024];
    18. int read = inputStream.read(b);
    19. System.out.println(""+new String(b,0,read));
    20. outputStream.flush();
    21. client.shutdownOutput();
    22. client.close();
    23. }
    24. }

    参考文献

    Java — TCP & Socket - 知乎

  • 相关阅读:
    记·CSDN 秋游活动
    FreeRtos-09事件组的使用
    我的知识树
    链家网房源价格信息的爬虫分析工具
    k8s之Service
    开启数据库审计 db,extended级别或os级别)并将审计文件存放到/opt/oracle/audit/下
    php简单商城小程序系统源码
    SpringBoot 同时从数据库和properties文件中读取国际化信息
    web前端-JQuery
    Debian 9 Stretch APT问题
  • 原文地址:https://blog.csdn.net/Bof_jangle/article/details/134078142