Java的Socket通信简单实例
服务端
package testlxd; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; import java.net.ServerSocket; import java.net.Socket; import java.text.SimpleDateFormat; import java.util.Date; import java.util.Scanner; public class Server_Test extends Thread { ServerSocket server = null; Socket socket = null; public Server_Test(int port) { try { server = new ServerSocket(port); } catch (IOException e) { e.printStackTrace(); } } @Override public void run() { super.run(); try { System.out.println(getdate() + " 等待客户端连接..."); socket = server.accept(); new sendMessThread().start();// 连接并返回socket后,再启用发送消息线程 System.out.println(getdate() + " 客户端 (" + socket.getInetAddress().getHostAddress() + ") 连接成功..."); InputStream in = socket.getInputStream(); int len = 0; byte[] buf = new byte[1024]; while ((len = in.read(buf)) != -1) { System.out.println(getdate() + " 客户端: (" + socket.getInetAddress().getHostAddress() + ")说:" + new String(buf, 0, len, "UTF-8")); } } catch (IOException e) { e.printStackTrace(); } } public static String getdate() { Date date = new Date(); SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss"); String result = format.format(date); return result; } class sendMessThread extends Thread { @Override public void run() { super.run(); Scanner scanner = null; OutputStream out = null; try { if (socket != null) { scanner = new Scanner(System.in); out = socket.getOutputStream(); String in = ""; do { in = scanner.next(); out.write(("" + in).getBytes("UTF-8")); out.flush();// 清空缓存区的内容 } while (!in.equals("q")); scanner.close(); try { out.close(); } catch (IOException e) { e.printStackTrace(); } } } catch (IOException e) { e.printStackTrace(); } } } // 函数入口 public static void main(String[] args) { Server_Test server = new Server_Test(1234); server.start(); } }
客户端
package testlxd; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.net.Socket; import java.net.UnknownHostException; import java.text.SimpleDateFormat; import java.util.Date; import java.util.Scanner; public class Client extends Thread { //定义一个Socket对象 Socket socket = null; public Client(String host, int port) { try { //需要服务器的IP地址和端口号,才能获得正确的Socket对象 socket = new Socket(host, port); } catch (UnknownHostException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } @Override public void run() { //客户端一连接就可以写数据给服务器了 new sendMessThread().start(); super.run(); try { // 读Sock里面的数据 InputStream s = socket.getInputStream(); byte[] buf = new byte[1024]; int len = 0; while ((len = s.read(buf)) != -1) { System.out.println(getdate() + " 服务器说: "+new String(buf, 0, len,"UTF-8")); } } catch (IOException e) { e.printStackTrace(); } } //往Socket里面写数据,需要新开一个线程 class sendMessThread extends Thread{ @Override public void run() { super.run(); //写操作 Scanner scanner=null; OutputStream os= null; try { scanner=new Scanner(System.in); os= socket.getOutputStream(); String in=""; do { in=scanner.next(); os.write((""+in).getBytes()); os.flush(); } while (!in.equals("bye")); } catch (IOException e) { e.printStackTrace(); } scanner.close(); try { os.close(); } catch (IOException e) { e.printStackTrace(); } } } public static String getdate() { Date date = new Date(); SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss"); String result = format.format(date); return result; } //函数入口 public static void main(String[] args) { //需要服务器的正确的IP地址和端口号 Client clientTest=new Client("127.0.0.1", 1234); clientTest.start(); } }
运行效果:
服务端
客户端
以上就是Socket的简单通信实例,实际开发过程按需修改即可,若遇到乱码问题,可尝试通过以下代码进行修改
in = new BufferedReader(new InputStreamReader(socket.getInputStream(),"UTF-8"));
out = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream(),"UTF-8"));