2

这是服务器代码

package echoserver;

import java.net.*;
import java.io.*;

public class EchoServer {

    public static void main(String[] args) {
        try {

            //establish server socket
            ServerSocket s = new ServerSocket(1981);

            //Thread client connectionsincoming
            while (true) {
                //wait for incoming connection
                Socket incoming = s.accept();
                Runnable r = new ThreadedEchoHandler(incoming);
                Thread t = new Thread(r);
                t.start();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}


package echoserver;

import java.net.*;
import java.util.*;
import java.io.*;

class ThreadedEchoHandler implements Runnable {

    public ThreadedEchoHandler(Socket i) {
        //initializing socket
        incoming = i;
    }

    public void run() {
        try {
            try {
                //recieve input stream from socket
                InputStream inStream = incoming.getInputStream();

                //recieve output stream from socket
                OutputStream outStream = incoming.getOutputStream();

                //Create a scanner from input stream
                Scanner scan = new Scanner(inStream);

                //Create printer writer from output stream and enabled auto flushing
                PrintWriter out = new PrintWriter(outStream, true);

                //prompt users on how to exit program soon as a long in into the server
                out.println("Enter BYE to exit");

                boolean done = false;

                //while done is not true and scanner has next line loop
                while (!done && scan.hasNextLine()) {

                    //reading text that came in from the socket
                    String line = scan.nextLine();

                    //On the server print the ip address of where the text is coming from and the text they typed
                    System.out.println("Recieved from " + incoming.getInetAddress().getHostAddress() + ": " + line);

                    //Echo back the text the client typed to the client
                    out.println("Echo: " + line);

                    //if they type BYE in caps terminate there connection and I also trimmed whitespaces
                    if (line.trim().equals("BYE")) {
                        done = true;
                    }
                }
            } //finally close the socket connection
            finally {
                incoming.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    private Socket incoming;
}

这是客户端的代码

package client;

import java.net.*;
import java.io.*;

public class Client {

    public static void main(String[] args) throws IOException {
        PrintWriter out = null;
        try {
            Socket s = new Socket(InetAddress.getLocalHost(), 1981);
            System.out.println("Connected to server on port 1981");
            out = new PrintWriter(s.getOutputStream());

            out.println("Hello");
            s.close();

        } catch (Exception ex) {
            System.err.println(ex.getMessage());
        }
    }
}

Socktes 已成功创建,但是当控制转到 t.start() 方法调用时,它没有调用 ThreadedEchoHandler 类的 run() 方法。

为什么会这样?任何的想法?

4

2 回答 2

2

客户端写入"Hello". PrintWriter到现在为止还挺好。

您可能希望PrintWriter将文本直接发送到套接字,但事实并非如此。构造函数的文档PrintWriter(OutputStream)说它创建了一个PrintWriter 没有自动行刷新。这意味着您必须out.flush()在想要实际发送某些内容时打电话。

直到你调用out.flush()的文本只存在于一些内部缓冲区中,而服务器将无法看到它。

于 2012-04-06T11:31:44.210 回答
0

我的猜测是,由于没有客户端连接到服务器,因此 acept 语句将永远阻塞。您可以将 accept() 包装在打印件中以证明或反驳。

于 2012-04-06T11:10:03.097 回答