Go to Post Could you explain this concept of a break from FIRST? Did my wife put you up to this? - Gary Dillard [more]
Home
Go Back   Chief Delphi > Technical > Programming > Java
CD-Media   CD-Spy  
portal register members calendar search Today's Posts Mark Forums Read FAQ rules

 
 
 
Thread Tools Rating: Thread Rating: 7 votes, 5.00 average. Display Modes
Prev Previous Post   Next Post Next
  #1   Spotlight this post!  
Unread 27-02-2014, 16:26
TheTurtleofDoom TheTurtleofDoom is offline
Registered User
FRC #2988
 
Join Date: Feb 2014
Location: Virginia
Posts: 4
TheTurtleofDoom is an unknown quantity at this point
Problem with socket servers

Hello, I am using roborealm to try and target the hotzone during autonomous, my sendsocket method or init mehtod is not working, I can not send the commands to the test socket server.

Driver-Station code
Code:
import java.io.FileReader;
import java.io.IOException;
import java.net.Socket;
import java.net.*;
import java.io.*;

/**
 *
 * @author rc10545
 */
public class Autonamous2014 {

    int[] Cog_X = new int[9999];
    //Creates the Cog_X array
    int cogi = 0;
    // controls Cog_X array
    Boolean shoot, moveRight, moveLeft, terminate;
    //controls what is passed to crio, and terminates the autonamus
    PrintWriter out;

    //Creates a variable that makess it easy to pass to Cro
    public static void main(String args[]) throws IOException {

        (new Autonamous2014()).runner();
        // creates a runner object so that we can have cross method  variables easily
    }// method main

    public void runner() throws IOException {
        int Cogx;
        // creates a variable that will become a simplified version of the int array"Cog_X"
        shoot = false;
        moveRight = false;
        moveLeft = false;
        terminate = false;
        // sets the conrol booleans to false
        init();
        // makes the connection to the crio, not in loop/sendsocket so there is only one connection attempt per ruin of the program
        while (terminate == false) {
            cog();
            //converts the text file into the int array "Cog_X" 
            Cogx = Cog_X[cogi];
            // simplifies the int array "Cog_X" to a single int varaible "Cogx"
            positioner(Cogx);
            // the logic, determines what the robot should do based off of its int variable "Cogx" position in pixels, -----WILL NEED UPDDATING TO WORK PROPERLY WITH THE GOOD CAMERA!!!!-----
            sendSocket();
            //passes the  commands to the Crio
        }// while loop

    }//method runner

    public void init() {
        byte[] ipAddr = new byte[]{10, 100, 72, 42};
        //the Ip address array of the Crio
        try {
            InetAddress addr = InetAddress.getByAddress(ipAddr);
            //converts the Ip address array into a usable IP adress
            Socket socket = new Socket(addr, 55000);
            //Tells it what socket to connect to on the Crio
            socket.setSoLinger(false, 0);
            //Tells it not to try to connect after the program has beeen closed down
            out = new PrintWriter(socket.getOutputStream(), true);
            //makes it so you can use the out.write("INSERT TEXT HERE"); command to pass info to the Crio
        }/**
         * try *
         */
        catch (IOException e) {
            System.out.println("Run the tcp server/Crio!");
            //if someone is using this wrong, tells them what they need to do in relatively simple language
        }//catch

    }// method init

    public void cog() throws IOException {

        FileReader fr = new FileReader("c:/cygwin64/home/rc10545/variables");
        // makes it so that the program can convert hte variable file into a char array; ---CHANGE IT WHEN THIS PROGRAM IS PUT ONTO THE DRIVERES STATION TO THE UPDATED FILE LOCATION!!!---
        char[] a = new char[9999999];
        //the char array that the file is read to
        int i;
        // contols the  string array "tokens"
        fr.read(a);
        //updates/reads the text to char array "a"
        String y = new String(a);
        //turns the char array "a" into string "y"
        String tokens[] = y.split("\r\n");
        //splits the string vARIABLE "Y" down on every line break into string array "tokens"
        i = tokens.length;
        //Gets a variable for how long the string array "tokens" is

        Integer convert = Integer.valueOf(tokens[i - 3]);
        // converts the latest item in string array "tokens" into the int variable "convert"

        Cog_X[cogi] = convert;
        // puts the int variable "convert" into int array "Cog_X" for bugfixing purposes, int array "Cog_X" could be replaced by int variable "convert" with no problems

    }//method cog

    public void positioner(int Cogx) {

        int remember = 0;
        //remembers wether it has done the initial movement

        if (remember == 0 && Cogx > 320) {
            moveRight = true;
            // sets the boolean variable "moveRight" to true, so in the sendSocket method it passess "goRight" to the crio
            remember++;
            // tells it that it has done the initial movement
        } else if (remember == 0) {
            moveLeft = true;
            // sets the boolean variable "moveLeft" to true, so in the sendSocket method it passess "goLeft" to the crio
            remember++;
            // tells it that it has done the initial movement
        }//does initial movement to bit right  of the center of hotzone

        if (remember == 2 && Cogx > 300 && Cogx < 330) {
            shoot = true;
            //tells it to shoot
        } else if (remember == 1) {
            remember++;
            //makes sure it does not shoot while moving
        }//shooter

        cogi++;
        //says that the latest index of int array "Cog_X" has been used
    }//method positioner

    public void sendSocket() throws IOException {

        out.write("cookie");
        //testing
        out.flush();
        // prevents ther from being a giant amount of text int the buffer by  force sending it to the Crio
        if (moveRight = true) {
            out.write("goRight");
            //tells the Crio to go right
        } else if (moveLeft == true) {
            out.write("goLeft");
            // tells the Crio to shoot
        } else if (shoot == true) {
            out.write("fire");
            // tells the Crio to shoot
            //terminate = true;
            // terminates the porgram, ---DISABLED FOR TESTING---         
        }

    }//socket writer

}//class; and nope not 150 lines of code ~75 lines of commenting
Server(slightly modified) code
Code:
package tcp_server;

import java.io.*;
import java.net.*;
import java.security.*;

/**
 *
 * Title: Sample Server Description: This utility will accept input from a
 * socket, posting back to the socket before closing the link. It is intended as
 * a template for coders to base servers on. Please report bugs to brad at
 * kieser.net Copyright: Copyright (c) 2002 Company: Kieser.net
 *
 * @author B. Kieser
 * @version 1.0
 *
 * Comprehensive style changes made by shall 12/20/12
 */
/*page --------------------------------------------------------------------- */
public class Tcp_server {

    private static int port = 55000;
    private static int maxConnections = 10;


    /* --------------------------------------------------------------------- */
    /*  LISTEN FOR INCOMING CONNECTIONS AND HANDLE THEM                      */
    /* --------------------------------------------------------------------- */
    public static void main(String[] args) {

        int i = 0;

        try {

            ServerSocket listener = new ServerSocket(port);
            Socket server;

            while ((i++ < maxConnections) || (maxConnections == 0)) {
                doComms connection;

                server = listener.accept();
                server.setSoLinger(false, 0);
                doComms conn_c = new doComms(server);
                System.out.println("GOT CONNECTION, CONNECTION COUNT [ " + i + " ]");
                Thread t = new Thread(conn_c);
                System.out.println("STARTING PROCESSING THREAD");
                t.start();

            }  /* while((i++ < maxConnections) || (maxConnections == 0)) */

        } catch (IOException ioe) {

            System.out.println("IOException on socket listen: " + ioe);
            ioe.printStackTrace();

        }  /* try / catch (ioe)*/

    }  /* main(String[] args) */

}  /* class Tcp_server */


/*page --------------------------------------------------------------------- */
class doComms implements Runnable {

    private Socket server;
    private String line;
    private String input;


    /*page ----------------------------------------------------------------- */
    doComms(Socket server) {

        this.server = server;

    }  /* doComms(Socket server) */


    /*page ----------------------------------------------------------------- */
    public void run() {

        input = "";

        try {

            /* ------------------------------------------------------------- */
            /*  GET INPUT FROM THE CLIENT                                    */
            /* ------------------------------------------------------------- */
            DataInputStream in = new DataInputStream(server.getInputStream());
            PrintStream out = new PrintStream(server.getOutputStream());

            while ((line = in.readLine()) != null && !line.equals(".")) {

                input = input + line;
                out.println("RECEIVED:" + line);

            }  /* while ((line = in.readLine()) != null && !line.equals(".")) */

            /* ------------------------------------------------------------- */
            /*  NOW WRITE TO THE CLIENT                                      */
            /* ------------------------------------------------------------- */
            System.out.println("FULL MESSAGE IS:" + input);
            out.println("OVERALL MESSAGE IS:" + input);

            //  server.close();
        } catch (IOException ioe) {

            System.out.println("IOException on socket listen: " + ioe);
            ioe.printStackTrace();

        }  /* try / catch (ioe) */

    }  /* run() */

}  /* class doComms */
There is no error message, but no data is passed to the socket server.
Reply With Quote
 


Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump


All times are GMT -5. The time now is 08:46.

The Chief Delphi Forums are sponsored by Innovation First International, Inc.


Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
Copyright © Chief Delphi