通过socket接收报警信息,存入数据库

通过ip,端口,连接服务器,得到内容:
public class TcpData {
	
	public static String IP=null;
	public static int PORT=0;
	public static Integer temp=0;
	
	public static void getData(){
		new DataBase().exeSQL2();
		System.out.println("地址为:"+IP+"----端口号为:"+PORT);
		 String hostname = IP;
         int port = PORT;
         String content = "{\"type\":\"identify\",\"istation\":\"-1\"}";
    	 String result="";
         PrintWriter  out = null;
         BufferedReader in = null;

         try {
             Socket socket = new Socket();
             //socket链接指定的主机,超过10秒抛出链接不上异常 
             socket.connect(new InetSocketAddress(hostname,port), 10000);
             // 得到请求的输出流对象  
             out = new PrintWriter(socket.getOutputStream());
             // 发送请求参数
             out.print(content);
             // flush输出流的缓冲
             out.flush();
             // 定义 BufferedReader输入流来读取URL的响应
             /*in = new BufferedReader(new InputStreamReader(socket.getInputStream(),"UTF-8"));
             String line;
             System.out.println("-------------------");
             char[] chars = new char[5];
             System.out.println(in.readLine());
             System.out.println("-------------------");
             while ((line = in.readLine()) != null) {
                 result = line;
                 System.out.println("获取的结果为:"+result);  
                 JsonUtils jsonUtils = new JsonUtils();
              
//                 jsonUtils.parseMap(result);
                 
                 
                 
             }*/
             Reader reader = new InputStreamReader(socket.getInputStream(),"UTF-8");
             char[] chars = new char[1024];
             int len;
             StringBuilder sb = new StringBuilder();
             System.out.println("长度为:"+reader.read(chars));
             while ((len=reader.read(chars)) != -1) {
               sb.append(new String(chars, 0, len));
               System.out.println("获取的结果为:"+sb);
               JsonUtils jsonUtils = new JsonUtils();
               //此为数据解析方法,单独运行该Java文件测试连通性时,可以注释掉该代码
               result = sb.toString();
               jsonUtils.parseMap(result);
               sb.setLength(0);
             }
             temp = 1;
             System.out.println("终止发送,程序持续访问Socket连接");
         }
         catch(Exception ex) {
             System.out.println("发送 POST 请求出现异常!"+ex);
             System.out.println("终止发送,程序持续访问Socket连接");
             temp = 1;
         }
         finally {
             try{
                 if(out!=null){
                     out.close();
                 }
                 if(in!=null){
                     in.close();
                 }
             }
             catch(IOException ex){
                 ex.printStackTrace();
             }
             if (temp==1) {
            	 new TcpData().getData();
             }
         }
	}
	

    public static void main(String[] args) {
    
    	System.out.println("--");
    	TcpData.getData();
    	
	}

}
对接收的内容进行解析:json字符串转Map
    public static Map<String, Object> parseMap(String jsonStr) throws IOException, ParseException {  
        Map<String, Object> map = objectMapper.readValue(jsonStr, Map.class);  
        System.out.pri
  • 1
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
首先,你需要连接到数据库,并创建一个表来存储接收到的字符串。可以使用以下代码: ``` import mysql.connector # 连接到数据库 mydb = mysql.connector.connect( host="localhost", user="yourusername", password="yourpassword", database="yourdatabase" ) # 创建一个表来存储接收到的字符串 mycursor = mydb.cursor() mycursor.execute("CREATE TABLE received_strings (id VARCHAR(255) PRIMARY KEY, string VARCHAR(255))") ``` 接下来,你需要编写代码来将接收到的字符串插入到数据库中。可以使用以下代码: ``` import socket import mysql.connector # 连接到数据库 mydb = mysql.connector.connect( host="localhost", user="yourusername", password="yourpassword", database="yourdatabase" ) # 创建一个socket并监听端口 s = socket.socket() host = socket.gethostname() port = 12345 s.bind((host, port)) s.listen(5) while True: # 接收客户端的连接 c, addr = s.accept() print('Got connection from', addr) # 接收客户端发送的字符串 string = c.recv(1024).decode('utf-8') print('Received string:', string) # 将接收到的字符串插入到数据库中 sql = "INSERT INTO received_strings (id, string) VALUES (%s, %s)" val = (string, string) mycursor = mydb.cursor() mycursor.execute(sql, val) mydb.commit() print(mycursor.rowcount, "record inserted.") # 关闭连接 c.close() ``` 在这个代码中,我们使用了Python的Socket模块来接收客户端发送的字符串,并将其存储到MySQL数据库中。我们为接收到的字符串创建了一个主键,并将其与字符串一起插入到数据库中。最后,我们关闭了与客户端的连接。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值