我的套接字程序出了问题

发布于 2024-11-06 10:53:24 字数 1371 浏览 4 评论 0原文

我写了一个与套接字通信的程序。但我不知道为什么它们不起作用。

服务器代码:

this.serverSocket = new ServerSocket(ServerConnector.port);
        this.socketListener = this.serverSocket.accept();
        System.out.println(this.socketListener.getPort());

        this.objIn = new ObjectInputStream(this.socketListener.getInputStream());
        System.out.println("1");

        this.objOut = new ObjectOutputStream(this.socketListener.getOutputStream());
        System.out.println("1");

        this.objOut.writeInt(19999);
        System.out.println("1");

        this.objOut.writeObject(new Date());
        System.out.println("1");

客户端代码:

this.clientSocket = new Socket(ClientConnector.host, ClientConnector.port);

        System.out.println(this.clientSocket.getPort());
        this.objIn = new ObjectInputStream(this.clientSocket.getInputStream());
        System.out.println("1");
        this.objOut = new ObjectOutputStream(this.clientSocket.getOutputStream());
        System.out.println("1");

        int i = (Integer) this.objIn.readInt();
        System.out.println(i);
        Date date = (Date) this.objIn.readObject();

事实是,他们没有显示我建议通过的任何信息(19999和日期),他们甚至无法打印一行“1”(我添加用于测试)。这意味着即使下面的行也无法正常工作。我真的很困惑这些,谁能找出错误?

this.objIn = new ObjectInputStream(this.clientSocket.getInputStream());

I wrote a program communicated with sockets.But I don't know why they don't work.

Server Code:

this.serverSocket = new ServerSocket(ServerConnector.port);
        this.socketListener = this.serverSocket.accept();
        System.out.println(this.socketListener.getPort());

        this.objIn = new ObjectInputStream(this.socketListener.getInputStream());
        System.out.println("1");

        this.objOut = new ObjectOutputStream(this.socketListener.getOutputStream());
        System.out.println("1");

        this.objOut.writeInt(19999);
        System.out.println("1");

        this.objOut.writeObject(new Date());
        System.out.println("1");

Client Code:

this.clientSocket = new Socket(ClientConnector.host, ClientConnector.port);

        System.out.println(this.clientSocket.getPort());
        this.objIn = new ObjectInputStream(this.clientSocket.getInputStream());
        System.out.println("1");
        this.objOut = new ObjectOutputStream(this.clientSocket.getOutputStream());
        System.out.println("1");

        int i = (Integer) this.objIn.readInt();
        System.out.println(i);
        Date date = (Date) this.objIn.readObject();

The truth is, they don't show any information I suggested to pass through(19999 and date), they even can't print a line of "1"(I added for testing). It means even the line below can't work normally. I really confused by these, who can figure the error out?

this.objIn = new ObjectInputStream(this.clientSocket.getInputStream());

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

樱花细雨 2024-11-13 10:53:24

您很可能正在体验Nagle 算法的效果。它试图优化 TCP 中的数据包发送。如果您想立即发送数据,则需要使用 socket接口上的setTcpNoDelay方法。

PS 不知道为什么这个问题被标记为osgi,因为它与 OSGi 完全无关。

You are most likely experiencing the effect of Nagle's Algorithm. which tries to optimize packet sending in TCP. If you want to send your data immediately you need to disable it using the setTcpNoDelay method on the socket interface.

P.S. no idea why the question is tag'ed as osgi, as it has no relevance to OSGi at all.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文