黑马程序员技术交流社区

标题: 通信的问题 [打印本页]

作者: 明光照    时间: 2012-9-25 20:03
标题: 通信的问题
这是我写的网络的代码。我不明白为什么彼此写的内容不能在对边显示
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();
}
}还请各位大神帮忙啊


作者: 覃宏海    时间: 2012-9-25 20:06
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);----------------------------------------------------问题出在这里了,你还没有输入内容,就已经把内容输出了,应该吧它放到while循环后
   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();
}
}
作者: 尤圣回    时间: 2012-9-25 20:07
你先打开TCPServer1 在打开TCPClient1
作者: 明光照    时间: 2012-9-25 20:10
覃宏海 发表于 2012-9-25 20:06
import java.io.*;
import java.net.*;
public class TCPServer1 {

这是一个循环等待的代码。放在while后面没意义啊
作者: 明光照    时间: 2012-9-25 20:11
尤圣回 发表于 2012-9-25 20:07
你先打开TCPServer1 在打开TCPClient1

我是这样打开的啊




欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2