socket 通讯
客户端:
package com.meiah.socket; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintStream; import java.net.Socket; import java.net.SocketAddress; import java.net.UnknownHostException; /** * socket客户端 * @author zhangsp * */ public class SocketClient { private Socket client = null; private BufferedReader buf = null; private PrintStream out = null; public SocketClient() { } //调用时传回四个参数,分别表示服务器IP地址,第一个运算数,运算符,第二个运算数 public Double socketCalculate(String ipAddr,double first, char operator, double second) { try { client = new Socket(ipAddr, 8888); buf = new BufferedReader(new InputStreamReader(client .getInputStream())); out = new PrintStream(client.getOutputStream()); out.println(first); out.println(operator); out.println(second); String str = buf.readLine(); out.flush(); buf.close(); out.close(); client.close(); client = null; return Double.parseDouble(str); } catch (UnknownHostException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } return null; } public boolean isConnected() { if (client.isConnected()) return true; else return false; } public void connect() { SocketAddress addr = client.getRemoteSocketAddress(); try { client.connect(addr); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public static void main(String[] args) { SocketClient socketClient = new SocketClient(); System.out.println(socketClient.socketCalculate("192.168.40.98",1, '+', 25)); } }
服务端代码:
package com.meiah.socket; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintStream; import java.net.ServerSocket; import java.net.Socket; /** * socketServer * @author zhangsp * */ public class SocketServer extends Thread{ public ServerSocket server = null; public Socket client = null; public PrintStream out = null; public BufferedReader buf = null; public SocketServer() { try { server = new ServerSocket(8888); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } @Override public void run() {//多线程处理计算部分,提供给多个用户同时使用这一个服务 // TODO Auto-generated method stub System.out.println("socket服务启动......"); while (true) { try { client = server.accept(); // String str = "Hello Socket!"; buf = new BufferedReader(new InputStreamReader(client .getInputStream())); double a = Double.parseDouble(buf.readLine().trim()); char operator = buf.readLine().trim().charAt(0); double b = Double.parseDouble(buf.readLine().trim()); double result = 0; switch (operator) { case '+': result = a + b; break; case '-': result = a - b; break; case '*': result = a * b; break; case '÷': result = a / b; break; case '%': result = a % b; break; case '^': result = Math.pow(a, b); break; default: result = a + b; break; } out = new PrintStream(client.getOutputStream()); out.println(result); out.flush(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } public void closeServer() { out.close(); try { client.close(); server.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public static void main(String[] args) { // TODO Auto-generated method stub SocketServer server = new SocketServer(); while(true){ server.run(); } } }