2012-06-01 15 views
6

का उपयोग कर असीमित सर्वर मैं लिखने योग्य खंड के बिना जावा एनओओ सर्वर बनाने के लिए this ट्यूटोरियल का उपयोग कर रहा हूं।जावा एनआईओ

सभी ठीक काम करता है, एक दिलचस्प बात यह है के लिए छोड़कर:

  • जब ग्राहक के पैकेट बहुत तेजी से भेज रहा है, सर्वर, सभी संदेशों को प्राप्त करता है नहीं करता है सर्वर हमेशा पहले और दूसरे पैकेट लेकिन नहीं मिल रहा है उस से भी अधिक।
  • यदि ग्राहक धीरे-धीरे पैकेट भेज रहा है तो सर्वर को सभी पैकेट मिलते हैं।

कोई विचार?

यदि मैं नीचे दिए गए कोड में उल्लिखित एक और वर्ग की आवश्यकता है, तो मैं सर्वर क्लास कोड जोड़ रहा हूं, मैं यहां हूं :)।

NIOServer वर्ग:

package server; 
import java.io.IOException; 
import java.net.InetAddress; 
import java.net.InetSocketAddress; 
import java.net.Socket; 
import java.nio.ByteBuffer; 
import java.nio.channels.SelectionKey; 
import java.nio.channels.Selector; 
import java.nio.channels.ServerSocketChannel; 
import java.nio.channels.SocketChannel; 
import java.nio.channels.spi.SelectorProvider; 
import java.util.*; 

import javax.xml.parsers.ParserConfigurationException; 

import org.xml.sax.SAXException; 

public class NioServer implements Runnable { 



// The host:port combination to listen on 
    private InetAddress hostAddress; 
    private int port; 

    // The channel on which we'll accept connections 
    private ServerSocketChannel serverChannel; 

    // The selector we'll be monitoring 
    private Selector selector; 

    //the cach will hundle the messages that came 
    private Cache cache; 

    // The buffer into which we'll read data when it's available 
    private ByteBuffer readBuffer = ByteBuffer.allocate(8192); 

    public NioServer(InetAddress hostAddress, int port , Cache cache) throws IOException { 
    this.cache = cache; 
    this.hostAddress = hostAddress; 
    this.port = port; 
    this.selector = this.initSelector(); 
    } 


    private Selector initSelector() throws IOException { 
     // Create a new selector 
     Selector socketSelector = SelectorProvider.provider().openSelector(); 

     // Create a new non-blocking server socket channel 
     this.serverChannel = ServerSocketChannel.open(); 
     serverChannel.configureBlocking(false); 

     // Bind the server socket to the specified address and port 
     InetSocketAddress isa = new InetSocketAddress(this.hostAddress, this.port); 
     serverChannel.socket().bind(isa); 

     // Register the server socket channel, indicating an interest in 
     // accepting new connections 
     serverChannel.register(socketSelector, SelectionKey.OP_ACCEPT); 

     return socketSelector; 
     } 

    private void accept(SelectionKey key) throws IOException { 
     // For an accept to be pending the channel must be a server socket channel. 
     ServerSocketChannel serverSocketChannel = (ServerSocketChannel) key.channel(); 

     // Accept the connection and make it non-blocking 
     SocketChannel socketChannel = serverSocketChannel.accept(); 
     Socket socket = socketChannel.socket(); 
     socketChannel.configureBlocking(false); 

     // Register the new SocketChannel with our Selector, indicating 
     // we'd like to be notified when there's data waiting to be read 
     socketChannel.register(this.selector, SelectionKey.OP_READ); 
     } 

    private void read(SelectionKey key) throws IOException { 
     SocketChannel socketChannel = (SocketChannel) key.channel(); 

     // Clear out our read buffer so it's ready for new data 
     this.readBuffer.clear(); 

     // Attempt to read off the channel 
     int numRead; 
     try { 
      numRead = socketChannel.read(this.readBuffer); 
      String test = new String(this.readBuffer.array()); 
      System.out.print(test); 

     } catch (IOException e) { 
      // The remote forcibly closed the connection, cancel 
      // the selection key and close the channel. 
     // key.cancel(); 
     // socketChannel.close(); 
      return; 
     } 

     if (numRead == -1) { 
      // Remote entity shut the socket down cleanly. Do the 
      // same from our end and cancel the channel. 
      key.channel().close(); 
      key.cancel(); 
      return; 
     } 

     // Hand the data off to our worker thread 
     this.cache.processData(this, socketChannel, this.readBuffer.array(), numRead); 
     } 

    public void run() { 
     while (true) { 
      try { 
      // Wait for an event one of the registered channels 

      this.selector.select(); 



      // Iterate over the set of keys for which events are available 
      Iterator selectedKeys = this.selector.selectedKeys().iterator(); 
      while (selectedKeys.hasNext()) { 
       SelectionKey key = (SelectionKey) selectedKeys.next(); 
       selectedKeys.remove(); 

       if (!key.isValid()) { 
       continue; 
       } 

       // Check what event is available and deal with it 
       if (key.isAcceptable()) { 
       this.accept(key); 
       } else if (key.isReadable()) { 
       this.read(key); 
       } 
      } 
      } catch (Exception e) { 
      e.printStackTrace(); 
      } 
     } 
     } 

    public static void main(String[] args) throws ParserConfigurationException, SAXException { 
    try { 
     Cache cache = new Cache(); 
     new Thread(cache).start(); 
     new Thread(new NioServer(null, 9090,cache)).start(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 
    } 
+3

आपके कोड में एक बग होना चाहिए। यदि आप और मदद चाहते हैं, तो हमें और जानकारी दें। –

+0

मेरे पास अब कोड नहीं है, मैं इसे रविवार को प्राप्त करूंगा। धन्यवाद –

+3

टीसीपी डेटा खोना नहीं है और न ही एनआईओ करता है। आप या तो सभी डेटा नहीं पढ़ रहे हैं या इसे कुछ फेंक रहे हैं। इस पर टिप्पणी करने के लिए कुछ कोड के बिना आगे टिप्पणी करना असंभव है। – EJP

उत्तर

1

मैं उम्मीद होती है कि यदि आप यूडीपी पढ़ रहे थे। ध्यान दें कि आप अपने पैकेट को read विधि पर कितनी धीमी गति से संसाधित कर रहे हैं। आप उन्हें सिस्टम पर प्रिंट कर रहे हैं। जो बहुत धीमी है और यह सुनिश्चित नहीं है कि आप processData विधि पर डेटा को अन्य थ्रेड पर कितनी तेजी से संभालने में सक्षम हैं। This library मैंने लिखा है कि यदि आप अपने अंतराल का स्रोत हैं तो इंटर-थ्रेड गैर-अवरोधन संचार करने में आपकी मदद कर सकते हैं। आपको अपने अंतर्निहित पठन सॉकेट बफर का आकार भी देखना चाहिए। इससे बड़ा यह है कि जितना अधिक कमरा आपको जल्दी और पकड़ने से पहले पैकेट छोड़ना शुरू हो जाएगा। टीसीपी के लिए, अगर आप अंतर्निहित सॉकेट बफर भर जाते हैं तो आपको शायद चैनल पर IOException मिलता है। यूडीपी के लिए पैकेट चुपचाप गिराए जाते हैं।

final Socket socket = channel.socket(); 
System.out.println(socket.getReceiveBufferSize()); 
socket.setReceiveBufferSize(newSize); 

नोट::

आप कर सकते हैं अंतर्निहित पढ़ने सॉकेट बफ़र आकार तक पहुंचने के लिए AFAIK, लिनक्स के लिए कुछ ओएस विन्यास की आवश्यकता हो सकती आप अंतर्निहित बफर आकार बदलने के लिए के लिए। यदि setReceiveBufferSize का कोई प्रभाव नहीं है (इसे देखने के लिए इसे दोबारा पढ़ें), इसके बारे में Google। :)

संबंधित मुद्दे