日期:2014-05-20 浏览次数:20908 次
package Ichat; import java.io.*; import java.net.*; import java.util.*; public class Client { private String name; private Socket s; private ReadThread rt; private DataOutputStream dos; public Client(){ try { s= new Socket("192.168.1.105",8889); } catch (Exception e) { e.printStackTrace(); } } public Client(Socket s){ this.s=s; } public void chat(){ System.out.println("在开始聊天之前请输入您的姓名:"); Scanner sc = new Scanner(System.in); this.name = sc.next(); try{ dos= new DataOutputStream(s.getOutputStream()); rt = new ReadThread(s); rt.start(); System.out.println("连接成功,现在您可以开始聊天了:"); while(true){ String msg = sc.next(); if("byebye".equals(msg)){ s.close(); dos.close(); System.out.println("连接已经断开..."); System.exit(0); } dos.writeUTF(name+"说: "+msg); } }catch (Exception e){ e.printStackTrace(); } } public static void main(String[] args) { new Client().chat(); } }
------解决方案--------------------