Stefan aus Biberach: Wieso liefert dieses Socket nichts ?

Beitrag lesen

Hi,

ich habe ein Problem und zwar wenn ich die Socket Anwendung starte die Non Blocking Sockets benutzt, wenn ich die Anwendung nun starte so sagt er will was empfangen aber er macht letztendlich nichts er sagt nur "null" als Antwort wie kann ich jetzt dafür sorgen dass, dieses Programm das tut was es soll Daten aus einer Socket Anwendung lesen ?

Hier der Quellcode:

CKernel:

package CKernel;

import java.net.*;
import java.io.*;
import java.util.*;
import java.nio.charset.*;
import java.nio.channels.*;
import java.nio.*;

public class CKernel
{
 SocketChannel sc;
 Selector selector;
 CharsetEncoder encoder;
 Charset charset;
 String inData;

final static int MAX_LAENGE = 1024;

public void CreateSocket(String ServerIP, int port)
 {
  try {
   // First of all create the socket channel
   sc = SocketChannel.open();

// i need a non-blocking socket
   sc.configureBlocking(false);

// the selector VERY IMPORTANT
   selector = Selector.open();

// Socket Addy
   InetSocketAddress addy = new InetSocketAddress
    (ServerIP, port);

// Connect 2 Server
   sc.connect(addy);

while(!sc.finishConnect())
   {
    // this runs until the kernel has connected to the Server

}

// Now we have got a connection but what's with the Charset
   // NOT INTERNATIONAL
   charset = Charset.forName("ISO-8859-1"); //GERMAN
   encoder = charset.newEncoder();

sc.register(selector, SelectionKey.OP_READ|SelectionKey.OP_WRITE);

} catch(IOException e) {
   // IOException bla la
  }
 }

public void Out(String msg)
 {
  System.out.println(msg);
 }

public void CloseSocket()
 {
  try {
   sc.close();
  } catch(IOException e) {
   // bla bla
  } catch(NullPointerException e) {
   // bla bla
  }
 }

public String SocketEmpfange()
 {
  try {
   while(true){

int n = selector.select();

if(n==0)
    {
     continue;
    }
    else
    {
     break;
    }
   }

Set readyKeys = selector.selectedKeys();
   Iterator it = readyKeys.iterator();

while(it.hasNext())
   {
    SelectionKey selKey = (SelectionKey)it.next();

if(selKey.isReadable())
    {
     // time to setup read
     ByteBuffer incomingData =
      ByteBuffer.allocate(MAX_LAENGE);
     incomingData.clear();

int count;

while((count = sc.read(incomingData)) > 0)
     {
      // reading the data

}

int pos = incomingData.position();

incomingData.flip();
     CharBuffer content = charset.decode(incomingData);

inData = content.toString();

break;
    }
   }
  } catch(IOException e){
   // bla bla
  } catch(NullPointerException e){
   // bla bla
  }

return inData;
 }

public void SocketSend()
 {

}
}

CDragonConsole:

import CKernel.*;

public class DragonConsole
{
 public static void main(String args[])
 {
  CKernel kernel = new CKernel();
  kernel.CreateSocket("localhost", 333);

kernel.Out("Socket succesfuly initialized :: OK_SOCKREADY\n");

kernel.Out("Trying to get some data...\n");

String tmp = kernel.SocketEmpfange();
  kernel.Out(tmp);
  kernel.CloseSocket();
 }
}

Vielen Dank für eure Bemühungen schon im Voraus

Stefan ;-)