user13760: 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?

user14501: 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

    }
}


13760: <p>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?</p>

<pre class="lang-c prettyprint-override"><code>for(int i = 0; i &lt; data.length(); i++)
</code></pre>

<p>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.</p>

<p>EDIT: Alright, I read <code>client.read()</code> documentation, so the problem is at arduino.</p>

<p>This is my java code:</p>

<pre class="lang-java prettyprint-override"><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();
}
}
</code></pre>

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

<pre class="lang-c prettyprint-override"><code>char c = client.read()
</code></pre>

<p>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?</p>
 14501: <p>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.</p>

<pre><code>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></pre>

<p>//Code is not tested!!!</p>

<p>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)</p>

<p>EDIT:</p>

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

    }
}
</code></pre>
