[xSocket-develop] Accessing nbc from another class
Status: Inactive
Brought to you by:
grro
|
From: dytu i. <jun...@wp...> - 2008-11-26 08:55:39
|
hi,
I have a problem. I can't send data using nbc.write("some text") to a
client from function in my main class. How can I select the connection
that I want to use if there are 2 different?
In my main function in my main class of my java program I created two
servers which are maintaining two different nbc on different sockets.
srvHandler = new xSocketDataHandler();
srv = new Server(8091, srvHandler);
srv.start();
srv2 = new Server(8090, new xSocketDataHandler());
srv2.start();
As You can see, xSocketDataHandler is another simple class (in file
xSocketDataHandler.java) with onData function only. It looks like this:
package xsocketserver;
import java.io.IOException;
import java.nio.BufferUnderflowException;
import java.nio.channels.ClosedChannelException;
import org.xsocket.*;
import org.xsocket.connection.*;
public class xSocketDataHandler implements IDataHandler
{
public INonBlockingConnection nbc;
public boolean onData(INonBlockingConnection nbc) throws IOException,
BufferUnderflowException, ClosedChannelException,
MaxReadSizeExceededException
{
try
{
String data = nbc.readStringByDelimiter("\0");
Main.stringSplit(data);
nbc.write(data + "\0");
//nbc.write("trele morele" + "\0");
}
catch(Exception ex)
{
System.out.println(ex.getMessage());
}
return true;
}
Now I want to access nbc from function StringSplit in my main class and
be able to send some data for example to the client on the another side
of connection on socket 8091, independently from onData function (now
sending data works only in this function). I set nbc to public in
xSocketDataHandler class and I tried many different ways of accessing
that nbc but doesn't mater what I do, the client doesn't receive data.
package xsocketserver;
import org.xsocket.connection.*;
import org.jdom.input.SAXBuilder;
import org.jdom.Document;
import org.jdom.JDOMException;
import org.jdom.Element;
import org.jdom.output.XMLOutputter;
import org.jdom.Attribute;
import java.io.*;
//import xSocketDataHandler.*;
public class Main
{
protected static IServer srv = null;
protected static IServer srv2 = null;
public static Document doc;
public static Element webapp;
public static INonBlockingConnection nbc;
public static xSocketDataHandler srvHandler;
public static void main(String[] args)
{
try
{
//nbc1 = new NonBlockingConnection("127.0.0.1", 8092);
srvHandler = new xSocketDataHandler();
srv = new Server(8091, srvHandler);
srv.start();
srv2 = new Server(8090, new xSocketDataHandler());
srv2.start();
//nbc = new NonBlockingConnection(srvHandler);
//nbc1.write("aaaaaaabbbbbbbaaaaa" + "\0");
}
catch(Exception ex)
{
System.out.println(ex.getMessage());
}
}
public static void stringSplit(String str)
{
try
{ nbc.write("some text");
//xSocketDataHandler obj = new xSocketDataHandler();
//obj.onData(true).nbc.write("dsfds");
//srvHandler obj = new srvHandler();
//obj.nbc.write("trele morele" + "\0");
//nbc1.write("aaaaaaa" + "\0");
}
catch(Exception ex)
{
System.out.println(ex.getMessage());
}
}
----------------------------------------------------
Uważaj na drodze:
http://klik.wp.pl/?adr=http%3A%2F%2Fprorocznia.pl%2Ff.html%3Fi%3D37169O-283747030O0&sid=568
|