serverSocket编程DEMO

目录

0、jar包依赖

1、ServerSocket服务端主程序:

2、任务执行类

3、tomcatJdbcPool:

4、客户端:


0、jar包依赖

serverSocket编程DEMO_第1张图片

1、ServerSocket服务端主程序:

package socket;

import java.net.ServerSocket;
import java.net.Socket;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
/**
 * 服务端,主程序。
 * @author Administrator
 *
 */
public class M {

	public static void main(String[] args) throws Exception {
		
		@SuppressWarnings("resource")
		ServerSocket serverSocket = new ServerSocket(2001);
		ExecutorService pool = Executors.newFixedThreadPool(100);
		while(true) {
			Socket socket = serverSocket.accept();
			SocketRunnable socketRunnable = new SocketRunnable(socket);
			pool.submit(socketRunnable);
		}
		
	}

}

2、任务执行类

package socket;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
/**
 * 任务执行类
 * @author Administrator
 *
 */
public class SocketRunnable implements Runnable {
	
	Socket socket;
	

	public SocketRunnable(Socket socket) {
		super();
		this.socket = socket;
	}


	@Override
	public void run() {
		InputStream inputStreamServer;
		try {
			//InputStream相当于从客户端获得数据,OutputStream相当于向客户端发送数据
			inputStreamServer = socket.getInputStream();
			byte[] data = new byte[1024];
			//将读取的内容存储到字节数组中,返回值是存储的字节数
			int length = inputStreamServer.read(data);
			String plcStr = new String(data, 0, length);
			System.out.println("Client:" + plcStr);
			// 添加数据入库的逻辑
			try {
				Connection connection = JdbcPool.getConnection();
				String sql = "insert into plc_source_string(str,createTime) values (?,now())";
				PreparedStatement statement = connection.prepareStatement(sql);
				statement.setString(1, plcStr);
				statement.execute();
				statement.close();
//				connection.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
			socket.shutdownInput();
			OutputStream outputStream = socket.getOutputStream();
			outputStream.write("OK".getBytes());
			socket.shutdownOutput();
			socket.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
		

	}

}

3、tomcatJdbcPool:

package socket;

import java.sql.Connection;
import java.sql.SQLException;

import org.apache.tomcat.jdbc.pool.DataSource;
/**
 * jdbc工具类
 * @author Administrator
 *
 */
public class JdbcPool {
	
	private static DataSource dataSource = null;
	
	public static Connection getConnection() throws SQLException {
		if(dataSource == null) {
			dataSource = new DataSource();
			dataSource.setUrl("jdbc:mysql://127.0.0.1:3306/plc?characterEncoding=UTF8");
			dataSource.setUsername("root");
			dataSource.setPassword("111111");
			dataSource.setDriverClassName("com.mysql.cj.jdbc.Driver");
		}
		return dataSource.getConnection();
	}
	
}

4、客户端:

package socket;

import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
/**
 * 客户端请求
 * @author Administrator
 *
 */
public class Client {

	public static void main(String[] args) throws Exception {
		String ip ="127.0.0.1";
		Socket client = new Socket(ip, 2001);
		OutputStream outputStream = client.getOutputStream();
		
		outputStream.write("347257538620230731103452|1|1,6,7,2|0|4.0|25|OK".getBytes());
		InputStream inputStream = client.getInputStream();
		byte[] b = new byte[1024];
		int read = inputStream.read(b);
		System.out.println(""+new String(b,0,read));
		outputStream.flush();
		client.shutdownOutput();
		client.close();
	}

}

你可能感兴趣的:(Java相关,#,JavaBase,websocket)