简体   繁体   English

通过Java套接字侦听端口

[英]Listen to port via a Java socket

A server software my client communicates with regularly sends transaction messages on port 4000. I need to print those messages to the console line by line. 我的客户端与之通信的服务器软件定期在端口4000上发送事务消息。我需要逐行将这些消息打印到控制台。 (Eventually I will have to write those values to a table, but I'm saving that for later.) (最终我必须将这些值写入表中,但我将其保存以供以后使用。)

I tried this code but it doesn't output anything: 我试过这段代码,但它没有输出任何东西:

package merchanttransaction;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.lang.ClassNotFoundException;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;

public class MerchantTransaction {
    public static void main(String[] args) {
        try {
            InetAddress host = InetAddress.getLocalHost();
            Socket socket = new Socket("192.168.1.104", 4000);
            ObjectInputStream ois = new ObjectInputStream(socket.getInputStream());
            String message = (String) ois.readObject();
            System.out.println("Message: " + message);

            ois.close();
        } catch (UnknownHostException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }
}

By the way, I need to be able to monitor that port until the program terminates. 顺便说一句,我需要能够监视该端口,直到程序终止。 I'm not sure if the code above will be able to do that because I don't see any iteration to the code. 我不确定上面的代码是否能够这样做,因为我没有看到代码的任何迭代。

I'm using Java version 1.6.0_24, SE Runtime Environment (build 1.6.0_24-b07) running on Ubuntu. 我正在使用在Ubuntu上运行的Java版本1.6.0_24,SE运行时环境(版本1.6.0_24-b07)。

You need to use a ServerSocket . 您需要使用ServerSocket You can find an explanation here . 你可以在这里找到解释。

What do you actually want to achieve? 你真的想要实现什么? What your code does is it tries to connect to a server located at 192.168.1.104:4000 . 您的代码所做的是尝试连接到位于192.168.1.104:4000的服务器。 Is this the address of a server that sends the messages (because this looks like a client-side code)? 这是发送消息的服务器的地址(因为这看起来像客户端代码)? If I run fake server locally: 如果我在本地运行虚假服务器:

$ nc -l 4000

...and change socket address to localhost:4000 , it will work and try to read something from nc -created server. ...并将套接字地址更改为localhost:4000 ,它将工作并尝试从nc创建的服务器读取内容。

What you probably want is to create a ServerSocket and listen on it: 你可能想要的是创建一个ServerSocket并监听它:

ServerSocket serverSocket = new ServerSocket(4000);
Socket socket = serverSocket.accept();

The second line will block until some other piece of software connects to your machine on port 4000. Then you can read from the returned socket. 第二行将阻塞,直到其他一些软件在端口4000上连接到您的机器。然后您可以从返回的套接字读取。 Look at this tutorial, this is actually a very broad topic (threading, protocols...) 看看这个教程,这实际上是一个非常广泛的主题(线程,协议...)

Try this piece of code, rather than ObjectInputStream . 试试这段代码,而不是ObjectInputStream

BufferedReader in = new BufferedReader (new InputStreamReader (socket.getInputStream ()));
while (true)
{
    String cominginText = "";
    try
    {
        cominginText = in.readLine ();
        System.out.println (cominginText);
    }
    catch (IOException e)
    {
        //error ("System: " + "Connection to server lost!");
        System.exit (1);
        break;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM