Using java Syntax Highlighting
- channel = DatagramChannel.open();
- channel.socket().bind(new InetSocketAddress(4666));
- channel.configureBlocking(true);
- receiver = new ListenThread();
- receiver.start();
Parsed in 0.031 seconds, using GeSHi 1.0.8.4
sender is initialized in other place.
This is the SendThread
Using java Syntax Highlighting
- private class SendThread extends Thread{
- private boolean stop = false;
- public SendThread(){
- super("This is the Sender Thread");
- }
- public void stopSender(){
- this.stop = true;
- this.interrupt();
- }
- public void run(){
- while(!send_packet_queue.isEmpty()){
- try{
- if(stop) return;
- Packet packet = send_packet_queue.poll();
- ByteBuffer sendBuffer = packet.getPacketData();
- InetSocketAddress tempTarget = packet.target;
- sendBuffer.position(0);
- channel.send(sendBuffer, tempTarget);
- System.out.println("End Sending Info to" + tempTarget+
- "---------------");
- }catch(Exception e){
- e.printStackTrace();
- }
- }
- }
- }
Parsed in 0.033 seconds, using GeSHi 1.0.8.4
This is the ListenThread
Using java Syntax Highlighting
- private class ListenThread extends Thread{
- private boolean stop = false;
- ByteBuffer buffer = Utils.getByteBuffer(1024);
- public ListenThread(){
- super("This is the Listen Thread");
- }
- public void stopListener(){
- this.stop = true;
- this.interrupt();
- }
- public void run(){
- while(!stop){
- try{
- buffer.clear();
- System.out.println("Begin Listening------------");
- SocketAddress senderAddress = channel.receive(buffer);
- //////////////////////////////////////////////////////////////////////////
- System.out.println("received request from other peer called: "+ senderAddress);
- buffer.limit(buffer.position());
- long x = System.currentTimeMillis();
- ByteBuffer rawData = Utils.getByteBuffer(buffer.limit());
- buffer.position(0);
- rawData.position(0);
- rawData.put(buffer);
- rawData.position(0);
- Packet rawPacket = new Packet(rawData, (InetSocketAddress)senderAddress);
- long y = System.currentTimeMillis();
- DDB.getSingleton().receivePacket(rawPacket);
- }catch(Exception e){
- e.printStackTrace();
- }
- }
- }
- }
Parsed in 0.038 seconds, using GeSHi 1.0.8.4
Hope someone can help me out, thanks

