import java.io.*;import java.net.*;public class Server { public static void main(String[] args)throws Exception{ boolean f=true; while(f){ try{ ServerSocket server=new ServerSocket(1234); System.out.println("服务器在端口1234监听…………"); Socket socket=server.accept(); System.out.println("开始通信…………"); OutputStream out=socket.getOutputStream(); PrintWriter pw=new PrintWriter(out); BufferedReader reader=new BufferedReader(new InputStreamReader(System.in)); pw.write("服务器:"+reader.readLine()); InputStreamReader in=new InputStreamReader(socket.getInputStream()); BufferedReader br=new BufferedReader(in); String message=br.readLine(); System.out.println(message); if("bye".equals(message)) f=false; pw.close(); br.close(); socket.close(); server.close(); }catch(Exception e){ System.out.println(e); } } }}import java.io.*;import java.net.*;public class Client {public static void main(String[] args){boolean f2=true; while(f2){ try{ Socket socket=new Socket("127.0.0.1",1234); InputStream in=socket.getInputStream(); BufferedReader br=new BufferedReader(new InputStreamReader(in)); String message=br.readLine(); System.out.println(message); OutputStream out=socket.getOutputStream(); PrintWriter pw=new PrintWriter(out); BufferedReader reader=new BufferedReader(new InputStreamReader(System.in)); pw.write("客户端:"+reader.readLine()); //服务器发来的信息 if("bye".equals(message)) f2=false; pw.close(); br.close(); socket.close(); }catch(Exception e){ System.out.println(e); } } }}
package com.v512.TestServer;import java.io.*;import java.net.ServerSocket;import java.net.Socket;import java.net.SocketException;public class TestServer { public static void main(String[] args) { try { ServerSocket s=new ServerSocket(8888); Socket s1=s.accept(); OutputStream os=s1.getOutputStream(); DataOutputStream dos=new DataOutputStream(os); InputStream is=s1.getInputStream(); DataInputStream dis=new DataInputStream(is); InputStreamReader isr=new InputStreamReader(System.in); BufferedReader br=new BufferedReader(isr); String info; while(true){ info=dis.readUTF(); System.out.println("对方说:"+info); if(info.equals("bye")) break; info=br.readLine(); dos.writeUTF(info); if(info.equals("bye")) break; } dos.close(); dis.close(); s1.close(); } catch (SocketException e) { System.out.println("网络连接异常,程序退出!"); } catch (IOException e) { e.printStackTrace(); } }}package com.v512.TestServer;import java.net.Socket;import java.net.SocketException;import java.io.*;public class TestClient {public static void main(String[] args) { try { Socket s1 = new Socket("localhost",8888); InputStream is=s1.getInputStream(); DataInputStream dis=new DataInputStream(is); OutputStream os=s1.getOutputStream(); DataOutputStream dos=new DataOutputStream(os); InputStreamReader isr=new InputStreamReader(System.in); BufferedReader br=new BufferedReader(isr); String info; while(true){ info=br.readLine(); dos.writeUTF(info); if(info.equals("bye")) break; info=dis.readUTF(); System.out.println("对方说:"+info); if(info.equals("bye")) break; } dis.close(); dos.close(); s1.close(); } catch (SocketException e) { System.out.println("网络连接异常,程序退出!"); } catch (IOException e) { e.printStackTrace(); } }}尝试下我改的。。。。。