Java - 从套接字读取?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7549875/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 20:26:46  来源:igfitidea点击:

Java - Read from socket?

javasocketsclient

提问by user207421

SO I just tried to read text from a socket, and I did the following:

所以我只是试图从套接字读取文本,我做了以下事情:

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

public class apples{
    public static void main(String args[]) throws IOException{
        Socket client = null;
        PrintWriter output = null;
        BufferedReader in = null;
        try {
               client = new Socket("127.0.0.1", 2235);
               output = new PrintWriter(client.getOutputStream(), false);
               in = new BufferedReader(new InputStreamReader(client.getInputStream()));

               while (true) {
                       System.out.println("Line: " + client.getOutputStream());
               }
        }
        catch (IOException e) {
            System.out.println(e);
        }
        output.close();
        in.close();
        client.close();
    }
}

This prints out weird numbers and stuff like:

这会打印出奇怪的数字和类似的东西:

java.net.SocketOutputStream@316f673e

I'm not really sure of all the Java functions and things, so how would I make the output print out as text?

我不太确定所有的 Java 函数和东西,那么我如何将输出打印为文本呢?

回答by galchen

look at:

看着:

while (true) {
     System.out.println("Line: " + client.getOutputStream());
}

getOutputSteam() returns an object that represents a stream. you can use this object to send data through the stream. here's an example:

getOutputSteam() 返回一个表示流的对象。您可以使用此对象通过流发送数据。这是一个例子:

BufferedOutputStream out = new BufferedOutputStream(this._socket.getOutputStream());
out.write("hello");
out.flush();

this will send the message "hello" through the socket

这将通过套接字发送消息“hello”

to read the data, you will use the inputstream instead

要读取数据,您将改用输入流

let me just point out - this is a client that you are creating. You also need to create a server. Use java's ServerSocket class for creating a server

让我指出 - 这是您正在创建的客户端。您还需要创建一个服务器。使用 java 的 ServerSocket 类来创建服务器

EDIT: you want to write a client/server application in java. you need to implement 2 processes: a client and a server. the server will listen on some port (using ServerSocket). the client will connect to that port, and send a message.

编辑:您想用 java 编写客户端/服务器应用程序。您需要实现 2 个进程:客户端和服务器。服务器将侦听某个端口(使用 ServerSocket)。客户端将连接到该端口,并发送一条消息。

first object you need to understand is ServerSocket. Server code:

您需要了解的第一个对象是 ServerSocket。服务器代码:

ServerSocket s = new ServerSocket(61616);    // this will open port 61616 for listening
Socket incomingSocket = s.accept();    // this will accept new connections

s.accept method is blocking - it waits for incoming connections, and goes to the next line only after a connection has been accepted. it creates a Socket object. for this socket object you will set up an input stream and output stream (to send/receive data).

s.accept 方法是阻塞的——它等待传入的连接,并且只有在连接被接受后才转到下一行。它创建一个 Socket 对象。对于此套接字对象,您将设置输入流和输出流(以发送/接收数据)。

on the client:

在客户端:

Socket s = new Socket(serverIp, serverPort);

this will open a socket to the server. ip in your case will be "127.0.0.1" or "localhost" (local machine), and port will be 61616.

这将打开一个到服务器的套接字。在您的情况下,ip 将是“127.0.0.1”或“localhost”(本地机器),端口将是 61616。

you will again, set up input/output stream, to send/receive messages

您将再次设置输入/输出流,以发送/接收消息

if you are connecting to a server that already exists, you only need to implement the client of course

如果你连接到一个已经存在的服务器,你当然只需要实现客户端

you can find many examples online

你可以在网上找到很多例子

回答by user207421

You aren't reading anythingwith this code:

您没有使用此代码阅读任何内容

while (true) {
     System.out.println("Line: " + client.getOutputStream());
}

should be:

应该:

String line;
while ((line = in.readLine()) != null) {
  System.out.println("Line: " + line); 
}