Dukascopy
 
 
Wiki JStore Search Login

Attention! Read the forum rules carefully before posting a topic.

    Try to find an answer in Wiki before asking a question.
    Submit programming questions in this forum only.
    Off topics are strictly forbidden.

Any topics which do not satisfy these rules will be deleted.

socket client side
 Post subject: socket client side Post rating: 0   New post Posted: Fri 16 May, 2014, 10:02 

User rating: 1
Joined: Wed 19 Feb, 2014, 10:42
Posts: 16
Location: NetherlandsNetherlands
Hi All,

I ran into this topic viewtopic.php?f=65&t=48225&p=67527&hilit=socket#p67527
which was locked so i opened a new topic instead.

Im working on copying trades from my laptop JForex terminal to my desktop JForex terminal. At this moment im just writing
the basics and wanting to establish the connection. The server socket is already established but i fail to implement
a correct client socket strategy. Whenever i open this strategy on my laptop which calls the socket on the desktop, it fails with a message
that it cannot bind to the socket... I checked, double checked and the firewall is not blocking it, port is open and available and the hostname can be correctly found connecting through my laptop jforex strategy.
The weirdest thing though is that once i run my .Net application i wrote to test the socketconnection on the server, it does connect without any delay and problems... so this tells me the problem is with the bind.(..) method, i guess.


            
            print("Inside 4a." );   //for debugging
            InetSocketAddress socketAddress = new InetSocketAddress(IPaddress, 8888); // IPaddess by param, 8888 just a free port for testing
            print("socketAddress=" + socketAddress.getHostName().toString());
            print("Inside 4b." );  //for debugging
           
            serverSocket.bind(socketAddress);   //This line fails!



this is part of the code which fails... When it fails it says
"09:00:10 java.net.BindException: Cannot assign requested address: bind"

Is there anyone who can help me or give me some pointers?
Anyone correctly established a socket client from within a Jforex terminal?

Thanks in advance,
Phil


 
 Post subject: Re: socket client side Post rating: 0   New post Posted: Fri 16 May, 2014, 11:46 
User avatar

User rating:
Joined: Fri 31 Aug, 2007, 09:17
Posts: 6139
Does the code work successfully outside the platform (i.e., from JForex-SDK)?


 
 Post subject: Re: socket client side Post rating: 0   New post Posted: Fri 16 May, 2014, 12:12 

User rating: 1
Joined: Wed 19 Feb, 2014, 10:42
Posts: 16
Location: NetherlandsNetherlands
API Support wrote:
Does the code work successfully outside the platform (i.e., from JForex-SDK)?


Yes, like i said, i have quickly written a .NET application which connects successfully to the socketserver and also
gets a response back from the sever.

Phil


 
 Post subject: Re: socket client side Post rating: 0   New post Posted: Fri 16 May, 2014, 12:20 
User avatar

User rating:
Joined: Fri 31 Aug, 2007, 09:17
Posts: 6139
We meant a java program using our JForex-SDK, see:
https://www.dukascopy.com/wiki/#Use_in_Eclipse
or
https://www.dukascopy.com/wiki/#Use_in_NetBeans
To detect if it is a JForex-specific, platform-specific or java-specific problem you would a)first need to create two simple java programs which test the socket client-server functionality, b) integrate the program in JForex-SDK to see if it works with strategies and c) launch the strategy from the platform. Once you succeeded doing a), we could assist you with b) and c).


 
 Post subject: Re: socket client side Post rating: 1   New post Posted: Fri 16 May, 2014, 15:15 

User rating: 1
Joined: Wed 19 Feb, 2014, 10:42
Posts: 16
Location: NetherlandsNetherlands
API Support wrote:
We meant a java program using our JForex-SDK, see:
https://www.dukascopy.com/wiki/#Use_in_Eclipse
or
https://www.dukascopy.com/wiki/#Use_in_NetBeans
To detect if it is a JForex-specific, platform-specific or java-specific problem you would a)first need to create two simple java programs which test the socket client-server functionality, b) integrate the program in JForex-SDK to see if it works with strategies and c) launch the strategy from the platform. Once you succeeded doing a), we could assist you with b) and c).


Ok, so i misunderstood that one.
Right now i have found some time to download Eclipse and create the test example from the DukasCopy Wiki page (Socket Channel).

When i do run this code, which i inserted into a class file within Eclipse, it runs just fine. The desktop (server computer) receives the two orders and those get created... Still however when i use the code within my JForex terminal, it fails on the bind method.

This is the code i used in the Eclipse editor (I have changed the IP):

package ClientSocketTest;


   import java.net.InetAddress;
   import java.net.InetSocketAddress;
   import java.nio.ByteBuffer;
   import java.nio.channels.SelectionKey;
   import java.nio.channels.Selector;
   import java.nio.channels.SocketChannel;
   import java.util.Iterator;

   public class SocketClient {
       public static void main(String[] args) throws Exception {
           SocketChannel channel = SocketChannel.open(new InetSocketAddress("111.222.3.44", 8777));
           try {
               channel.configureBlocking(false);
               Selector selector = Selector.open();
               SelectionKey channelKey = channel.register(selector, SelectionKey.OP_READ | SelectionKey.OP_WRITE);
               ByteBuffer writeBuffer = ByteBuffer.allocateDirect(100);
               ByteBuffer readBuffer = ByteBuffer.allocate(1000);
               writeBuffer.flip();
               long lastSent = Long.MIN_VALUE;
               boolean lastBuy = false;
               int ordersCount = 0;
               while (true) {
                   if (!writeBuffer.hasRemaining() && lastSent + 5000 < System.currentTimeMillis()) {
                       if (ordersCount >= 2) {
                           break;
                       }
                       writeBuffer.clear();
                       String command = lastBuy ? "SELL" : "BUY";
                       System.out.println("Sending [" + command + "] command");
                       writeBuffer.put(command.getBytes("UTF-8"));
                       writeBuffer.put((byte) 0x04);
                       writeBuffer.flip();
                       channelKey.interestOps(SelectionKey.OP_READ | SelectionKey.OP_WRITE);
                       lastBuy = !lastBuy;
                       ordersCount++;
                   }
                   //block for 100ms waiting for ready channel
                   int actions = selector.select(100);
                   if (actions == 0) {
                       continue;
                   }

                   for (Iterator<SelectionKey> iterator = selector.selectedKeys().iterator(); iterator.hasNext();) {
                       SelectionKey key = iterator.next();
                       SocketChannel socketChannel = (SocketChannel) key.channel();
                       if (key.isReadable()) {
                           while (socketChannel.read(readBuffer) > 0) {
                               readBuffer.flip();
                               for (int i = readBuffer.position(); i < readBuffer.limit(); i++) {
                                   byte b = readBuffer.get(i);
                                   if (b == 0x04) {
                                       byte[] data = new byte[i - readBuffer.position()];
                                       readBuffer.get(data);
                                       readBuffer.get();
                                       System.out.println(new String(data, "UTF-8"));
                                   }
                               }
                               readBuffer.compact();
                           }
                       }
                       if (key.isWritable()) {
                           while (writeBuffer.hasRemaining() && socketChannel.write(writeBuffer) > 0) {
                           }
                           if (!writeBuffer.hasRemaining()) {
                               key.interestOps(SelectionKey.OP_READ);
                               lastSent = System.currentTimeMillis();
                           }
                       }
                       iterator.remove();
                   }
               }
           } finally {
               channel.close();
           }
       }
   }



Hope you/anybody can help me getting it to work within the JForex terminal.

Thanks in advance.

Phil


 
 Post subject: Re: socket client side Post rating: 0   New post Posted: Fri 16 May, 2014, 15:35 

User rating: 1
Joined: Wed 19 Feb, 2014, 10:42
Posts: 16
Location: NetherlandsNetherlands
Thanks all for listening and helping out.

I have found the issue which was causing the problem.
It turned out that these lines:
            ServerSocketChannel serverChannel = ServerSocketChannel.open();
            //get the associated server socket
            ServerSocket serverSocket = serverChannel.socket();
                try {
                    //bind the server socket to the port
                   InetSocketAddress socketAddress = new InetSocketAddress(IPaddress, port);
                   serverSocket.bind(socketAddress);
            ...

were wrong or not needed. (I guess since i used a serverSocket instead of a regular SocketChannel...)
So when i replaced the above lines with the single line below:
            SocketChannel serverChannel = SocketChannel.open(new InetSocketAddress(IPaddress, port));

It just worked fine. Thanks a lot.
Now i can proceed and alter the code accordingly to meet my needs.

Best Regards,
Phil


 

Jump to:  

  © 1998-2025 Dukascopy® Bank SA
On-line Currency forex trading with Swiss Forex Broker - ECN Forex Brokerage,
Managed Forex Accounts, introducing forex brokers, Currency Forex Data Feed and News
Currency Forex Trading Platform provided on-line by Dukascopy.com