标签:
实现比较简单,基本上就用ServerSocket和Socket就行了,
另外用一个线程进行监听,基本上就没什么问题了
Code:
负责监听的线程:
1 package 网络; 2 3 import java.io.IOException; 4 import java.io.PrintStream; 5 import java.net.*; 6 import java.util.Scanner; 7 8 public class ListenerThread extends Thread { 9 10 private Socket c; 11 private Scanner in; 12 13 public ListenerThread(){ 14 c = null; 15 in = null; 16 } 17 18 public ListenerThread(Socket c){ 19 this.c = c; 20 try { 21 in = new Scanner(c.getInputStream()); 22 } catch (IOException e) { 23 System.out.println("启动失败"); 24 System.exit(0); 25 } 26 } 27 28 @Override 29 public void run() { 30 @SuppressWarnings("unused") 31 PrintStream fs = null; 32 try { 33 fs = new PrintStream(c.getOutputStream()); 34 } catch (IOException e) { 35 e.printStackTrace(); 36 System.exit(0); 37 } 38 while(in.hasNextLine()){ 39 System.out.println(c.getLocalAddress().toString()+":"+in.nextLine()); 40 } 41 } 42 43 public void setSocket(Socket c) { this.c = c; } 44 public Socket getSocket() { return this.c; } 45 46 }
客户端:
1 package 网络; 2 3 import java.net.*; 4 import java.util.*; 5 import java.io.*; 6 7 public class b { 8 9 public ListenerThread lt; 10 public Socket server; 11 public static int port = 8001; 12 public static String ip = "localhost"; 13 14 public b(){ 15 try { 16 server = new Socket(ip, port); 17 } catch (IOException e) { 18 System.out.println("程序启动失败\n"); 19 System.exit(0); 20 } 21 22 @SuppressWarnings("resource") 23 Scanner in = new Scanner(System.in); 24 String s = null; 25 try { 26 27 PrintStream ps = new PrintStream(server.getOutputStream()); 28 lt = new ListenerThread(server); 29 30 lt.start(); 31 32 do{ 33 if( s == null ) continue; 34 ps.println(s); 35 } while(!(s = in.nextLine()).equals("exit")); 36 37 } catch (IOException e) { 38 e.printStackTrace(); 39 } 40 41 try { 42 server.close(); 43 } catch (IOException e) { 44 System.out.println("程序出现异常\n"); 45 } 46 47 } 48 49 public static void main(String[] args) { 50 new b(); 51 } 52 53 }
服务端:
1 package 网络; 2 3 import java.net.*; 4 import java.util.*; 5 import java.io.*; 6 7 public class a { 8 9 ListenerThread lt; 10 ServerSocket server; 11 public static int port = 8001; 12 13 public a(){ 14 try { 15 server = new ServerSocket(port); 16 } catch (IOException e) { 17 System.out.println("程序启动失败\n"); 18 System.exit(0); 19 } 20 21 @SuppressWarnings("resource") 22 Scanner in = new Scanner(System.in); 23 String s = null; 24 do { 25 26 try { 27 Socket c = server.accept(); 28 29 PrintStream ps = new PrintStream(c.getOutputStream()); 30 lt = new ListenerThread(c); 31 32 lt.start(); 33 34 do{ 35 if( s == null ) continue; 36 ps.println(s); 37 } while(!(s = in.nextLine()).equals("exit")); 38 39 } catch (IOException e) { 40 e.printStackTrace(); 41 } 42 43 } while(!(s = in.nextLine()).equals("close")); 44 45 try { 46 server.close(); 47 } catch (IOException e) { 48 System.out.println("程序出现异常\n"); 49 } 50 51 } 52 53 public static void main(String[] args) { 54 new a(); 55 } 56 57 }
另外:
先把服务端打开,在用客户端连接它就可以了(需要在同一台电脑下,如果要跨电脑,就需要改在b.java中的ip)
标签:
原文地址:http://www.cnblogs.com/yyf0309/p/5661521.html