下面是Server和Client的程序,想要实现的功能是打开两个cmd窗口分别运行两个程序以后,Client端和Server端的对话。
程序如下:
Server端:
- Java code
import java.net.*;import java.io.*;public class TalkServer{ public static void main(String[] args) { try { ServerSocket ss = null; try { ss = new ServerSocket(8889); } catch(Exception e) { System.out.println("Cannot listen to: " + e); } String line; Socket s = null; try { s = ss.accept(); } catch(IOException ie) { System.out.println("IOError: " + ie); } BufferedReader br = new BufferedReader(new InputStreamReader(s.getInputStream())); PrintWriter pw = new PrintWriter(s.getOutputStream()); BufferedReader typeIn = new BufferedReader(new InputStreamReader(System.in)); //System.out.println("Client: " + br.readLine()); line = typeIn.readLine(); while(!line.equals("exit")) { pw.println(line); pw.flush(); System.out.println("Server: " + line); System.out.println("Client: " + br.readLine()); line = typeIn.readLine(); } br.close(); pw.close(); s.close(); ss.close(); } catch(Exception e) { System.out.println("Error: " + e); } }}
Client端:
- Java code
import java.net.*;import java.io.*;public class TalkClient{ public static void main(String[] args) { try { Socket s = null; try { s = new Socket("127.0.0.1", 8889); } catch(IOException ie) { System.out.println("Error: " + ie); } BufferedReader br = new BufferedReader(new InputStreamReader(s.getInputStream())); PrintWriter pw = new PrintWriter(s.getOutputStream()); BufferedReader typeIn = new BufferedReader(new InputStreamReader(System.in)); //System.out.println("Server: " + br.readLine()); String line = typeIn.readLine(); while(!line.equals("exit") ) { pw.println(line); pw.flush(); System.out.println("Client: " + line); System.out.println("Server: " + br.readLine()); line = typeIn.readLine(); } br.close(); pw.close(); s.close(); } catch(Exception e) { System.out.println("Error: " + e); } }}
打开以后输入任何字符都不能显示。
------解决方案--------------------
- Java code
//刚那个是Server的,这个是Client。毕竟是刚学时写的,LZ将就对比下看问题在哪。import java.net.*;import java.util.Scanner;import java.io.*;public class Client { public static void main(String[] args) { try { Socket socket = new Socket("localhost",1234); Thread t = new Thread(new clientWriter(socket)); t.start(); Thread t1 = new Thread(new clientReader(socket)); t1.start(); }catch(Exception e) { e.printStackTrace(); } }}class clientWriter implements Runnable { private Socket socket; public clientWriter(Socket socket) { this.socket = socket; } public void run() { Scanner in = new Scanner(System.in); try { DataOutputStream output = new DataOutputStream(socket.getOutputStream()); while(true) { String info = in.nextLine(); output.writeUTF(info); if(info.equalsIgnoreCase("bye")) { System.out.println("自己下线,退出程序"); System.exit(0); } } }catch(Exception e) { e.printStackTrace(); } }}class clientReader implements Runnable { private Socket socket; public clientReader(Socket socket) { this.socket = socket; } public void run() { try { DataInputStream input = new DataInputStream(socket.getInputStream()); while(true) { String info = input.readUTF(); System.out.println("对方说:" + info); if(info.equalsIgnoreCase("bye")) { System.out.println("对方下线了,结束通话…"); System.exit(0); } } }catch(Exception e) { e.printStackTrace(); } }}