jQuery in Action, 3rd edition
The moose likes Sockets and Internet Protocols and the fly likes Java program works in Windows but not in Mac Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Sockets and Internet Protocols
Bookmark "Java program works in Windows but not in Mac" Watch "Java program works in Windows but not in Mac" New topic

Java program works in Windows but not in Mac

abacaba obocobo

Joined: Aug 14, 2013
Posts: 2
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

Maxim Karvonen
Ranch Hand

Joined: Jun 14, 2013
Posts: 117
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.
Ivan Jozsef Balazs

Joined: May 22, 2012
Posts: 970

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 agree. Here's the link: http://aspose.com/file-tools
subject: Java program works in Windows but not in Mac
It's not a secret anymore!