This one is easy. Remember the network code splits your big data block into a lot of small packets and sends each of them to the other side? These packets do not arrive all at the same time, so if you read at the other side, and a bunch arrives, the kernel gives you what it received so far, up to the length you requested. Normally, it is less than what you asked for,
After reading the InputStream.read description, and your buffer loop reader, it starts to make sense, but it's a few steps before you arrive there. The first thing what you must do is to stop using code that you do not fully understand. If you don't entirely understand your "read buffer" loop code, you're coding the higher levels on top of "I think it might do X", for some unknown value of X. Together with the inherently random nature of network IO wrt timing, speed, and bandwidth, you get what you experience, seemingly random behavior that works and then breaks again. The only way forward is to exactly know what X is. You do that by writing your own "read buffer" loop from the InputStream.read documentation. That way, you know the purpose of each and every statement, you know exactly what to expect from that low level function, and you can stack the higher level code on top of known functionality. So as homework, I'd suggest you write your own "read data into buffer" loop, using only the description of InputStream.read. Afterwards, compare notes, where does your implementation work differently from the $random website one? My guess is you'll do the "-1" case differently, in particular, after receiving all data that you need. The second thing you must do is to stop thinking in server-only and client-only. It seems to me you're coding a server, and you;re coding a client, then run them both and hope it works. In networking, failure and blocking can have 3 causes. A) Server code can be wrong, B) Client code can be wrong, C) Either side can get blocked or die or whatever, due to things that the other side does or does not do. The 3rd item is what is causing your blocking. To get a handle on this, you can draw the interactions between the server and the client with a message sequence chart
once again your assistance has helped solved this problem.
The key was (1) file-data come in smaller chunks (packets) and (2) fully Understanding the code and thus the need for two read().and current = bytesRead; line code. And if instead of 'current' it was named 'chunk', i would have understood it better
The knowledge of 1. made understanding the read()s code very simple. The first read() was to accept the first chunk that was then used to calculate the remaining bytes for the 3rd parameter of read(). And inside the do-while loop, bytesRead continues to add up to small chunks, which goes back to calculate the remainder length-size to be read until finished.
Once I knew this it wasn't to difficult to find where the problem was. the BufferedInputStream object -> bis wasn't closed. If the jpeg file was already saved, bis doesn't need to be closed. But if the server is writing the file at run time, it can't complete the write without bis closed. So once I closed bis, I was able to send the same file save at the server at run time. Even if it read the file in one big swoop bis would still need to be closed, but understanding the concept deeper was the key that made realise closing bis needed was fix.
ripoff gave a hint on this but joining the dots of these hints bit by bit to get a proper overview is not always possible for me
What still annoys me though is every time I try to make another step forward i always encounter a snag. Right now, I can send files from client to server, save, and send the same back to client. So in the server code, after sending a jpg file, i now also sent a text file with String data to client. But when I opened the text file at the client (on my android phone), it didn't contain the string-text. It opens well (which means it wasn't corrupted or something like that) but it just didn't have any string-text data in it that was in the original text file from server.
Its really annoying but this however is where i am stuck at again, why is the text-file empty when I open it at the client device?
My additional text file with string text.
This is the additional code, the rest is as in the code above (in post 3). Many thanks anyone who helps
Server code
...
...
File pathPlusfile = new File("C:\\rootFolde\\renderData.txt");
appendToFile( pathPlusfile, "This my crap string text here just for test", 31 );
//================== Send Data to Client ============================================
mybytearray = new byte [(int)pathPlusfile.length()];
fis = new FileInputStream(pathPlusfile);
bis = new BufferedInputStream(fis);
bis.read(mybytearray,0,mybytearray.length);
os = sock.getOutputStream();
//========================== send file to client ===================
os.write(mybytearray,0,mybytearray.length);
os.flush();
bis.close();
...
...
static BufferedWriter bufferedWriter = null;
public static void appendToFile( File myPathPlusFile, String S, int num ){
try{
bufferedWriter = new BufferedWriter(new FileWriter(myPathPlusFile, true));
bufferedWriter.write( S );
bufferedWriter.newLine();
bufferedWriter.append( S );
bufferedWriter.append( " " );
bufferedWriter.append( Integer.toString(num) );
bufferedWriter.newLine();
bufferedWriter.flush();
}
catch (IOException e){
e.printStackTrace();
}
closeFile( );
}
}
I don't mind posting questions where i am stuck at but when i get stuck at every single point I try to make progress it makes me look so dumb :wub:
Moving this to Networking. Please don't post in other forums just because one is quiet; it's not meant to be a real-time chat system but a categorised forum.
[Note: the stuff below is not written with a bad attitude or hash tone. Its just me giving my good and valid reason]
There is a very good reason for this
1. I agree this is a voluntary thing and not everyone can be here all the time, so my mentioning of this forum being quiet over the weekend wasn't a complain in any sense. Rather I appreciate the great assistance and help experienced people give those of us with less skills and experience
2. Because of my day job (weekdays) I have more time to work on my project during weekends than during weekdays. (I still have time during the week but i have more opportunity of doing marathon hours on my project at weekends). There are also lots of interruptions for me during weekdays
3. I had placed threads here before on Friday night and only one person replied to it, but I needed more than what they gave but I never had one till mid-monday, whereas there are lots of other users who would have helped but don't just visit this sub-forum that much.
And that makes a waste of my optimsed periods of weekends
Alberth who had been extremely helpful even mentioned it in the other thread that he doesn't visit here that much.
Thanks for your little note, networking forum is not visited regularly by me, so I missed the next problem
So I wasn't expecting a chatroom kind of conversation rather I was giving more people who can help the opportunity to see my thread at the time when I am also able to work on my project and their advice the most
4. More so the thread can still be classified as programming in general, so it doesn't break anything by placing this under general programming. Its not as wild as placing a game design question, Lounge question, visual arts question or business and law question in the general programming forum
5. Other mods often see my client server question there a few times and have been ok with it.
6. The advantage of forums is the different global time zones and this means when one is sleeping others can be awake It. It is also a fact that the ratio of visits during the weekends of GP forum to here is probably roughly 10:1 (where 1 here means one or two people) thus it will be counter productive for a weekend project-person to stick here because of the mild deviation between general programming and network programming
7. And I forgot to mention that you didn't even put the suitcase moved-post icon there, This thread just "disappears". So maybe other people I'm trying to reply to wouldn't even see this post
So in conclusion my need to place networking code threads in GP forum is, - I want to maximise (and not waste) my weekends. not that i was expecting a chat service
(I'm mostly a weekend project person [though i also have a lot of interrupted times during the week] and I gained a lot this past weekend and don't want to miss that in future because of this kind of bureaucracy. That's why I've gone at length to make this case. But it is all with respect, not personal)