这是我写的网络的代码。我不明白为什么彼此写的内容不能在对边显示
import java.io.*;
import java.net.*;
public class TCPServer1 {
public static void main(String args[]) throws Exception {
ServerSocket server = new ServerSocket(8888);
Socket socket = null;
while (true) {
socket = server.accept();
InputStream is = socket.getInputStream();
OutputStream os = socket.getOutputStream();
// BufferedReader br = new BufferedReader(new
// InputStreamReader(is));
DataInputStream dis = new DataInputStream(is);
BufferedReader sin = new BufferedReader(new InputStreamReader(
System.in));//从键盘上输入数据
DataOutputStream dos = new DataOutputStream(os);
String line = "hello world";
String readLine = "sorry Client";
System.out.println("Server:" + dis.readUTF());
readLine = sin.readLine();
while (!readLine.equals("bye")) {
dos.writeUTF(readLine);
dos.flush();
System.out.println("Server" + readLine);
System.out.println("Client" + dis.readUTF());
readLine = sin.readLine();
}
}
}
}
import java.io.*;
import java.net.*;
public class TCPClient1 {
public static void main(String args[]) throws Exception {
Socket socket = new Socket("127.0.0.1", 8888);
InputStream is = socket.getInputStream();
OutputStream os = socket.getOutputStream();
BufferedReader sin = new BufferedReader(
new InputStreamReader(System.in));// 从键盘上输入数据
PrintWriter pw = new PrintWriter(os);
BufferedReader br = new BufferedReader(new InputStreamReader(is));
String readLine = sin.readLine();
while (!readLine.equals("bye")) {
pw.println(readLine);
pw.flush();
System.out.println("Server:" + br.readLine());
readLine = sin.readLine();
}
pw.close();
br.close();
socket.close();
}
}还请各位大神帮忙啊
|