0

i'm trying to write a program which uses sockets to send and receive an image. The Java source should run on android devices. But the class TCP also works on normal java applications. To be hornest it is just a feature of my whole program.

First of all i want to share my source codes: Java : Async (to use it on newer Versions of android >5.**)

class  Async extends AsyncTask<String, TCPClient, String> {

@Override
protected String doInBackground(String... args) {
    TCPClient cp = new TCPClient();
    try {
        if (args[1].equals("remote"))
            cp.TCPSend(args[0]);
        else {
             cp.TCPrecv();
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
    return "";
}

}

TCP Client:

class TCPClient
{
public void TCPSend(String sentence) throws IOException
{
    String modifiedSentence;
    Socket clientSocket = new Socket("192.168.0.14", 1334);
    DataOutputStream outToServer = new DataOutputStream(clientSocket.getOutputStream());
    BufferedReader inFromServer = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
    outToServer.writeBytes(sentence + '\n');
    modifiedSentence = inFromServer.readLine();
    System.out.println("FROM SERVER: " + modifiedSentence);
    clientSocket.close();
}
public Bitmap TCPrecv() throws IOException
{
    Socket clientSocket = new Socket("192.168.0.14", 1331);
    DataInputStream dis;
    dis = new DataInputStream(clientSocket.getInputStream());
    BufferedReader inFromServer = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
    String size_pic = inFromServer.readLine();
    int bytesize=Integer.parseInt(size_pic);
    int bytesRead=0;
    byte[] pic = new byte[bytesize];
    int read=0;
    while(read <  bytesize && !(bytesRead ==-1)) {
        bytesRead = dis.read(pic, 0, 10239);
        read += bytesRead;
    }
   return BitmapFactory.decodeByteArray(pic, 0, pic.length);
}
}

This programm reads a image as a ByteArray. It works for 36/37 packets. Don't matter which size of packets i use. The last packet get lost.

I write some own codes of C Servers but the best code i found on this forum.

The Code snipe contains client and server functions and it works perfect.

But the connection of the the Java Programm works not for all packets.

C Code :

void error(const char *msg)
{
perror(msg);
exit(1);
}
int send_image(int socket){

FILE *picture;
int size, read_size, stat, packet_index;
char send_buffer[10240], read_buffer[256];
packet_index = 1;

picture = fopen("../MR.PNG", "r");
printf("Getting Picture Size\n");

if(picture == NULL) {
    printf("Error Opening Image File"); }

fseek(picture, 0, SEEK_END);
size = ftell(picture);
fseek(picture, 0, SEEK_SET);
printf("Total Picture size: %i\n",size);

//Send Picture Size
printf("Sending Picture Size\n");
char *ptr=(char *) malloc(500);
sprintf(ptr,"%d",size);
strcat(ptr,"\n");
 write(socket,ptr,20);

 //Send Picture as Byte Array
 printf("Sending Picture as Byte Array\n");

/* do { //Read while we get errors that are due to signals.
  stat=read(socket, &read_buffer , 255);
  printf("Bytes read: %i\n",stat);
 } while (stat < 0);*/

printf("Received data in socket\n");
printf("Socket data: %c\n", read_buffer);

while(!feof(picture)) {
//while(packet_index = 1){
  //Read from the file into our send buffer
  read_size = fread(send_buffer, 1, sizeof(send_buffer)-1, picture);

  //Send data through our socket
  do{
    stat = write(socket, send_buffer, read_size);
  }while (stat < 0);

  printf("Packet Number: %i\n",packet_index);
  printf("Packet Size Sent: %i\n",read_size);
  printf(" \n");
  printf(" \n");


  packet_index++;

  //Zero out our send buffer
 bzero(send_buffer, sizeof(send_buffer));
int send_image(int socket){

FILE *picture;
int size, read_size, stat, packet_index;
char send_buffer[10240], read_buffer[256];
packet_index = 1;

picture = fopen("../MR.PNG", "r");
printf("Getting Picture Size\n");

if(picture == NULL) {
    printf("Error Opening Image File"); }

 fseek(picture, 0, SEEK_END);
 size = ftell(picture);
 fseek(picture, 0, SEEK_SET);
 printf("Total Picture size: %i\n",size);

 //Send Picture Size
printf("Sending Picture Size\n");
 write(socket, (void *)&size, sizeof(int));

 //Send Picture as Byte Array
 printf("Sending Picture as Byte Array\n");

  do { //Read while we get errors that are due to signals.
  stat=read(socket, &read_buffer , 255);
   printf("Bytes read: %i\n",stat);
 } while (stat < 0);

 printf("Received data in socket\n");
printf("Socket data: %c\n", read_buffer);

 while(!feof(picture)) {
//while(packet_index = 1){
  //Read from the file into our send buffer
  read_size = fread(send_buffer, 1, sizeof(send_buffer)-1, picture);
 //strcat(send_buffer,"\n");
  //Send data through our socket
  do{

    stat = write(socket, send_buffer, read_size);
  }while (stat < 0);

  printf("Packet Number: %i\n",packet_index);
  printf("Packet Size Sent: %i\n",read_size);
  printf(" \n");
  printf(" \n");


  packet_index++;

  //Zero out our send buffer
  bzero(send_buffer, sizeof(send_buffer));
 }
}
 }
}

int main(int argc, char *argv[])
{
 int sockfd, newsockfd, portno;
 socklen_t clilen;
 char buffer[256];
    char contentbuffer[255];
 struct sockaddr_in serv_addr, cli_addr;
 int n;
 sockfd = socket(AF_INET, SOCK_STREAM, 0);
 if (sockfd < 0)
    error("ERROR opening socket");
 bzero((char *) &serv_addr, sizeof(serv_addr));
 portno = 1331;
 serv_addr.sin_family = AF_INET;
 serv_addr.sin_addr.s_addr = INADDR_ANY;
 serv_addr.sin_port = htons(portno);
 if (bind(sockfd, (struct sockaddr *) &serv_addr,
          sizeof(serv_addr)) < 0)
          error("ERROR on binding");
 listen(sockfd,5);
 clilen = sizeof(cli_addr);
while(1)
{
 newsockfd = accept(sockfd,
             (struct sockaddr *) &cli_addr,
             &clilen);
 if (newsockfd < 0)
      error("ERROR on accept");
    int i=0;
    send_image(newsockfd);
    }
 close(newsockfd);
 close(sockfd);
 return 0;
}

I dont understand why this code 2 times works without problems. But without any changes the last bytes get lost... :-/

Sorry, my english is very badly. But i realy need help. It is so frustrating to debug an program without the knowlege to understand what the problem is.

Sorry again. I have trouble with the code snipes system. It want work on Class TCP Client.

Thank for your attention.

MFG Neks

Edit: After some comments i'm modified my Java Code. Now the same code works on a real Java Programm with Java EE IDE. This program works fine. But the same code want work on Android. I don't understand why. I can post pics of the debugging sessions.

package com.vib;

import java.io.; import java.net.;

class NewTcpClient {

@SuppressWarnings("resource")
public void TCPrecv() throws IOException {
    Socket clientSocket = new Socket("192.168.0.14", 1331);
    DataInputStream dis;
    dis = new DataInputStream(clientSocket.getInputStream());
    BufferedReader inFromServer = new BufferedReader(new InputStreamReader(
            clientSocket.getInputStream()));
    String size_pic = inFromServer.readLine();

    int bytesize = Integer.parseInt(size_pic);
    int bytesizeread = 10239;
    int bytesRead = 0;
    byte[] pic = new byte[bytesize];
    int read = 0;
    int count=0;
    while (read < bytesize)
    {   
        bytesRead = dis.read(pic, 0, bytesizeread);
        read += bytesRead;
        count++;

    }

clientSocket.close(); System.out.println(pic);

}

public static void main(String argv[]) throws Exception {
    NewTcpClient tcp = new NewTcpClient();
    tcp.TCPrecv();
}

}

I don't understand whats going on... i uses this programm on normal IDE now for 3 houres... now it want work... without any changes. How can i debugg something so mystil? ^^

After a break of 2 hours the program works fine. What can be a reason for this bug.

  • You're ignoring the end of stream condition. – user207421 Jun 24 '16 at 09:43
  • If i use while (dis.available() >1) { bytesRead = dis.read(pic, 0, bytesizeread);} the Program stop receiving before all datas are received. – Nekromencer Jun 24 '16 at 10:01
  • 1
    Do not use `available`. It does not what you expect. It returns an **estimated** count of bytes that can be read without blocking .. so mostly it's pretty useless. – Fildor Jun 24 '16 at 10:02
  • What is the better choice? A EOF construct like http://stackoverflow.com/questions/16945335/java-need-a-while-loop-to-reach-eof-i-e-while-eof-keep-parsing this? – Nekromencer Jun 24 '16 at 10:03
  • 1
    I see you use more than one input stream on the socket. You should not do that (dis and inFromServer). Also you destroy your pic by always writing from offset 0. – Fildor Jun 24 '16 at 10:08
  • On a real Java Programm the Programm works with while (read < bytesize) { bytesRead = dis.read(pic, 0, bytesizeread); read += bytesRead; count++; } :-) Thank you both ... oh i dont know is the image okey curropt – Nekromencer Jun 24 '16 at 10:12
  • 1
    `bytesRead = dis.read(pic, 0, 10239); read += bytesRead;` When EOF is reached, there will be a bytesRead = -1. You ignore that and add it to `read` ... So `read` will always be 1 less than actually read ... – Fildor Jun 24 '16 at 10:12
  • InFrom Server just receives the Information about the size. Is this realy a fault? The infromServer has no communication to dis and is already finished when i use dis. Or? – Nekromencer Jun 24 '16 at 10:15
  • Damn i'm so stupid. You are right. i added a if(bytesRead>0) befor read+=*** – Nekromencer Jun 24 '16 at 10:26
  • The C server implementation you found is blocking, slow connections can cause DoS, it keeps opening the same file over and over again and it really isn't that great for production... You really should consider using a library, maybe an HTTP server with static file service (like nginx/apache) instead of writing your own. This will allow you to focus on what you need to do instead of getting busy learning about server architecture design, file handle caching and other issues. – Myst Jun 24 '16 at 21:34
  • Okey that could be my problem. But my problem is that i learn C at this moment and never used something like this. What does this liberary replace? The connection to the server? On start of c programming i use a lot of connections which sends http requests like GET /index.html ... and so. Does my program this kind of communication needs after implementing? I'm a student and learned a lot of about server architechtures. If i could realize this programm without some other specific libs i were happy. Can you explain how to change my server system to fix this issue? I'm trying to fix the read issu – Nekromencer Jun 25 '16 at 10:33
  • I checked my code and realized that it is right. The programm should read in later versions more than one Image. That's the reason why he is reading the same image in every iteration. The path will replaced with a variable. – Nekromencer Jun 25 '16 at 10:53

0 Answers0