cantiaozi 2017-03-30 12:22 采纳率: 50%
浏览 768
已结题

java小程序,模拟客户端和服务端通信的,编译通过,运行时没有任何输出,求教

客户端代码:
import java.io.*;
import java.net.*;
public class ClientTalk implements Runnable {
Socket s;
DataOutputStream dos;
DataInputStream dis;
InputStreamReader isr;
BufferedReader br;

public void run() {
try {
String string2 = dis.readUTF();
while(string2.equalsIgnoreCase("exit") != true) {
System.out.println("server : " + string2);
string2 = dis.readUTF();
}
dis.close();
}catch (Exception e) {

  }

}

public static void main(String []args) {
try {
Socket s = new Socket("127.0.0.1",8888);

     DataOutputStream dos = new DataOutputStream(s.getOutputStream());
     DataInputStream dis = new DataInputStream(s.getInputStream());
     InputStreamReader isr = new InputStreamReader(System.in);
     BufferedReader br = new BufferedReader(isr);

     Runnable myRunnable = new ClientTalk();
     Thread thread = new Thread(myRunnable);
     thread.start();
     String string = br.readLine();

     while(string.equalsIgnoreCase("exit") != true) {
        dos.writeUTF(string);
        string = br.readLine();
     }

     dos.close();
  }catch (Exception e) {
     e.printStackTrace();
  }

}
}

服务端代码:
import java.io.*;
import java.net.*;
public class ServerTalk implements Runnable {
ServerSocket ss;
Socket s;
DataOutputStream dos;
DataInputStream dis;
InputStreamReader isr;
BufferedReader br;

public void run() {
try {
String string2 = dis.readUTF();
while(string2.equalsIgnoreCase("exit") != true) {
System.out.println("client : " + string2);
string2 = dis.readUTF();
}
dis.close();
}catch (Exception e) {

  }

}

public static void main(String []args) {
try {
ServerSocket ss = new ServerSocket(8888);
Socket s = ss.accept();
DataOutputStream dos = new DataOutputStream(s.getOutputStream());
DataInputStream dis = new DataInputStream(s.getInputStream());
InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(isr);

     Runnable myRunnable = new ServerTalk();
     Thread thread = new Thread(myRunnable);
     thread.start();
     String string = br.readLine();

     while(string.equalsIgnoreCase("exit") != true) {
        dos.writeUTF(string);
        string = br.readLine();
     }

     dos.close();
  }catch (Exception e) {
     e.printStackTrace();
  }

}
}

编译通过了,分别在两个命令提示符中运行客户端和服务端,在命令行中输入信息后没有任何的输出。请问这是为什么?

  • 写回答

1条回答 默认 最新

  • kingbaron 2017-03-31 02:03
    关注

    String string2 = dis.readUTF();
    很明显可以看出,这一句是会抛出异常的,言尽于此,自己去打断点调试,看看是为什么吧。提示:你并没有正真实例化ClientTalk类实例的一系列IOStream属性。

    评论

报告相同问题?

悬赏问题

  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作