Dukascopy
 
 
Wiki JStore Search Login

JFOREX SDK how to add socket client to strategy
 Post subject: JFOREX SDK how to add socket client to strategy Post rating: 1   Post Posted: Thu 10 Apr, 2014, 18:58 
User avatar

User rating: 6
Joined: Tue 11 Feb, 2014, 11:38
Posts: 60
Location: PolandPoland
Hi
how add this socket client to strategy - Jforex platform?

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;

public class SocketClient {

        public static void main(String[] args) {
                try {
                        InetAddress host = InetAddress.getLocalHost();
                        Socket socket = new Socket(host.getHostName(), 7001);
                        ObjectOutputStream outputStream = new ObjectOutputStream(socket.getOutputStream());
                        outputStream.writeObject("SELL");
                        ObjectInputStream inputStream = new ObjectInputStream(socket.getInputStream());
                        String message = (String) inputStream.readObject();
                        System.out.println("Message: " + message);
                        inputStream.close();
                        outputStream.close();
                } catch (UnknownHostException e) {
                        e.printStackTrace();
                } catch (IOException e) {
                        e.printStackTrace();
                } catch (ClassNotFoundException e) {
                        e.printStackTrace();
                }
        }
}


Example please ...

Regards
:roll:


 
 Post subject: Re: JFOREX SDK how to add socket client to strategy Post rating: 1   Post Posted: Thu 10 Apr, 2014, 19:10 
User avatar

User rating: 6
Joined: Tue 11 Feb, 2014, 11:38
Posts: 60
Location: PolandPoland
and preferably this:

import java.io.*;
import java.net.*;
public class SocketClient {
   public static void main(String[] args) {
      BufferedReader in = new BufferedReader(new InputStreamReader(
         System.in));
      PrintStream out = System.out;      
      try {
         Socket c = new Socket("localhost",8888);
         printSocketInfo(c);
         BufferedWriter w = new BufferedWriter(new OutputStreamWriter(
            c.getOutputStream()));
         BufferedReader r = new BufferedReader(new InputStreamReader(
            c.getInputStream()));
         String m = null;
         while ((m=r.readLine())!= null) {
            out.println(m);
            m = in.readLine();
            w.write(m,0,m.length());
            w.newLine();
            w.flush();
         }
         w.close();
         r.close();
         c.close();
      } catch (IOException e) {
         System.err.println(e.toString());
      }
   }
   private static void printSocketInfo(Socket s) {
      System.out.println("Remote address = "
         +s.getInetAddress().toString());
      System.out.println("Remote port = "
         +s.getPort());
      System.out.println("Local socket address = "
         +s.getLocalSocketAddress().toString());
      System.out.println("Local address = "
         +s.getLocalAddress().toString());
      System.out.println("Local port = "
         +s.getLocalPort());
   }
}


thanks and bye


 
 Post subject: Re: JFOREX SDK Socket server Post rating: 1   Post Posted: Fri 11 Apr, 2014, 11:00 
User avatar

User rating: 6
Joined: Tue 11 Feb, 2014, 11:38
Posts: 60
Location: PolandPoland
Hi,

Simple server multi connection:

import java.io.*;
import java.net.*;
public class ReverseEchoServer implements Runnable {
   private Socket con = null;
   public static void main(String[] args) {
      try {
         ServerSocket s = new ServerSocket(8888);
         printServerSocketInfo(s);
         while (true) {
            Socket c = s.accept();
            printSocketInfo(c);
            ReverseEchoServer v = new ReverseEchoServer(c);
            Thread t = new Thread(v);
            t.start();
         }
      } catch (IOException e) {
         System.err.println(e.toString());
      }
   }
   public ReverseEchoServer(Socket c){
      con = c;
   }
   public void run() {
      try {
         BufferedWriter w = new BufferedWriter(new OutputStreamWriter(
            con.getOutputStream()));
         BufferedReader r = new BufferedReader(new InputStreamReader(
            con.getInputStream()));
         String m = "Welcome to Reverse Echo Server."+
            " Please type in some words.";
         w.write(m,0,m.length());
         w.newLine();
         w.flush();
         while ((m=r.readLine())!= null) {
            if (m.equals(".")) break;
            char[] a = m.toCharArray();
            int n = a.length;
            for (int i=0; i<n/2; i++) {
               char t = a[i];
               a[i] = a[n-1-i];
               a[n-i-1] = t;
            }
            w.write(a,0,n);
            w.newLine();
            w.flush();
         }
         w.close();
         r.close();
         con.close();
      } catch (IOException e) {
         System.err.println(e.toString());
      }
   }
   private static void printSocketInfo(Socket s) {
      System.out.println("Remote address = "
         +s.getInetAddress().toString());
      System.out.println("Remote port = "
         +s.getPort());
      System.out.println("Local socket address = "
         +s.getLocalSocketAddress().toString());
      System.out.println("Local address = "
         +s.getLocalAddress().toString());
      System.out.println("Local port = "
         +s.getLocalPort());
   }
   private static void printServerSocketInfo(ServerSocket s) {
      System.out.println("Server socker address = "
         +s.getInetAddress().toString());
      System.out.println("Server socker port = "
         +s.getLocalPort());
   }
}


and link to page with cool examples
https://www.herongyang.com/JDK/Socket-Network-Communication.html
SSL socket
https://www.herongyang.com/JDK/SSL-Socket-Communication-Testing-Program.html

No way to guess at the answer...
Very wise people write these examples in Dukascopy wiki.
Like self and others make life difficult.
Bye.


 
 Post subject: JFOREX SDK- Socket Connections Post rating: 1   Post Posted: Sat 12 Apr, 2014, 06:35 
User avatar

User rating: 6
Joined: Tue 11 Feb, 2014, 11:38
Posts: 60
Location: PolandPoland
Hi,
Image


Attachments:
SocketConnections.png [210.32 KiB]
Downloaded 268 times
DISCLAIMER: Dukascopy Bank SA's waiver of responsability - Documents, data or information available on this webpage may be posted by third parties without Dukascopy Bank SA being obliged to make any control on their content. Anyone accessing this webpage and downloading or otherwise making use of any document, data or information found on this webpage shall do it on his/her own risks without any recourse against Dukascopy Bank SA in relation thereto or for any consequences arising to him/her or any third party from the use and/or reliance on any document, data or information found on this webpage.
 
 Post subject: Re: JFOREX SDK Tcp Socket Server save to file and read from file Post rating: 1   Post Posted: Sat 12 Apr, 2014, 14:19 
User avatar

User rating: 6
Joined: Tue 11 Feb, 2014, 11:38
Posts: 60
Location: PolandPoland
Tcp Server: save string to file
package breakermind;

import java.io.*;
import java.net.*;
public class MasterServer implements Runnable {
 
   // Path to directory "D:\\dir\\" or "/dir/path/"    
   private String Path = "/user/";
   
   private Socket con = null;
   public static char[] a = null;
   public static int n = 0;
   
   // SERVER COMMANDS
   public static String set = ".SET";
   public static String put = ".PUT";
   public static String end = ".END";
   
   public static void main(String[] args) {
      try {
         ServerSocket s = new ServerSocket(8888);
         printServerSocketInfo(s);
         while (true) {
            Socket c = s.accept();
            printSocketInfo(c);
            MasterServer v = new MasterServer(c);
            Thread t = new Thread(v);
            t.start();
         }
      } catch (IOException e) {
         System.out.println(e.toString());
      }
   }
   public MasterServer(Socket c){
      con = c;
   }
   @SuppressWarnings("resource")
public void run() {
      try {
        
         BufferedWriter w = new BufferedWriter(new OutputStreamWriter(con.getOutputStream()));
         BufferedReader r = new BufferedReader(new InputStreamReader(con.getInputStream()));
         String m = "Welcome to Breakermind Master Server.Send command...";
         w.write(m,0,m.length());
         w.newLine();
         w.flush();
     
         while ((m=r.readLine())!= null) {
//===============================================================================
// start catch command           
//===============================================================================   
            // get command from string   
            String command = m.substring(0, 4);
            System.out.println("Command : " + command);
            
            //====================================== Stop client when .END
             if (command == end) break;         
            
            if (command == set){     
                 //======================================= Set positions .SETPOSITIONS
                   System.out.println("Positions : " + m);
               BufferedWriter fout = new BufferedWriter(new FileWriter(Path + "positions.txt"));
                  fout.write(m);
                  fout.close();
                  m = "OK";
                a = m.toCharArray();
                n = a.length;
            }

            //====================================== Get positions .GET read from file      
            
            if (command == put){
                 BufferedReader inFile = null;
                 String stt;
                 inFile = new BufferedReader(new FileReader(Path + "positions.txt"));
                 while ((stt = inFile.readLine()) != null) {
                   System.out.println("LINE from Server file: " + stt);
                         a = stt.toCharArray();
                      n = a.length;
                      inFile.close();
                   break;
                }                    
            }
            
                a = m.toCharArray();
                n = a.length;

//===============================================================================
// send back         
//===============================================================================           
            w.write(a,0,n);
            w.newLine();
            w.flush();
         }
         w.close();
         r.close();
         con.close();
      } catch (Exception e) {
         System.out.println(e.toString());
      }
   }
   private static void printSocketInfo(Socket s) {
      System.out.println("Remote address = "+s.getInetAddress().toString());
      System.out.println("Remote port = "+s.getPort());
      System.out.println("Local socket address = "+s.getLocalSocketAddress().toString());
      System.out.println("Local address = "+s.getLocalAddress().toString());
      System.out.println("Local port = "+s.getLocalPort());
   }
   private static void printServerSocketInfo(ServerSocket s) {
      System.out.println("Server socker address = "+s.getInetAddress().toString());
      System.out.println("Server socker port = "+s.getLocalPort());
   }
}


 

Jump to:  

cron
  © 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