Sunday, January 10, 2016

android socket sending big picture, data loss ... seek solutions. .




            


Now send pictures between the terminal, send pictures similar between micro letter as a client of the image into a byte stream sent to the server, then the server to find people you want to send the data to the additional terminal equipment. Now send two simulators on the LAN big picture, send and receive no problems, deployed to the external network server, the server receives the code: int length = 0;
int totalNum = 0;
byte [] buffer = new byte [1024];
while ((length = dis.readInt ())! = 0) {
length = dis.read (buffer, 0, length);
System.out.println ("length: -------- & gt;" + length);

totalNum + = length;
out.writeInt (length);
out.write (buffer, 0, length);
out.flush ();
}
System.out.println ("totalNum: -------- & gt;" + totalNum);
out.writeInt (0);
out.flush ();
Debug.info ("totalNum ::::" + totalNum);
initService.getEnterpriseMsgService () save (msg);.
Each received 1024 bytes, sometimes to System.out.println ("length: -------- & gt;" + length); ---- Direct is null, the simulator each time you send and the received data is the same, there is no problem to send the big picture. Members Great God, seeking solutions to the problem code, or server problems (a PC company, it is the kind of card). . .


Reply:
Communication with the socket
Reply:
socket packet loss is easy, why do not you put this image into the server, the client http request it
Reply:
Should be the code in question, there is no use of TCP socket number noted loss problems in theory. Usually there is such a misunderstanding, the sender 1k1k hair, recipient 1k1k income, assuming that a 1k packet is transmitted and received by packet; when crowded network poke, the recipient receives a response is not necessarily a 1k; may be 800; and another 200 in the next response; so the application of the concept of flow to programming, rather than the concept of using the packet;
Reply:
Directly own the underlying socket transfer requires a good design, you might as well pass this directly with the http protocol upper simpler
Reply:
quote the second floor lieri111 reply:
socket packet loss is easy, why do not you put this picture on the server, the client http requests it

I was on the phone inside the selected album with pictures you sent to a cell phone. . .
Reply:
You can use HTTP server ah, can refer NanoHttpD http server, the IP address of the machine + sdcard pictures to send to your chosen user (socket), and then direct the user to get through this url, it will be better.
Reply:
quote 3rd floor jialiry reply:
should be the code in question, there is no use of TCP socket number noted loss problems in theory. Usually there is such a misunderstanding, the sender 1k1k hair, recipient 1k1k income, assuming that a 1k packet is transmitted and received by packet; when crowded network poke, the recipient receives a response is not necessarily a 1k; may be 800; and another 200 in the next response; so the concept of application flows to programming, rather than the concept of using the package;

Indeed pure in this issue are not always received 1024 bytes, sometimes in multiple receive. The concept of programming with the flow? what do you mean! Please advise. .
Reply:
3rd floor, a good explanation, I have also experienced similar problems, if this is received 800, mark it, next time will receive only 224 received
Reply:
quote landlord Agoni_tt reply:
now send pictures between the terminal, send pictures between micro-channel similar as the client to send pictures into a byte stream to the server, then the server to find people you want to send the data to the additional terminal equipment. Now send two simulators on the LAN big picture, send and receive no problems, deployed to the external network server, the server receives the code: int length = 0;
int totalNum = 0;
byte [] buffer = new byte [1024];
......


I know the reason, I have also experienced ,,,,

1024 too, if the picture is less than 1024 direct you to read the back of the. . . .

size is file size. . . .


 

byte [] buffer;
if (size & gt; 1024)
buffer = new byte [1024];
else
buffer = new byte [size];

Reply:
Explain the concept of programming is not very good flow, only socket communication is concerned, you can put it as a black box, one end of the inflow, outflow and the other end, the number noted in the analysis, communication between the two sides complete response at a higher level, including a statement , also independent of the socket of a higher level. Noted in the number of online games such as packages, are usually small, a few dozen bytes, two package graded writing once attended a recipient can not be expected to read and write operations on the socket number you noted in the analysis, the two sides of the communication response What help. Another reader socket buffer size limit by its hardware; bag big amazing decomposition 1k2k communication between computers are no problem, the phone's hardware buffer how much I did not know, know people say it, exchange communicate with.
Reply:
Communication with the socket; himself should first make some agreement; for example the first 4 bytes define the size of the package; then 2 bytes define the contents of the package, such as 0 for reply, 1 file, 2 chat, and so on; according to the following package The content definition number noted in the structure, such as send pictures, with 256 bytes representing the file name, extension 8 bytes; followed by the image content. When received, first read the analysis of the first four bytes, determine the size of the package, and then they have been read, read the full package, further analysis and processing. Is a socket like water sample to flow out, you cut down a section of the analysis process.
Reply:
Thank you for your answer, I posted about the process of code generally, we look at, give suggestions
 
The client sends:

private void readFileSendData (String filePath)
throws FileNotFoundException, IOException {
Log.i (TAG, "readFileSendData filePath =" + filePath);
DataInputStream ddis = new DataInputStream (
new FileInputStream (filePath));
Log.i (TAG, "ddis =" + ddis);
int length = 0;
int totalNum = 0;
byte [] buffer = new byte [1024]; // uploaded every size
Log.i (TAG, "img.avaliable =" + ddis.available ());

while ((length = ddis.read (buffer))! = -1) {
totalNum + = length;
Log.i (TAG, "length =" + length);
dos.writeInt (length);
dos.write (buffer, 0, length);
dos.flush ();
}

dos.writeInt (0);
dos.flush ();

if (ddis! = null) {
ddis.close ();
ddis = null;
}
Log.i (TAG, "readFileSendData (): send bytes =" + totalNum);
}
-------------------

Server transfer:
SessionSocket sessionSocket = getSessions (receiveId);
if (null! = sessionSocket) {
Socket socket = sessionSocket.getSocket ();
Debug.info ("found each other");
// SaveMessage (message, null);
DataOutputStream out = new DataOutputStream (socket
.getOutputStream ());
out.writeInt (procotol);
out.writeInt (sendId);
out.writeInt (receiveId);
out.writeUTF (TimeUtil.getAbsoluteTime ());
out.flush ();

int length = 0;
int totalNum = 0;
byte [] buffer = new byte [1024];
while ((length = dis.readInt ())! = 0) {
length = dis.read (buffer, 0, length);
System.out.println ("length: -------- & gt;" + length);
totalNum + = length;
out.writeInt (length);
out.write (buffer, 0, length);
out.flush ();
}

out.writeInt (0);
out.flush ();
Debug.info ("totalNum ::::" + totalNum);
initService.getEnterpriseMsgService () save (msg);.

} Else {// save the information to the database
Debug.info ("not found each other");
// SaveMessage (message, 1);
initService.getEnterpriseMsgService () save (msg);.

}
} Catch (IOException e) {
e.printStackTrace ();
}

-------------------
The client receives:

private void receiveDataWriteFile (String filePath)
throws FileNotFoundException, IOException {
Log.i (TAG, "receiveDataWriteFile (): filePath =" + filePath);
DataOutputStream ddos = new DataOutputStream (new FileOutputStream (
filePath)); // voice or image write local SD card
Log.i (TAG, "receiveDataWriteFile (): ddos =" + ddos);
int length = 0;
int totalNum = 0;
byte [] buffer = new byte [1024];
Log.i (TAG, "receiveDataWriteFile () to begin receiving data ...");
while ((length = dis.readInt ())! = 0) {
Log.i ("receiveDataWriteFile ()", "length:" + length);
length = dis.read (buffer, 0, length);
totalNum + = length;
ddos.write (buffer, 0, length);
ddos.flush ();
}

if (ddos! = null) {
try {
ddos.close ();
ddos = null;
} Catch (IOException e) {
e.printStackTrace ();
}
}

Log.i (TAG, "receiveDataWriteFile (): receive bytes =" + totalNum);
}





Reply:
quote 9th floor KPRF2009 reply:
quote landlord Agoni_tt reply: send a picture now is between the terminal, send pictures between micro-channel similar as the client image into a byte stream sent to the server, then the server to find people you want to send the data to the additional terminal equipment. Now send two simulators on the LAN big picture, send and receive no problems, deployed to the external network server, the server receives the code: int length = 0;
int totalNum = 0;
byte [] buffer ......

Well this is a question which has been added to the code, but not the cause of the problem seems. . .
Reply:
LAN transmission between two simulator no matter how big or small picture image is no problem, there is a problem outside the Internet,
Reply:
Still doing it; length = dis.read (buffer, 0, length); then want to read and actually read do not necessarily match the number noted; you try to change the parameter name printed
.In addition, the middle you insert int little effect, and may go inside to read buffer is not recommended.
 total length of the sender first pass in the past, and has been sent 
totalNum = file length;
dos.writeInt (totalNum);
while (totalNum & gt; 0) {
length = ddis.read (buffer);
dos.write (buffer, 0, length);
dos.flush ();
totalNum - = length;
}
After receiving end to determine the total length, it has been read until the entire file transfer ends.
totalNum = dis.readInt ();
while (totalNum & gt; 0) {
length = dis.read (buffer, 0, 1024);
if (length & gt; 0)
{
ddos.write (buffer, 0, length);
ddos.flush ();
totalNum - = length;
}
}

Reply:
quote 15th floor jialiry reply:
or doing it; length = dis.read (buffer, 0, length); then want to read and actually read It does not necessarily match the number noted; you try to change the parameter name printed
.In addition, the middle you insert int little effect, and may go inside to read buffer is not recommended.
Java code? 1234567891011121314151617181920 total length of the sender first pass in the past, and has been sent to totalNum = text ......

Blessings beginning is out.writeInt (0); as a sign of the end judgment, passed several useless value, resulting buffer inconsistencies. if sent to another terminal device file is large (10 megabytes), through this transmission mode the pressure is quite large. .
Reply:
Oh, to solve the like.
I'm just learning Java, because in the past to do C ++, on the socket Jiaoshu, could not help but interrupt. In fact, Java's DataOutputStream and other types of socket has been packaged with the concept of a flow, I also talk about the concept of programming with the flow, ashamed. < img alt = "" src = "http://forum.csdn.net/PointForum/ui/scripts/csdn/Plugin/001/face/10.gif" />
Reply:
Code written in a very strange, while ((length = dis.readInt ())! = 0) sentence led to a lot of uncertainty.
Because you do not just come for the data that your head may just start position is in the picture data in a certain period of four byte 0 places, so this time we will not continue normal execution.

If you want to 1K, 1K to forward data from one end to the other end, all without so write. On the server side can, by identifying variables to record it. For example
int blockLength = 0;
int buffLength = 1024;
int receivedLength = 0;
byte [] buffer = new byte [buffLength];
int startPos = 0;
while ((length = dis.read (buffer, startPos, buffLength-startPos)) & gt; 0) {
if (startPos + length == buffLength) {
// A block is full, send it to client
startPos = 0; // reset startPos
}
else {
startPos + = length;
}
}
Reply:
This simple, direct the picture into a string using Base64 and then sent, then the server re-decoded on the line
Reply:
To send and receive images, you must increase the buffer size.
 byte [] buffer = new byte [4096]; 


Try this method:
 InputStream is // your InputStream 
OutputStream out // your OutputStream
byte [] buffer = new byte [1024];
int length = 0;
try {
while ((length = is.read (buffer)) & gt; 0) {
out.write (buffer, 0, length);
}

} Catch (Exception e) {
// TODO: handle exception
}










Reply:

Reply:

Reply:
Learn. . . . . . .

No comments:

Post a Comment