2013-03-22 180 views
1

下面的代码应该允许用户输入一个URL,它也返回该网站的IP地址,但它不工作。Java服务器,客户端程序

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

这里是我得到的错误,当用户进入一个网站,从

IOException: java.net.SocketException: Connection reset 

在这里得到的IP地址是我的客户端代码

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(); 
} 
} 
} 
+1

“它不工作”,请提供一些具体信息,如错误,你得到或者任何可以帮助回答你的问题 – Abubakkar 2013-03-22 16:51:07

+1

......还有,我不介意的编辑,但有点拼写检查和适当的代码格式化会帮助 – 2013-03-22 16:52:03

+0

我添加了什么错误即时得到 – 2013-03-22 16:57:12

回答

1

不带任何参数,主机会localhost,用户将被propted一个网伊特。 ArrayOutOfBoundsException,因为你没有检查参数。

有一个参数,它是主机。传递网站将无法正常工作,因为该网站无法按预期工作。

两个参数运行,它的工作原理,如果第一个参数是localhost

+0

那么我将如何解决这个代码。 – 2013-03-22 17:37:21

+0

仅在程序开始时检查参数。将第一个参数设为主机名,将第二个参数设为网站。如果其中一个或两个都不存在,请妥善处理。 – 2013-03-22 17:39:52

+0

行,所以我跑我的代码在Eclipse中,这是这种情况发生,首先每一件事情“服务器现在已经开始并正在等待的客户。” sencond输入WWW网址(必须有WWW!),那么用户输入www.google.com,然后我得到这个错误IOException:java.net.SocketException异常:连接复位 – 2013-03-22 17:41:18