Web服务器接受套接字作为参数 [英] web server accepting socket as argument

查看:81
本文介绍了Web服务器接受套接字作为参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的教授提供的示例服务器.我要编写一个简单的Web浏览器以接受套接字并显示接收到的数据.获取异常打开套接字.使用套接字8080.任何想法都将非常有帮助.

this is an example server given by my professor. i am to write a simple web browser to accept a socket and display data received. getting exception opening socket. using socket 8080. any ideas would be very helpful.

public class server {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
    // read arguments
	int portNum = java.lang.Integer.parseInt(args[0]); // the second argument is
                                                // a port number.
     
     // The server just opens a ServerSocket using the specified port number,
     // and then goes into an infinite loop.
   
     // try connecting to the server
     ServerSocket s = null;
     try {
         s = new ServerSocket(portNum);
     } catch (Exception ex0 ) {
       System.out.println("Exception opening socket");
       System.exit(1); // exit with error.
       
     }
     
     while(true){
       // the server waits (using an accept call) for a client to connect.
       Socket clientConnection = null;
       try {
          clientConnection = s.accept();
       } catch (Exception ex1) {
          System.out.println("Exception with accept");
          System.exit(1); // exit with error.
       }
       System.out.println("Client connected to Server");

       // the socket provides input and output streams we can treat 
       // like any other input and output stream.
       DataInputStream netInput = null; 
       DataOutputStream netOutput = null; 
       try {
          netInput = new DataInputStream(clientConnection.getInputStream());
          netOutput = new DataOutputStream(clientConnection.getOutputStream());
       } catch (java.io.IOException ex1) {
         System.out.println(" Unable to create input or output stream for network connection.");
         System.exit(1); // exit with error.
       
       }

        int input; // input from network
       try {
          do {
             input = netInput.read();
              netOutput.write(input);
              System.out.print((char)input);
          } while(input!=-1);
       } catch (Exception ex3) {
         System.out.println("IO Exception" );
       }
       System.out.println("Client Disconnect"); 
     }
    } // end of main.
}

推荐答案

好的,谢谢.有没有人有什么建议?我不必担心浏览器的客户端.我只需要实现get函数.该程序必须接受端口号,并且能够显示Web数据,例如文本,图像数据和目录信息.我真的不知道从哪里开始,尤其是因为我无法打开任何端口.也许我没有输入正确的端口号.有谁知道如何找出计算机使用的哪个端口号对诸如Firefox之类的浏览器进行测试?任何建议都是有用的.
Ok thanks. Does anyone have any suggestions? I do not have to worry about the client side of the browser. I only need to implement the get function. The program must accept a port number and be able to display web data such as text, image data, and directory info. I''m really not knowing where to start especially since I''m not able to open any port. Maybe I''m not inputing right port number. Does anyone know how to find out what port number a computer is using say for a browser such as firefox for testing purposes? Any suggestions what so ever would be useful thanks.


这篇关于Web服务器接受套接字作为参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆