2013-12-17 57 views
0

我正在尝试构建一个Web服务器,然后我可以通过它获取,发布,放置和删除操作。一些基本和简单的东西。那为什么这不是真的开始呢? ServerSocket和Sockets

所以我决定写,做以下简单的Web服务器:

package aisisplatform.web; 

import aisisplatform.web.interfaces.*; 
import aisisplatform.web.exceptions.*; 
import java.net.*; 
import java.io.*; 

public class WebServer implements WebServerInterface{ 

    private int bindToPort; 
    private Socket socket; 
    private ServerSocket serverSocket; 

    public WebServer(int port){ 
     bind(port); 
    } 

    @Override 
    public void bind(int port) throws WebServerException { 
     if(port == 0){ 
      throw new WebServerException("Cannot bind to port specified"); 
     } 

     bindToPort = port; 
    } 

    @Override 
    public void start() throws WebServerException { 
     try{ 
      System.out.println("Starting Web Server"); 
      serverSocket = new ServerSocket(bindToPort); 
      socket = serverSocket.accept(); 
      System.out.println("localhost:" + bindToPort + " is active."); 
     }catch(IOException e){ 
      throw new WebServerException(e); 
     } 
    } 

    @Override 
    public void stop() throws WebServerException { 
     try{ 
      System.out.println("Stopping Web Server"); 
      serverSocket.close(); 
      socket.close(); 
      System.out.println("Web Server Stopped"); 
     }catch(IOException e){ 
      throw new WebServerException(e); 
     } 
    } 

    @Override 
    public void restart(){ 
     System.out.println("Stopping Web Server"); 
     stop(); 
     System.out.println("Web Server Stopped"); 
     System.out.println("Starting Web Server"); 
     start(); 
     System.out.println("localhost:" + bindToPort + " is active."); 
    } 
} 

,然后将其用于:

package aisisplatform; 

import aisisplatform.web.*; 
import aisisplatform.web.exceptions.*; 

public class AisisPlatform { 

    private static String[] argumentsArray; 

    public static void main(String[] args) { 
     if(args.length > 0){ 
      argumentsArray = args[0].split("="); 
      if(argumentsArray[0].equals("--port")){ 
       WebServer webServer = new WebServer(Integer.parseInt(argumentsArray[1])); 
       switch (args[1]) { 
        case "start": 
         webServer.start(); 
         break; 
        case "stop": 
         webServer.stop(); 
         break; 
        case "restart": 
         webServer.restart(); 
         break; 
        default: 
         throw new WebServerException("We only allow --port=xxxxx start | stop | restart "); 
       } 
      } 
     } 
    } 

} 

然后你可以的参数运行应用程序:--port=xxxxx start | stop | restart的问题很简单。运行应用程序得到线:System.out.println("Starting Web Server");然后它就停止。

我试过调试它,并通过它,它设置端口47000,但它永远不会完成开始...... Netbeans就像“我设置了服务器套接字,现在我完成了,让我挂在这里......“没有任何错误信息抛出。

+0

问:您是否尝试从客户端“连接”到端口47000?如果需要,可以从命令提示符使用Windows'telnet localhost 47000'。 – paulsm4

+0

它*是*开始。它只是在您接受连接之前不会打印任何内容,即该消息位于错误的地方。是什么让你认为你不能使用零作为绑定端口? – EJP

回答

1

运行应用程序获取到:System.out.println(“Starting Web Server”);然后它就停止。

socket = serverSocket.accept(); 

这是一个阻塞调用,并且不会返回到客户端实际连接到您的服务器......

2
socket = serverSocket.accept(); 

该方法阻塞直到建立连接。你应该使用分离的线程。

0

因为它等待连接需要进行。
检查的ServerSocket.accept()方法评论
“ 监听连接到此套接字进行,并接受 它。该方法块,直到建立连接。”

要启动ServerSocket(Listener)在同一个线程(Main)中。所以主线程会一直等到Connection Made

相关问题