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());
        }
    }
}

ソケットは正常に作成されていますが、制御が t.start() メソッド呼び出しに移動したときに、ThreadedEchoHandler クラスの run() メソッドが呼び出されていません。

なぜこうなった?何か案が?

4

2 に答える 2

0

クライアントがサーバーに接続していないため、 acept ステートメントは永久にブロックされていると思います。証明または反証するために、プリントで accept() をラップできます。

于 2012-04-06T11:10:03.097 に答える