问题描述
public class Server{ private ServerSocket ss; private Socket socket; private BufferedReader in; private PrintWriter out; public Server(){ try { ss = new ServerSocket(6000); while (true) { socket = ss.accept(); in = new BufferedReader(new InputStreamReader(socket.getInputStream())); out = new PrintWriter(socket.getOutputStream(),true); String line = in.readLine(); out.println("you input is :" + line); out.close(); in.close(); socket.close(); } //ss.close(); } catch (IOException e) {} } public static void main(String[] args) { new Server(); } }public Client() { try { socket = new Socket("192.168.1.2", 6000); in = new BufferedReader(new InputStreamReader(socket.getInputStream())); out = new PrintWriter(socket.getOutputStream(),true); BufferedReader line = new BufferedReader(new InputStreamReader(System.in)); out.println(line.readLine()); line.close(); out.close(); in.close(); socket.close(); } catch (IOException e) {} } public static void main(String[] args) { new Client(); } } 这个程序为什么从客户端发消息服务器没反应呢?求解,我菜鸟,急求哈~~~
解决方案
public class Server{ private ServerSocket ss; private Socket socket; private BufferedReader in; private PrintWriter out; public Server() { try { ss = new ServerSocket(6000); while (true) { socket = ss.accept(); in = new BufferedReader(new InputStreamReader(socket.getInputStream())); out = new PrintWriter(socket.getOutputStream(),true); String line = in.readLine(); out.println("you input is :" + line); out.close(); in.close(); socket.close(); } // ss.close(); } catch (IOException e) {} } public static void main(String[] args) { new Server(); } } public Client() { try { socket = new Socket("192.168.1.2", 6000); in = new BufferedReader(new InputStreamReader(socket.getInputStream())); out = new PrintWriter(socket.getOutputStream(),true); BufferedReader line = new BufferedReader(new InputStreamReader(System.in)); out.println(line.readLine()); line.close(); out.close(); in.close(); socket.close(); } catch (IOException e) {} } public static void main(String[] args) { new Client(); } }
解决方案二:
楼主是在本机上写的Client 和 Server两个类?如果是在本地机上,你的ip地址应该是127.0.0.1啊,或者是你本地机的ip,可以在控制台下输入ipconfig来查看你当前的ip地址。
解决方案三:
刚刚给你的程序加了行号。地址:192.168.1.2换成127.0.0.1看看行不行!(还有Clinet这个类去哪了?)