简体   繁体   English

延迟处理客户端数据:将Java套接字ID视为文件ID

[英]Delay processing client data : Treat java socket id as a file id

I have a java client-server application that works fine . 我有一个运行正常的Java客户端服务器应用程序。 Now i wish to make some changes that i am not sure if its possible . 现在,我想做一些不确定的事情,如果可能的话。 I want to delay proccessing the client data when accept of the server breaks . 我想在接受服务器中断时延迟处理客户端数据。 I would like to wrap all the processing logic after accept breaks into a function that i call whenever i wish to process data and not immediately after the accept breaks . 我想将接受中断后的所有处理逻辑包装到一个我希望处理数据时而不是在接受中断后立即调用的函数中。 Perl allows this using the sysread function which treats the socket id as a File handler and does the processing. Perl允许使用sysread函数将套接字ID视为文件处理程序并进行处理。 Is it possible in Java ? Java是否可能?

What exists : 存在什么:

void server_side()
{ 

     // usual server code

     accept() // blocking code waiting for client data 

     ...accept breaks so process data now ..
}

What i want : 我想要的是 :

void server_side(){

     // usual servercode

     accept() // blocking for client data

     .. I dont want to process just yet .. Wait !
}

void main() 
{
  .. Usual code ..

   **Ok lets process client data here .**

}

In a way Perl is beautiful and allows such a thing to happen . Perl在某种程度上很漂亮,并且允许发生这种事情。 In perl you can use the sysread command like this 在perl中,您可以像这样使用sysread命令

    Create socket

       void create()
    {

        .. Usual socket code ..
         $sock = $mainsock->accept();
         ... DONT process here . Will process later in function processing 

    }

void processing()

{

    .. Use sysread to treat socket handler as file . 

    .. Lets read now.

    my $bytes_to_read = 1024;
    my $bytes_read = sysread($sock, $buffer, $bytes_to_read);

    ...

}

Can i do this in Java ? 我可以用Java做到这一点吗?

Nothing is forcing you to ever do anything to a socket after you accept it. 接受套接字后,什么也没有强迫您执行任何操作。 You have to hold on to the socket object, obviously, to know where to go for the data (or to send data), but you can wait as long as you want for your application to handle the data, regardless of the language. 显然,您必须抓住套接字对象才能知道数据的存放位置(或发送数据),但是无论您使用哪种语言,只要您想让应用程序处理数据,就可以等待。

http://docs.oracle.com/javase/7/docs/api/java/net/Socket.html http://docs.oracle.com/javase/7/docs/api/java/net/Socket.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM