客户端代码:
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();
}
}
}
编译通过了,分别在两个命令提示符中运行客户端和服务端,在命令行中输入信息后没有任何的输出。请问这是为什么?