📄 client.java
字号:
package com.yuther.sms.client;import java.io.DataOutputStream;import java.io.InputStream;import java.io.OutputStream;import java.net.Socket;import java.util.Properties;import org.apache.log4j.Logger;public class Client { static Logger log = Logger.getLogger(Client.class); private static int SMSSERVER_PORT; private static String SMSSERVER_HOST; public static synchronized int sendMessage(String nbr, String content) { Socket socket = null; InputStream in = null; OutputStream out = null; Properties p = new Properties(); try { InputStream ins = Client.class.getClassLoader() .getResourceAsStream("conf.properties"); p.load(ins); } catch (Exception e) { log.error("获取配置文件出错", e); } Client.SMSSERVER_PORT = Integer.parseInt(p.getProperty("SMSSERVER_PORT")); Client.SMSSERVER_HOST = p.getProperty("SMSSERVER_HOST"); log.info("短信服务器IP:" + SMSSERVER_HOST); log.info("短信服务器端口:" + SMSSERVER_PORT); try { socket = new Socket(SMSSERVER_HOST, SMSSERVER_PORT); // 用户名 byte[] phoneNum_byte = nbr.getBytes("UTF-8"); // 发送内容 byte[] content_byte = content.getBytes("UTF-8"); int phoneNum_Len = phoneNum_byte.length; int content_Len = content_byte.length; System.out.println("phoneNum_Len:" + phoneNum_Len); System.out.println("content_len:" + content_Len); out = socket.getOutputStream(); DataOutputStream dataOut = new DataOutputStream(out); dataOut.writeInt(phoneNum_Len); dataOut.writeInt(content_Len); dataOut.write(phoneNum_byte); dataOut.write(content_byte); in = socket.getInputStream(); int result = in.read(); return result; } catch (Exception e) { return -1; } finally { try { out.close(); in.close(); socket.close(); } catch (Exception e) { } } } public static void main(String[] args) throws Exception { String nbr = "+8613572090972"; String content = "收到短信!"; int result = sendMessage(nbr, content); System.out.println("发送结果:" + result); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -