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.