在& Out无法解析为类型(Java Socket)

时间:2013-11-16 03:18:39

标签: java sockets networking types

我真的不知道问题是什么。我正在学习Java,我正在关注Oracle的网络文档。我只是为他们的例子做了一个复制和粘贴,并得到了这个错误。有人可以帮助他吗?谢谢:))

以下是Oracle的代码:

import java.net.Socket;
import java.net.ServerSocket;

public class EchoServer {

    public static void main(String[] args) throws Exception {

        // create socket
        int port = 4444;
        ServerSocket serverSocket = new ServerSocket(port);
        System.err.println("Started server on port " + port);

        // repeatedly wait for connections, and process
        while (true) {

            // a "blocking" call which waits until a connection is requested
            Socket clientSocket = serverSocket.accept();
            System.err.println("Accepted connection from client");

            // open up IO streams
            In  in  = new In (clientSocket);
            Out out = new Out(clientSocket);

            // waits for data and reads it in until connection dies
            // readLine() blocks until the server receives a new line from client
            String s;
            while ((s = in.readLine()) != null) {
                out.println(s);
            }

            // close IO streams, then socket
            System.err.println("Closing connection with client");
            out.close();
            in.close();
            clientSocket.close();
        }
    }
}

5 个答案:

答案 0 :(得分:1)

看来你错了。

我第一次尝试就能运行它。

echo: 
hi mom
echo: hi mom
how are you?
echo: how are you?

我拿走了他们所拥有的东西并重新混合了一下以使其更简单(更少的代码,并且它做同样的事情)。您需要JDK 7,但可以在IDE中删除它们。首先在服务器上运行。然后在客户端上运行。然后单击IDE的输出区域并开始在客户端中键入。或者您可以从命令行运行它们。

package com.examples;
import java.net.*;
import java.io.*;

public class EchoServer {
  public static void main(String... args) throws IOException {

    int port;

    if ( args.length != 1 ) {
        System.out.println("listening to port 9999");
        port = 9999;
    } else {
        port = Integer.parseInt ( args[ 0 ] );

    }

    try (
            ServerSocket serverSocket =
                    new ServerSocket(port);
            Socket clientSocket = serverSocket.accept();
            PrintWriter out =
                    new PrintWriter(clientSocket.getOutputStream(), true);
            BufferedReader in = new BufferedReader(
                    new InputStreamReader(clientSocket.getInputStream()));
    ) {
        String inputLine;
        while ((inputLine = in.readLine()) != null) {
            out.println(inputLine);
        }
    }
  }
}

以上使用新的JDK 7尝试使用自动关闭语法。你要这个。

然后是重新混合的客户端。

package com.examples;

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

public class EchoClient {
  public static void main ( String... args ) throws IOException {

    String host;
    int port;

    if ( args.length != 2 ) {
        System.out.println("binding to port localhost:9999");
        host = "localhost";
        port = 9999;
    } else {
        host = args[ 0 ];
        port = Integer.parseInt ( args[ 1 ] );
    }

    try (
            Socket echoSocket = new Socket ( host, port );
            PrintWriter out =
                    new PrintWriter ( echoSocket.getOutputStream (), true );
            BufferedReader in =
                    new BufferedReader (
                            new InputStreamReader ( echoSocket.getInputStream () ) );
            BufferedReader stdIn =
                    new BufferedReader (
                            new InputStreamReader ( System.in ) )
    ) {
        System.out.println("Type in some text please.");
        String userInput;
        while ( ( userInput = stdIn.readLine () ) != null ) {
            out.println ( userInput );
            System.out.println ( "echo: " + in.readLine () );
        }
    }
  }
}

服务器如下:

我们从传递给main的args中获取端口号,或者如果它们没有在命令行上传递,则将它们设置为9999。

    int port;

    if ( args.length != 1 ) {
        System.out.println("listening to port 9999");
        port = 9999;
    } else {
        port = Integer.parseInt ( args[ 0 ] );

    }

然后在try语句中,我们打开服务器套接字条纹gak

            ServerSocket serverSocket =
                    new ServerSocket(port);
            Socket clientSocket = serverSocket.accept();
            PrintWriter out =
                    new PrintWriter(clientSocket.getOutputStream(), true);
            BufferedReader in = new BufferedReader(
                    new InputStreamReader(clientSocket.getInputStream())); 

如果上述内容没有意义,那么您必须了解有关流的更多信息。转到关于输入/输出的教程(读取和写入文件),然后回到这个。

基本上它是将服务器套接字的输出流包装在PrintWriter中并将输入流包装在BufferedReader中。如果您之前没有使用它,那么Java I / O API有点令人生畏,所以我建议稍微开始使用I / O教程。

这是原件:
EchoClient
EchoServer

根据您搞砸的地方,开始阅读IO-Streams,然后IO-Character Streams

然后在几个小时的背景后回到这里。

答案 1 :(得分:0)

In和Out不是定义的类型。因此,你得到错误。将其更改为另一个定义的类型,它应该没问题。

也许你正试图做这样的事情?

PrintWriter out =
                new PrintWriter(clientSocket.getOutputStream(), true);                   
BufferedReader in = new BufferedReader(
                new InputStreamReader(clientSocket.getInputStream()));

答案 2 :(得分:0)

EchoServer.java示例中,我假设InOut应分别为PrintWriterBufferedReader(或其他一些输出和输入流对象) ,因为InOut没有意义,除非你自己编写了对象):

PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true);                  
BufferedReader in = new BufferedReader(new InputStreamReader
                                      (clientSocket.getInputStream()));

如果您还没有,请查看Reading from and Writing to a Socket Tutorial

答案 3 :(得分:0)

您在看什么Oracle doc? (请链接)

Lesson: All About Sockets看起来像你应该阅读的内容。它有以下EchoServer

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

public class EchoServer {
public static void main(String[] args) throws IOException {

    if (args.length != 1) {
        System.err.println("Usage: java EchoServer <port number>");
        System.exit(1);
    }

    int portNumber = Integer.parseInt(args[0]);

    try (
        ServerSocket serverSocket =
            new ServerSocket(Integer.parseInt(args[0]));
        Socket clientSocket = serverSocket.accept();    
        PrintWriter out =
            new PrintWriter(clientSocket.getOutputStream(), true);                  
        BufferedReader in = new BufferedReader(
            new InputStreamReader(clientSocket.getInputStream()));
    ) {
        String inputLine;
        while ((inputLine = in.readLine()) != null) {
            out.println(inputLine);
        }
    } catch (IOException e) {
        System.out.println("Exception caught when trying to listen on port "
            + portNumber + " or listening for a connection");
        System.out.println(e.getMessage());
    }
}
}

答案 4 :(得分:0)

我稍微改进了一下这个例子。 EchoServer在第一次连接后不再终止(这让我烦恼)。我尝试使用System.console(),但它在我的IDE中不起作用,所以我把它装罐了。

请注意使用Scanner(在EchoClient中),它会大大减少代码并使示例更具可读性。

客户端:

        try (
                Socket echoSocket = new Socket ( host, port );
                PrintWriter socketOut =
                        new PrintWriter ( echoSocket.getOutputStream (), true );
                Scanner socketIn =  new Scanner( echoSocket.getInputStream () );
                Scanner console = new Scanner(System.in);
        ) {
            System.out.println("Type in some text please.");
            while ( console.hasNextLine () ) {
                String userInput = console.nextLine ();
                socketOut.println ( userInput );
                System.out.println ( "echo: " + socketIn.nextLine () );
            }
        }

这是一个更好的例子,我认为它也更好地回答了原始问题。

具有while循环的完整服务器:

package com.examples;
import java.net.*;
import java.io.*;

public class EchoServer {
    public static void main(String... args) throws IOException {

        int port;

        if ( args.length != 1 ) {
            System.out.println("listening to port 9999");
            port = 9999;
        } else {
            port = Integer.parseInt ( args[ 0 ] );

        }

        while(true) {
            try (
                    ServerSocket serverSocket =
                            new ServerSocket(port);
                    Socket clientSocket = serverSocket.accept();
                    PrintWriter out =
                            new PrintWriter(clientSocket.getOutputStream(), true);
                    BufferedReader in = new BufferedReader(
                            new InputStreamReader(clientSocket.getInputStream()));
            ) {
                String inputLine;
                while ((inputLine = in.readLine()) != null) {
                    out.println(inputLine);
                }
            }
        }
    }
}

注意主方法如何工作,即使最终用户(开发人员学习套接字)在IDE中运行并且传递args也不方便。当我编写示例时,我会花费大量时间来尽量简化和扩大我所涉及的主题。

再一次是客户端,请注意使用Scanner比使用BufferredReader更短的代码,它更合乎逻辑(如果有行处理它们,则不必检查null)。

完整客户

package com.examples;

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


public class EchoClient {
    public static void main ( String... args ) throws IOException {

        String host;
        int port;

        if ( args.length != 2 ) {
            System.out.println("binding to port localhost:9999");
            host = "localhost";
            port = 9999;
        } else {
            host = args[ 0 ];
            port = Integer.parseInt ( args[ 1 ] );

        }



        try (
                Socket echoSocket = new Socket ( host, port );
                PrintWriter socketOut =
                        new PrintWriter ( echoSocket.getOutputStream (), true );
                Scanner socketIn =  new Scanner( echoSocket.getInputStream () );
                Scanner console = new Scanner(System.in);
        ) {
            System.out.println("Type in some text please.");
            while ( console.hasNextLine () ) {
                String userInput = console.nextLine ();
                socketOut.println ( userInput );
                System.out.println ( "echo: " + socketIn.nextLine () );
            }
        }


    }

}

我还在Client中重命名了变量,因此更清楚他们在做什么。 现在我写了这个,我意识到我可以为EchoServer做同样的事情。

我可能会向Boon添加一组套接字工具。