0

In my server I send to socket an infinite cycle of ("number: %d\n",i). And, at each cycle, I increase the number by one. I then use the following code to receive it in the android device (which is the TCP client): while(connected){

            //receive data through socket  
            BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
            Log.d(TAG, "1");

             while(connected){              

            //receive data through socket  
            BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
            Log.d(TAG, "1");

             while ( (line = in.readLine()) != null){

                   Log.d(TAG, line);
                   str.append(line);
                   Log.d(TAG, "2");

                   runOnUiThread(new Runnable() {

                    @Override
                    public void run() {

                        input.setText( str );

                }});

             }


        }

this is the result of the logcat:

r: 25363332´┐¢number: 3405er: 3839er: 4201er: 4925´┐¢number: 55776228´┐¢number: 6301´┐¢number: 7265´┐¢number: 8351number: 9170number: 9414number: 97765102101057 210934er: 11079er: 11441´┐¢number: 1173112020´┐¢number: 12093´┐¢number: 12455num ber: 12672er: 13491´┐¢number: 13903er: 14337er: 14699´┐¢number: 15351´┐¢number: 15713er: 1614716364´┐¢number: 1643716726´┐¢number: 16799number: 17016number: 173 787´┐¢number: 17885number: 187047´┐¢number: 19695er: 201291´┐¢number: 20419´┐¢nu mber: 20781´┐number: 21143er: 21215er: 2157721794´┐¢number: 21867er: 2230122518n umber: 22808er: 2362724446´┐¢number: 25125er: 2555926378727948´┐¢number: 28383er : 284557number: 28600286729number: 2896229396er: 295413´┐¢number: 30433number: 3

So this is not recognizing the \n, and between socket readings it is losing data. Is it because of the constant actualization of the UI thread?

How should I do this instead?

UPDATE

this is the server code in C:

 int main(int argc, char **argv)
 {

socklen_t client_len;
struct sockaddr_in cli_addr;
struct sockaddr_in serv_addr;
int client_count=0;
int nbytes, i, sockfd, new_sockfd;
char buf[20];

/* Init socket */   
sockfd=socket(AF_INET, SOCK_STREAM, 0);     
if (sockfd<0) error("ERROR opening socket");
bzero((char *) &serv_addr, sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = INADDR_ANY;
serv_addr.sin_port = htons( atoi(argv[1]) );

/*associate socket to the port*/
if (bind(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0) error("ERROR on binding");

/* connection to the client */
listen(sockfd, 1); /*server is ready to receive connections*/
client_len = sizeof(cli_addr);
new_sockfd = accept(sockfd, (struct sockaddr *) &cli_addr, &client_len); /*server accepts request */
client_count =1; /*write to socket when client is available*/   

i = 1;

while(1){


    sprintf(buf, "number: %d\n", i); 

    if (client_count >0){
        nbytes = write( new_sockfd, buf, sizeof(buf)); /* write to socket */
        if(nbytes<0){
            printf("ERROR on sendto");
        }
    }

    i = i+1;
}   

return (0); 

}

user2115240
  • 29
  • 2
  • 11
  • It does recognize the '\n' but java drops it since you are using readLine(). what is the server code and in what language it is ? – Mr.Me Mar 21 '13 at 17:38
  • are you creating a custom protocol? why not use already existing technologies like json/web services and such? I think you have to create a socket poller and poll this socket for data. some q/a here. http://www.coderanch.com/t/206964/sockets/java/polling-socket. this link also might be helpful http://stackoverflow.com/questions/11789407/socket-connections-and-polling-which-is-a-better-solution-in-terms-of-battery-l – Sergey Benner Mar 21 '13 at 17:46
  • I've edited the question with server code. It is in C. – user2115240 Mar 21 '13 at 18:10
  • write() returns the number of bytes written. The third parameter should be the length of the string. Check the return value being equal to the length of the send string. I see no loop in your client to receive the lines. I see two readlines() where you use only one. – greenapps Mar 21 '13 at 20:08
  • the code from the android is in a cycle. I forgot to add that. I also changed having two readlines to one readline, but still having the same problem. – user2115240 Mar 22 '13 at 10:14
  • oh my bad, I hadn't realized that I should have while( (line=in.readLine())!=null) . now I receive all data. Although it is not being presented on screen. should I have some kind of pause in thread in order to he program to be able to upload the data to UIthread? Thank you for your help! – user2115240 Mar 22 '13 at 10:58
  • Now post your correted code please. – greenapps Mar 22 '13 at 15:23
  • Ok I corrected the code. I've realized the BufferedReader does not recognize the line terminator. My intention would be, that when "in" receives number: %d\r\n he would stop read the buffer and continue in the code. Do you know how I could do this? – user2115240 Mar 22 '13 at 15:50
  • That happens already because you read line by line using readLine(). But that line comes without \n so you have to add it yourself like str.append(line + "\n"); Mr.Me already told you so. – greenapps Mar 22 '13 at 20:12

0 Answers0