客户端:
package com.wulala;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.net.UnknownHostException;
public class Client {
public static void main(String[] args) {
Client client = new Client();
client.run();
}
public void run() {
Socket client;
try {
client = new Socket("127.0.0.1", 9999);
// 2.打开socket的流信息,准备下面的操作
OutputStream os = client.getOutputStream();
// 3.写信息
os.write(("xxxxasdf12233").getBytes());
}
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
括号可能不完整, 不重要.
客户端没啥可说的, 就是打开一个目标服务器一个IP, 跟端口, 然后outputsteam一些东西进去, 模拟回头STM32通过GPRS连接服务器后通过串口透传发送数据.
服务器端:
package com.wulala;
import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;
public class MultiSocketServer {
public static void main(String args[]) {
MultiSocketServer multiSocketServer = new MultiSocketServer();
try {
multiSocketServer.startServer();
} catch (Exception ex) {
System.out.println(ex.getMessage());
}
}
public void startServer() throws IOException {
ServerSocket ss = null;
try {
ss = new ServerSocket(9999);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
while (true) {
// 1.打开socket连接
// 等待客户端的请求
final Socket server = ss.accept();
System.out.println("服务-----------请求开始start");
// 2.打开socket的流信息,准备下面的操作
final InputStream is = server.getInputStream();
byte b[] = new byte[1024];
int readCount = is.read(b);
String str = new String(b);
str = str.trim();
final String serverFileName = str;
if (readCount > 0) {
new Thread() {
@Override
public void run() {
System.out.println("线程" + Thread.currentThread().getName() + "启动");
// 3.对流信息进行读写操作
System.out.println("客户端传过来的信息是:" + serverFileName);
ExecuteDML edml = new ExecuteDML();
try {
edml.initParam("dbconfig.properties");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
int insertResult = 0;
String deviceID = "";
int activate = 0;
if (serverFileName.length() > 9) {
deviceID = serverFileName.substring(0, 8);
activate = Integer.valueOf(serverFileName.substring(8));
}
try {
//insertResult = edml.insertWXData("aaaa", 113);
insertResult = edml.insertWXData(deviceID, activate);
System.out.println("Total "+insertResult+" result been changed");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
is.close();
server.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return;
}
}.start();
}
System.out.println("服务-----------请求结束over");
}
}
}
简单来说, 一有数据过来, 就起一个线程, 在这个项目里面, 通常发一个字符串过来就完事, 判断字符串的前8位为设备名, 后面就是激活数.或者别的什么.
以后要进行接收数据的校验, 分析什么的, 迟点重构吧.
还有一个写数据库的类:
package com.wulala;
import java.io.FileInputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;
import java.util.Properties;
import java.util.UUID;
public class ExecuteDML {
private String driver;
private String url;
private String user;
private String password;
public void initParam(String paramFile) throws Exception {
Properties props = new Properties();
props.load(new FileInputStream(paramFile));
driver = props.getProperty("driverClassName");
url = props.getProperty("url");
user = props.getProperty("user");
password = props.getProperty("password");
}
public int insertData(String data) throws Exception {
Class.forName(driver);
try (Connection conn = DriverManager.getConnection(url, user, password); Statement stmt = conn.createStatement()) {
return stmt.executeUpdate("INSERT INTO MEMS_REC (ID,X,Y, CREATE_TIME) VALUES ('" + UUID.randomUUID().toString() + "','1','" + data
+ "', now());");
}
}
public int insertWXData(String deviceID,int activate) throws Exception {
Class.forName(driver);
try (Connection conn = DriverManager.getConnection(url, user, password); Statement stmt = conn.createStatement()) {
return stmt.executeUpdate("INSERT INTO WANG_XIN (ID,DEVICE_ID,ACTIVATE) VALUES ('" + UUID.randomUUID().toString() + "','"+deviceID+"'," + activate
+ ");");
}
}
// public static void main(String[] args) throws Exception {
//
// ExecuteDML edml = new ExecuteDML();
// edml.initParam("dbconfig.properties");
// int result = edml.insertData("3");
// System.out.println("Total " + result + " record been add");
// }
}