好久没有写javaeye博客了,最近写了个TCP Socket编程,和大家一起分享!
?
服务端:
package test; import java.io.BufferedReader; import java.io.DataInputStream; import java.io.DataOutputStream; import java.io.IOException; import java.io.InputStreamReader; import java.net.ServerSocket; import java.net.Socket; public class TCPServer2 { /** * @param args * @throws IOException */ public static void main(String[] args) throws IOException { new TCPServer2().go(); } public void go(){ ServerSocket ss; Socket s = null; try { ss = new ServerSocket(8888); s = ss.accept(); } catch (IOException e) { e.printStackTrace(); } SendThread send = new SendThread(s); new Thread(send).start(); RecvThread recv = new RecvThread(s); new Thread(recv).start(); } class SendThread implements Runnable{ private Socket s = null; private DataOutputStream dos = null; public SendThread(Socket s){ this.s = s; try { dos = new DataOutputStream(s.getOutputStream()); } catch (IOException e) { e.printStackTrace(); } } @Override public void run() { String str = ""; while(true){ System.out.println("input message:"); BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); try { str = br.readLine(); dos.writeUTF(str); if("bye".equals(str)) break; } catch (IOException e) { e.printStackTrace(); } } } } class RecvThread implements Runnable{ private Socket s = null; private DataInputStream dis = null; public RecvThread(Socket s){ this.s = s; try { dis = new DataInputStream(s.getInputStream()); } catch (IOException e) { e.printStackTrace(); } } @Override public void run() { String str = ""; while(true){ try { str = dis.readUTF(); if("bye".equals(str)) break; System.out.println("received message:"+str); } catch (IOException e) { e.printStackTrace(); } } } } }
?
客户端:
package test; import java.io.BufferedReader; import java.io.DataInputStream; import java.io.DataOutputStream; import java.io.IOException; import java.io.InputStreamReader; import java.net.Socket; import java.net.UnknownHostException; public class TCPClient2 { public static void main(String[] args) throws UnknownHostException, IOException{ new TCPClient2().go(); } public void go() { try { Socket s = new Socket("127.0.0.1",8888); RecvThread recv = new RecvThread(s); new Thread(recv).start(); SendThread send = new SendThread(s); new Thread(send).start(); } catch (UnknownHostException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } class RecvThread implements Runnable{ private Socket s = null; private DataInputStream dis = null; public RecvThread(Socket s){ this.s = s; try { dis = new DataInputStream(s.getInputStream()); } catch (IOException e) { e.printStackTrace(); } } @Override public void run() { String str = ""; while(true){ try { str = dis.readUTF(); if("bye".equals(str)) break; System.out.println("received message:"+str); } catch (IOException e) { e.printStackTrace(); } } } } class SendThread implements Runnable{ private Socket s; private DataOutputStream dos; public SendThread(Socket s){ this.s = s; try { dos = new DataOutputStream(s.getOutputStream()); } catch (IOException e) { e.printStackTrace(); } } @Override public void run() { String str = ""; while(true){ System.out.println("input message:"); BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); try { str = br.readLine(); dos.writeUTF(str); if("bye".equals(str)) break; } catch (IOException e) { e.printStackTrace(); } } } } }
?