Forums Register Login

Java program works in Windows but not in Mac

+Pie Number of slices to send: Send
Hi I have done a program that send a screen capture over a socket. Well, it works if I take the screen capture in the mac and send it to the windows pc(7, xp doest work), but if i want to take the screen capture in windows and send it to the mac it just send part of the file... Here is the client


And The server

+Pie Number of slices to send: Send
Can you clarify, which exaclty part of the file is sent? It is a first part or tail part or some other completely unrelated part?

Main problem with your code is mixing readers and byte input on a same stream and not using a buffered streams consistently. In server code you wraps socket.getInputStream into a buffered writer. That buffered writer may also consume part of the file into it's internal buffer (it is called buffered for a reason). And you server.client() method miss that initial file fragment already put in a buffer.

One of possible fixes. Use DataInputStream/DataOutputStream. Read/write commands using readUtf/writeUtf methods. Write a file length before a file content (so you know how many bytes to read on a server side). Actual format for a client->server screen command will be something like "sceenon <file-length> <file-content>". And stick to only one stream instance across your code. If you have created DataInputStream for a socket.getInputStream() then use only that DataInputStream to communicate with the other side.
+Pie Number of slices to send: Send


This builds a reader (using the platform default encoding - can you tell what it is?) around the input stream.
If the input stream happens to be a binary stream you can not assume its being a character stream using the platform default encoding!


Also in "copyScreen" you break the loop if less than 1024 bytes were read. Actually why?
I've read about this kind of thing at the checkout counter. That's where I met this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 2495 times.
Similar Threads
File Transfer via Sockets
Copying Multiple Files Over A Socket
Please help with BufferReader.read()
blocking a website
using byte array to copy contents of one file to another
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 29, 2024 07:05:47.