功能描述:通过socket 监听系统的某一个端口,当我们向系统指定的端口发送不同的指令时,让服务器执行相应的命令
代码如下:
server:
package com.qimh.tcp.socket;import java.net.ServerSocket;import java.net.Socket;public class EchoThreadServer { public static void main(String[] args) { try { //服务器对象 ServerSocket server = new ServerSocket(9999); //客户端对象 Socket client = null; boolean flag = true; while(flag){ System.out.println("服务器端已经启动,等待客户端请求......"); client = server.accept();//这里是阻塞方法 //生成一个响应线程,每个客户端对应一个响应线程 new Thread(new EchoThread(client)).start(); } client.close(); server.close(); System.out.println("服务器已经关闭........."); } catch (Exception e) { // TODO: handle exception e.printStackTrace(); } } public void initSocket(){ try { //服务器对象 ServerSocket server = new ServerSocket(9999); //客户端对象 Socket client = null; boolean flag = true; while(flag){ System.out.println("服务器端已经启动,等待客户端请求......"); client = server.accept();//这里是阻塞方法 //生成一个响应线程,每个客户端对应一个响应线程 new Thread(new EchoThread(client)).start(); } client.close(); server.close(); System.out.println("服务器已经关闭........."); } catch (Exception e) { // TODO: handle exception e.printStackTrace(); } } }
import java.io.BufferedReader;import java.io.InputStreamReader;import java.io.PrintStream;import java.net.Socket;//服务器端响应线程类public class EchoThread implements Runnable { private Socket client;//客户端对象 public EchoThread(Socket client) { // TODO Auto-generated constructor stub this.client = client; } @Override public void run() { // TODO Auto-generated method stub //负责接受客户端传来的字符串,把字符串在回写给客户端。就像回声一样传个对方 try { //负责读取从客户端发送过来的字符串,因为读取的是字符串,所以应该使用字符流,BufferReader BufferedReader buff = new BufferedReader(new InputStreamReader(client.getInputStream())); PrintStream out = new PrintStream(client.getOutputStream()); boolean flag = true; while(flag){ String msg = buff.readLine(); if( msg == null || msg.equalsIgnoreCase("bye")){ flag = false;//结束线程 }else{ //把接收的字符串回写给客户端 out.println("echo:"+msg); } } buff.close(); client.close(); } catch (Exception e) { // TODO: handle exception e.printStackTrace(); } }}
client:
import java.io.BufferedReader;import java.io.InputStreamReader;import java.io.PrintStream;import java.net.Socket;public class EchoThreadClient { public static void main(String[] args) { // TODO Auto-generated method stub try { Socket client = new Socket("localhost", 9999);//创建和服务器的连接 BufferedReader buff;//负责从服务器端 读取回写的字符串 BufferedReader input;//负责读取从控制台输入的字符串 PrintStream output;//负责在屏幕输入回写的字符串 buff = new BufferedReader(new InputStreamReader(client.getInputStream())); input = new BufferedReader(new InputStreamReader(System.in)); output = new PrintStream(client.getOutputStream()); boolean flag = true; while(flag){ System.out.println("请输入信息:"); String msg = input.readLine(); if(msg == null || msg.equalsIgnoreCase("bye")){ flag = false;//结束 }else{ output.println(msg);//输出给服务器 //获得服务器回写的字符串 String echo = buff.readLine(); System.out.println(echo); } } buff.close(); client.close(); } catch (Exception e) { // TODO: handle exception e.printStackTrace(); } }}
联想:此功能可以放入web项目中,
(1)那就需要添加一个监听器,代码如下:
<listener>
<listener-class>com.qimh.spring.struts2.listener.TcpSocketListener</listener-class>
</listener>
启动项目时,在contextInitialized 中启动服务器端,代码如下:
public void contextInitialized(ServletContextEvent arg0) { // TODO Auto-generated method stub System.out.println("监听器初始化 start ..."); //匿名内部类 Thread t2 = new Thread( new Runnable(){ @Override public void run() { EchoThreadServer server = new EchoThreadServer(); server.initSocket(); } } ); t2.start(); System.out.println("监听器初始化 end ..."); }