właśnie nie mam zapętlonego tworzenia obiektów w zasadzi doszedłem do wniosku że bardzo nabiera na pamięci serwer i klient gdy wysyła i odbiera pakiety.
import java.util.TimerTask;
public class CC_timer extends TimerTask {
public void run() {
for (int xx=0;xx<=1000;xx++) {
C_start.wys.wyslij();
C_start.a++;
}
}
}
import java.io.IOException;
import java.nio.BufferUnderflowException;
import java.nio.channels.ClosedChannelException;
import org.xsocket.MaxReadSizeExceededException;
import org.xsocket.connection.IDataHandler;
import org.xsocket.connection.INonBlockingConnection;
public class C_echo implements IDataHandler {
@Override public boolean onData(INonBlockingConnection nbc) throws IOException,ClosedChannelException,BufferUnderflowException,MaxReadSizeExceededException{
String x = nbc.readStringByLength(100);
System.out.println("S >> C = "+x);
nbc.setIdleTimeoutMillis(1000);
C_start.licznik++;
C_start.okno.setTitle(String.valueOf(C_start.licznik));
try { nbc.close(); } catch(java.io.IOException ee) {}
return true;
}
}
import java.net.InetAddress;
import org.xsocket.connection.INonBlockingConnection;
import org.xsocket.connection.NonBlockingConnectionPool;
public class C_send {
InetAddress inetAddress=null;
INonBlockingConnection nbc=null;
NonBlockingConnectionPool pool = new NonBlockingConnectionPool();
synchronized public void wyslij() {
// nbc.setIdleTimeoutMillis(1000);
try{ nbc = pool.getNonBlockingConnection(inetAddress, 8090);
try{ nbc.write("|01|______|02|______|03|______|04|______|05|______|06|______|07|______|08|______|09|______|10|______"); }catch(Exception ee){}
}catch(java.io.IOException f){}
}
}
import java.io.IOException;
import java.net.InetAddress;
import java.util.Timer;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JApplet;
import javax.swing.JFrame;
import org.xsocket.connection.INonBlockingConnection;
import org.xsocket.connection.IServer;
import org.xsocket.connection.Server;
public class C_start extends JApplet {
INonBlockingConnection bc=null;
public static C_send wys = new C_send();
public static JFrame okno = new JFrame();
public static int a=0;
public static int licznik=0;
@Override public void init(){
try{ C_start.wys.inetAddress = InetAddress.getByName("127.0.0.1"); }catch (Exception ee){}
Timer timer1 = new Timer(); CC_timer timer1_task = new CC_timer(); timer1.schedule (timer1_task, 0, 1);
try { System.out.println("--------------------------------------------------------");
System.out.println("| Client START! |");
System.out.println("--------------------------------------------------------");
IServer srv = new Server(8095, new C_echo()); srv.run(); srv.start();
} catch (IOException ex) { Logger.getLogger(C_start.class.getName()).log(Level.SEVERE, null, ex); }
}
}
import java.io.IOException;
import java.net.InetAddress;
import java.nio.BufferUnderflowException;
import java.nio.channels.ClosedChannelException;
import org.xsocket.MaxReadSizeExceededException;
import org.xsocket.connection.IDataHandler;
import org.xsocket.connection.INonBlockingConnection;
public class S_echo implements IDataHandler {
InetAddress ip;
@Override public boolean onData( INonBlockingConnection nbc) throws IOException,ClosedChannelException, BufferUnderflowException,MaxReadSizeExceededException{
String x = nbc.readStringByLength(100);
nbc.setIdleTimeoutMillis(1000);
S_start.test_ip= nbc.getRemoteAddress();
System.out.println("C >> S = "+x);
S_start.wys.wyslij();
try { nbc.close(); } catch(java.io.IOException ee) {}
nbc.flush();
nbc.close();
return true;
}
}
import java.net.InetAddress;
import org.xsocket.connection.INonBlockingConnection;
import org.xsocket.connection.NonBlockingConnectionPool;
public class S_send {
InetAddress inetAddress=null;
INonBlockingConnection nbc=null;
NonBlockingConnectionPool pool= new NonBlockingConnectionPool();
String test2="";
String test="";
synchronized public void wyslij() {
try{test2 = S_start.test_ip.toString(); test = test2.substring(1); inetAddress = InetAddress.getByName(test);
nbc = pool.getNonBlockingConnection(inetAddress, 8095);
try{ nbc.write("|01|_xxx__|02|______|03|______|04|______|05|______|06|______|07|______|08|______|09|______|10|______"); }catch(Exception ee){}
nbc.flush(); }catch(java.io.IOException f){}
}
}
import java.io.IOException;
import java.net.InetAddress;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JApplet;
import org.xsocket.connection.INonBlockingConnection;
import org.xsocket.connection.IServer;
import org.xsocket.connection.Server;
public class S_start extends JApplet {
public static InetAddress inetAddress=null;
public static INonBlockingConnection bc=null;
public static S_send wys = new S_send();
public static InetAddress test_ip=null;
@Override public void init(){
try { System.out.println("--------------------------------------------------------");
System.out.println("| Serwer START! |");
System.out.println("--------------------------------------------------------");
IServer srv = new Server(8090, new S_echo()); srv.run(); srv.start();
} catch (IOException ex) { Logger.getLogger(S_start.class.getName()).log(Level.SEVERE, null, ex); }
}
}
może ktoś rzucić okiem?
soket:
http://sourceforge.net/projects/xsocket/files/xsocket%202.x/2.7.2/xSocket-2.7.2.jar/download
projekt w netbeans:
http://mgs_saladin.republika.pl/nauka_05.zip