客户端 - 服务器套接字

时间:2013-10-01 12:28:35

标签: java sockets

我是Java的佼佼者,所以我需要做些什么呢? 我创建了类,我添加了主要方法,我粘贴了这段代码,但这不起作用。 在服务器类中给出了一些关于finally变量的错误。我什么都不懂。

客户等级

    //Creating the client socket:
    Socket socket = new Socket ();

    //Binding to the local socket address:
    InetAddress localIpAddress = InetAddress.getByName ("0.0.0.0");
    int localIpPort = 0;
    SocketAddress localSocketAddress = new InetSocketAddress (localIpAddress, localIpPort);
    socket.bind (localSocketAddress);

    //Connecting to the remote socket address:
    InetAddress remoteIpAddress = InetAddress.getByName ("localhost");
    int remoteIpPort = 20000;
    SocketAddress remoteSocketAddress = new InetSocketAddress (remoteIpAddress, remoteIpPort);
    socket.connect (remoteSocketAddress);

    //Receiving and/or sending data through inbound and outbound streams:
    BufferedReader reader = new BufferedReader (new InputStreamReader (socket.getInputStream ()));
    BufferedWriter writer = new BufferedWriter (new OutputStreamWriter (socket.getOutputStream ()));

    String request = "Hello";
    writer.write (request);
    writer.newLine ();
    // Do not forget to flush
    writer.flush ();

    // Reading the response
    String response = reader.readLine ();

    //Shutting-down the inbound and outbound streams:
    socket.shutdownInput ();
    socket.shutdownOutput ();

    //Closing the socket:
    socket.close ();
    [...]

服务器类

    //Creating the server socket:
    ServerSocket socket = new ServerSocket ();

    //Binding to the local socket address -- this is the one the clients should be connecting to:
    InetAddress localIpAddress = InetAddress.getByName ("0.0.0.0");
    int localIpPort = 20000;
    SocketAddress localSocketAddress = new InetSocketAddress (localIpAddress, localIpPort);
    socket.bind (localSocketAddress);

    while (true) {

            //For each connection accepting a client socket, and:
            Socket client = socket.accept ();

            // Starting a new Thread for each client
            new Thread () {

                    public void run () {
                            try {
                                    //Receiving and/or sending data;
                                    BufferedReader reader = new BufferedReader (new InputStreamReader (client.getInputStream ()));
                                    BufferedWriter writer = new BufferedWriter (new OutputStreamWriter (client.getOutputStream ()));

                                    // Reading the request
                                    String request = reader.readLine ();
                                    // Write the response
                                    String response = "Welcome";
                                    writer.write(response);
                                    writer.newLine();
                                    // Do not forget to flush!
                                    writer.flush();

                                    client.close ();
                            } catch (Exception e) {
                                    e.printStackTrace();
                            }
                    }
            };
    }

    //Closing the server socket;
    socket.close ();

2 个答案:

答案 0 :(得分:1)

Socket client = socket.accept ();更改为final Socket client = socket.accept ();

答案 1 :(得分:1)

您正在使用匿名内部类,并且要从包含范围访问变量(如客户端),必须将它们声明为final。

最终关键字意味着您之后无法重新分配变量,但我认为这不会是一个问题。

说明:

这里...

new Thread () {

                public void run () {
                        try {

您正在动态继承Thread,从runnable接口实现run方法。这是一个匿名类,因为你还没有声明一个可以在其他地方重用的新类 - 它是Thread的一次性子类,并且是匿名的,你只能使用类定义之外的变量(如果它们是final的)。