Java服务器、客户端程序

标签 java client-server

下面的代码应该允许用户输入 URL 并让它返回该网站的 IP 地址,但它不起作用。

该应用程序是一个控制台应用程序。我曾经可以使用它,但我不知道为什么它现在不起作用。

这是当用户输入网站以获取 IP 地址时出现的错误

IOException:  java.net.SocketException: Connection reset

这是我的客户端代码

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;

public class Client {
    public static void main(String[] args) {

        String hostname = "localhost";
        int port = 6052;

        if (args.length > 0) {
            hostname = args[0];
        }

        Socket clientSocket = null;
        PrintWriter os = null;
        BufferedReader is = null;


        try {
            clientSocket = new Socket(hostname, port);
            os = new PrintWriter(clientSocket.getOutputStream(), true);
            is = new BufferedReader(new    InputStreamReader(clientSocket.getInputStream()));
        } catch (UnknownHostException e) {
            System.err.println("Don't know about host: " + hostname);
        } catch (IOException e) {
            System.err.println("Couldn't get I/O for the connection to: " + hostname);
        }


        if (clientSocket == null || os == null || is == null) {
            System.err.println("Something is  really wrong. ");
            return;
        }

        try {
            if (args.length != 2) {

                System.out.print("Enter a www web address (must have www!) ");
                BufferedReader br = new BufferedReader(new InputSreamReader(Sy.in))
                String keyboardInput = br.readLine();
                os.println(keyboardInput);

            } else {
                os.println(args[1]);
            }
            String responseLine = is.readLine();
            System.out.println("The IP address of " + args[1] + "is" + responseLine);


        } catch (UnknownHostException e) {
            System.err.println("Trying to connect to host: " + e);
        } catch (IOException e) {
            System.err.println("IOException:  " + e);
        }
    }
}

这是我的服务器代码

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;

public class Server {
    public static void main(String args[]) {
        int port = 6052;
        Server server = new Server(port);
        server.startServer();
    }

    ServerSocket echoServer = null;
    Socket clientSocket = null;
    int numConnections = 0;
    int port;

    public Server(int port) {
        this.port = port;
    }

    public void stopServer() {
        System.out.println("Server working hold on a min.");
        System.exit(0);
    }

    public void startServer() {
        try {
            echoServer = new ServerSocket(port);
        } catch (IOException e) {
            e.printStackTrace();
        }

        System.out.println("Server is now started and is waiting for Clients.");


        while (true) {
            try {
                clientSocket = echoServer.accept();
                numConnections++;
                new Thread(new ServerConnection(clientSocket, numConnections,
                        this)).start();
            } catch (IOException e) {
                System.out.println(e);
            }
        }
    }
}




 class ServerConnection implements Runnable {
 private static BufferedReader is;
 private static PrintStream os;
 private static Socket clientSocket;
 private static int id;
 private static Server server;

 public ServerConnection(Socket clientSocket, int id, Server server) {
this.clientSocket = clientSocket;
this.id = id;
this.server = server;
System.out.println( "Connection " + id + " established with: " + clientSocket );
try {
    is = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
    os = new PrintStream(clientSocket.getOutputStream());
} catch (IOException e) {
    System.out.println(e);
}
}




public void run() {
    String line;
try {
    boolean serverStop = false;

            line = is.readLine();
    System.out.println( "Received " + line + " from Connection " + id + "." );

                 InetAddress hostAddress = InetAddress.getByName(line);
                 String IPaddress = hostAddress.getHostAddress();
                 os.println(IPaddress);

          is.close();
        os.close();
     clientSocket.close();
} catch (IOException e) {
    e.printStackTrace();
}
}
}

最佳答案

如果没有参数,主机将为localhost,用户将被支持访问网站。 ArrayOutOfBoundsException 因为您没有检查参数。

只要有一个参数,它就是主机。传递站点将不起作用,因为该站点不会按预期工作。

使用两个参数运行,如果第一个参数是localhost,则它可以工作。

关于Java服务器、客户端程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15575710/

相关文章:

java - RMI 应用程序在从终端运行时抛出错误,但在 Eclipse 中工作正常

c# - Stream.Read 正在组合两个不同的读取

java - hibernate 查询语法异常

java - 使用 JAVA 和 Selenium 的 REST ASSURED 计算数组的数量并访问 JSON 响应中的特定数组

java - 我的 onUpgrade() 方法应该是什么样子?

java - 如果 PayPal 的 IPN 服务无法连接到 IPN 目的地会怎样?

java - 为什么我的变量在这个方法中全部消失了?

c++ - 成功进行 SSL 通信的要求

c - 伯克利套接字,运行简单的客户端和服务器连接

java - 如何处理程序状态逻辑?