【问题标题】:running client and server class on same machine but no connection?在同一台机器上运行客户端和服务器类但没有连接?
【发布时间】:2015-09-14 17:42:13
【问题描述】:

我在笔记本电脑上使用教科书客户端和服务器类,但是当我运行这两个程序时,它们似乎没有连接。我只是打开两个命令提示符窗口,在一个窗口上编译并运行服务器,然后在另一个窗口上编译客户端。我做错了什么?

这是服务器代码

import java.util.Date;
import java.net.ServerSocket;
import java.net.Socket;
import java.io.DataOutputStream;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;

public class DateServer {
    public static void main(String[] args) {
        Date now = new Date();
        try {
            System.out.println("Waiting for a connection on port 7655.");
            ServerSocket serverSock = new ServerSocket(7655);
            Socket connectionSock = serverSock.accept();
            BufferedReader clientInput = new BufferedReader(
                    new InputStreamReader(connectionSock.getInputStream()));
            DataOutputStream clientOutput = new DataOutputStream(
                    connectionSock.getOutputStream());
            System.out.println("Connection made, waiting for client " +
                    "to send their name.");
            String clientText = clientInput.readLine();
            String replyText = "Welcome, " + clientText +
                    ", Today is " + now.toString() + "\n";
            clientOutput.writeBytes(replyText);
            System.out.println("Sent: " + replyText);
            clientOutput.close();
            clientInput.close();
            connectionSock.close();
            serverSock.close();
        } catch (IOException e) {
            System.out.println(e.getMessage());
        }
    }
}

这是客户端代码:

import java.net.Socket;
import java.io.DataOutputStream;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;

public class DateClient {
    public static void main(String[] args) {
        try {
            String hostname = "localhost";
            int port = 7655;
            System.out.println("Connecting to server on port " + port);
            Socket connectionSock = new Socket(hostname, port);
            BufferedReader serverInput = new BufferedReader(
                    new InputStreamReader(connectionSock.getInputStream()));
            DataOutputStream serverOutput = new DataOutputStream(
                    connectionSock.getOutputStream());
            System.out.println("Connection made, sending name.");
            serverOutput.writeBytes("Dusty Rhodes\n");
            System.out.println("Waiting for reply.");
            String serverData = serverInput.readLine();
            System.out.println("Received: " + serverData);
            serverOutput.close();
            serverInput.close();
            connectionSock.close();
        } catch (IOException e) {
            System.out.println(e.getMessage());
        }
    }
}

谢谢

【问题讨论】:

  • 在 netbeans 和 cmd 上都能正常运行
  • @swapnil7 你使用了两个不同的 cmd 屏幕吗?
  • 是的,我使用了两个不同的 cmd 屏幕
  • 您不会刷新输出,即您无法确保数据实际发送到服务器。您依赖于数据将直接写入服务器或底层缓冲区将以某种方式刷新的事实 - 在您的系统中似乎并非如此。您应该检查的另一件事是是否存在阻止连接的防火墙规则。
  • @swapnil7 我尝试更改端口...但仍然没有连接。他们似乎永远在奔跑。有什么想法吗?

标签: java sockets


【解决方案1】:

尝试更改端口号看看。可能是其他应用程序正在此端口上运行。您的代码没有问题。我可以运行它。

【讨论】:

  • 我尝试更改端口...但仍然没有连接。他们似乎永远在奔跑。有什么想法吗?
  • 你有什么异常吗?你应该运行客户端程序,不仅要编译。
  • @user2910237 没有例外。只是没有回应。 swapnil7 认为它是我的防火墙。
  • 当我第一次尝试运行您的程序时,防火墙被阻止并要求访问。
猜你喜欢
  • 1970-01-01
  • 2011-11-20
  • 1970-01-01
  • 2023-03-18
  • 1970-01-01
  • 1970-01-01
  • 2021-07-31
  • 2013-05-18
  • 2017-06-01
相关资源
最近更新 更多