http://soft.yesky.com/59/2257059.shtml
import java.io.*;
import java.net.*;
public class MyServer {
    public static void main(String[] args) throws IOException {
        ServerSocket server = new ServerSocket(5678);//服务器端是 ServerSocket
        Socket client = server.accept();
        BufferedReader in = new BufferedReader(new InputStreamReader(client.getInputStream()));
        PrintWriter out = new PrintWriter(client.getOutputStream());
        while (true) {//这个循环应该总是在执行。那out.println("has receive....");输出到哪里去了?为什么一旦//client端发信息过来后,这个输出内容就能马上被client端接收呢,其他情况就被丢掉了。F6跟踪的话,执行完一个循环后就不知道到哪里去了
            String str = in.readLine();//从socket输入中读一行,以回车结尾
            System.out.println(str);
            out.println("has receive....");///out是输出流,将println转向了socket。往socket输出中写一行,以回车结尾?
            out.flush();
            if (str.equals("end"))
                break;
        }
        client.close();
    }
}

解决方案 »

  1.   

    服务器端监听socket端口,通过socket通讯,不知道你想问什么?
      

  2.   

    因为in.readLine在等你客户端输入信息。客户端System.in是阻塞式方法,你不输入信息,服务器就在in.readLine这里傻等着。直到你输入信息,它才执行,获取客户端的输入,接着往下执行,客户端才接收信息"has receive...."。这是为什么不用多线程,只能一人说一句话的原因。
      

  3.   

    先调用 :    * if a security manager exists and its <code>checkListen</code> 
         * method doesn't allow the operation.
         * 
         * @see        java.net.SocketImpl
         * @see        java.net.SocketImplFactory#createSocketImpl()
         * @see        java.net.ServerSocket#setSocketFactory(java.net.SocketImplFactory)
         * @see        SecurityManager#checkListen
         */
        public ServerSocket(int port) throws IOException {
    this(port, 50, null);
        }
        public ServerSocket(int port, int backlog, InetAddress bindAddr) throws IOException {
    setImpl();
    if (port < 0 || port > 0xFFFF)
        throw new IllegalArgumentException(
           "Port value out of range: " + port);
    if (backlog < 1)
      backlog = 50;
    try {
        bind(new InetSocketAddress(bindAddr, port), backlog);  //netSocketAddress(bindAddr, port)
    //获取一些操作系统的信息。
    } catch(SecurityException e) {
        close();
        throw e;
    } catch(IOException e) {
        close();
        throw e;
    }
        }
    在调用:  public Socket accept() throws IOException {
    if (isClosed())
        throw new SocketException("Socket is closed");
    if (!isBound())
        throw new SocketException("Socket is not bound yet");
    Socket s = new Socket((SocketImpl) null);
    implAccept(s);
    return s;
        }
    //接受来连接等等
      

  4.   

    我只是想问问,为什么服务器运行了,但是就是显示不出来out.println("has receive...."); 内容