0

I'm trying to send short strings via Java client to Arduino server and vice versa. Sending data to client goes fine, but when I try to send data to arduino it cuts the string into bytes and then sends the first letter. Will the following work?

for(int i = 0; i < data.length(); i++)

Even if it can be sent as characters I want to send it in one piece, because strings are very short so there should be no problems with length.

EDIT: Alright, I read client.read() documentation, so the problem is at arduino.

This is my java code:

public class TCP {
static Socket clientSocket;

public static void main(String args[]) throws Exception {
    clientSocket = new Socket("10.10.10.57", 5000); 
    sendToServer("HAI THERE\n");
    String msg = getFromServer();
    System.out.println(msg);
    close();    
}

static void sendToServer(String msg) throws Exception {
    //create output stream attached to socket
    PrintWriter outToServer = new PrintWriter(new OutputStreamWriter(clientSocket.getOutputStream()));
    //send msg to server
    outToServer.print(msg + '\n');
    outToServer.flush();
}

static String getFromServer() throws Exception{
    //create input stream attached to socket
    InputStreamReader isr = new InputStreamReader(clientSocket.getInputStream());
    BufferedReader inFromServer = new BufferedReader(isr);
    //read line from server
    String res = inFromServer.readLine(); // if connection closes on server end, this throws java.net.SocketException 
    return res;
}

static void close() throws IOException{
    clientSocket.close();
}
}

This code sends only one byte at the same time.It might be problem in my arduino code, since I read it with

char c = client.read()

One more thing. If there is any java master, I would like to know what to do with exceptions. How should I handle disconnected socket, failed connection to server, and so forth?

2

The problem is, that "char" is just one character. So you have to build your return out of the chars... if you want to use the "client.read()" function.

String res = ""; //this will be the resived msg after the loop
do {
   char c = client.read(); //c will be -1 if no byte is left
   if(c!=-1) 
       strcat(res, String(c)); //Append the char to the result string
} while(c!=-1);

//Code is not tested!!!

To your java problem... just put your code parts into try catchs not whole functions (remove it for the function and add it on the points eclipse tell you to). So you can react to each error individually. Then put client stuff in a own thread so it not blocks any other stuff. Every disconnect or faild connection will throw an exception if you dont catch it with an if. So you have to handel it their (try to reconnect or something)

EDIT:

static void close() {
    try {
        client.close();
    } catch (IOException e) {
        system.out.println(e.toString()); //Print the error or do something else

    }
}
  • great, thank you, it should probably work! soooo, for example: static void close() throws IOException, I delete IOException, and do it in main method? – Rok Dolinar Nov 27 '15 at 17:27
  • I put some example code in my answer above. – Cracker0dks Nov 27 '15 at 18:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.