客户端与服务器的通讯
基于TCP协议的Socket
客户端发送消息:
protected void connectServerWithTCPSocket() throws Exception{ Socket socket; // 创建一个Socket对象,并指定服务端的IP及端口号 /** * 服务器IP:123.57.56.94 * 服务器post:22;3389 */ socket = new Socket("123.57.56.94", 3389); // 创建一个InputStream用户读取要发送的文件。 InputStream inputStream = new FileInputStream("F://a.txt");//测试文件 // // 获取Socket的OutputStream对象用于发送数据。 OutputStream outputStream = socket.getOutputStream(); // 创建一个byte类型的buffer字节数组,用于存放读取的本地文件 byte buffer[] = new byte[4 * 1024]; int temp = 0; // 循环读取文件 while ((temp = inputStream.read(buffer)) != -1) { // 把数据写入到OuputStream对象中 outputStream.write(buffer, 0, temp); } // 发送读取的数据到服务端 outputStream.flush();
持续监听服务器端口(java)
- 编写服务器监听类
package Listener; import javax.servlet.ServletContextEvent; import javax.servlet.ServletContextListener; import javax.servlet.http.HttpServlet; public class videoandGPS extends HttpServlet implements ServletContextListener { public void contextDestroyed(ServletContextEvent arg0) { } public void contextInitialized(ServletContextEvent arg0) { System.out.println("-----------------video and GPS thread--------------------------"); //gps守护线程 System.out.println("-----------------GPS监听已启动--------------------------"); StartThread s = new StartThread(); s.setDaemon(true);// 设置线程为后台线程,tomcat不会被hold,启动后依然一直监听。 s.start(); } }
- 守护主线程
package Listener; import java.io.IOException; import java.net.ServerSocket; import java.net.Socket; public class StartThread extends Thread { public void run() { int GPSport = 9999; System.out.println("-----------------GPSport:" + GPSport + "---------------------------"); try { ServerSocket serverSocket = new ServerSocket(GPSport); // 轮流等待请求 while (true) { // 等待客户端请求,无请求则闲置;有请求到来时,返回一个对该请求的socket连接 Socket clientSocket = serverSocket.accept(); // 创建zithread对象,通过socket连接通信 Thread t = new Thread(new ChildThread(clientSocket)); t.start(); } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
- 守护子线程
package Listener; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStream; import java.io.PrintWriter; import java.net.Socket; public class ChildThread implements Runnable { private BufferedReader reader; private Socket socket; public ChildThread(Socket clientSocket) { try { // 得到socket连接 socket = clientSocket; // 得到客户端发来的消息 InputStreamReader isReader = new InputStreamReader(socket.getInputStream()); reader = new BufferedReader(isReader); //发送指令 OutputStream os = socket.getOutputStream(); PrintWriter pw = new PrintWriter(os); String info = "[3G*4700546714*0005*VERNO]"; pw.write(info); pw.flush(); //socket.shutdownOutput(); } catch (IOException e) { e.printStackTrace(); } } public void run() { String message; try { while ((message = reader.readLine()) != null) { System.out.println("客户端消息: " + message); } } catch (IOException e) { e.printStackTrace(); } } }
web.xml配置监听
<listener> <listener-class>videoandGPS.videoandGPS</listener-class> </listener>
客户端无法完成发送问题
体现在初始化Socket时出现异常,客户机不能实现与服务器端口的连接。