i'm creating a client(android)/server(java) program and the problem is everything works in LAN but when i put my jar on server it gets stuck on creating the ObjectInputStream object.
here is client piece:
oos = new ObjectOutputStream(s.getOutputStream());
oos.flush();
ois = new ObjectInputStream(s.getInputStream());
and the server:
oos = new ObjectOutputStream(socket.getOutputStream());
oos.flush();
ois = new ObjectInputStream(socket.getInputStream());
so both are equal.
i also tried without the flushes and also switching the ObjectOutputStream creation with the ObjectInputStream in the client.
everything works in LAN. seems pretty logical. but not over internet.
thanks!
Related
I have this client code
dOut = new DataOutputStream(socket.getOutputStream());
oos = new ObjectOutputStream(socket.getOutputStream());
dOut.writeByte(2); <--when readByte on server gives -84
oos.writeObject(rectangle);
if slightly changed
dOut = new DataOutputStream(socket.getOutputStream());
dOut.writeByte(2); <--when readByte on server gives 2
oos = new ObjectOutputStream(socket.getOutputStream());
oos.writeObject(rectangle);
why is this happening? case is similar in inputstream as well.
Yes. The data will get hopelessly mixed up.
There's no need for this. ObjectOutputStream already has all the methods of DataOutputStream. You don't need them both.
I'm trying to work on multiple AVD in Android and sending data between them using Sockets.
Serverside code-snippet:
ServerSocket ss = new ServerSocket(10000);
Log.v("ReceiverTask", "Receiver waiting for requests");
connectedSocket = ss.accept();
ObjectInputStream ois = new ObjectInputStream(connectedSocket.getInputStream());
Object obj = ois.readObject();
ois.close();
ss.close();
Client side code:
Socket socket = new Socket(InetAddress.getByAddress(new byte[]{10, 0, 2, 2}),njr.sendTo());
BufferedOutputStream bos = new BufferedOutputStream(socket.getOutputStream());
ObjectOutputStream oos = new ObjectOutputStream(bos);
oos.writeObject(njr);
Log.d("Client","Object send successfull");
oos.flush();
bos.flush();
oos.reset();
oos.close();
bos.close();
socket.close();
The problem is that The objects that are sent from one AVD (as given by log) is not being received at other AVD. This happens sometimes and not always at a same point. Any hints as to what could be the problem???
use oos.flush() at the end in client.
socket = new Socket(InetAddress.getByAddress(new byte[]{10, 0, 2, 2}),njr.sendTo());
bos = new BufferedOutputStream(socket.getOutputStream());
oos = new ObjectOutputStream(bos);
oos.writeObject(njr);
oos.flush();
oos.close();
Log.d("Client","Object send successfull");
Close the stream after reading, else it will assume that its still open
oos.Close();
For some reasons i have to set up on android SocketServer which waits for connection with computer. All is going good, socket with client (computer) is creating, but streams don't open. It's just pausing without any error or message.
client:
s = new Socket(ip, 4567);
ois = new ObjectInputStream(s.getInputStream());
System.out.println("ois..");// not showing, so can't open input stream
oos = new ObjectOutputStream(s.getOutputStream());
System.out.println("oos.."); // same here
server:
socket = new ServerSocket(4567);
System.out.println("Waiting for connection,,"); // showing
client = socket.accept();
System.out.println("Connected"); //showing
ois = new ObjectInputStream(client.getInputStream());
System.out.println("ois.."); // not showing
oos = new ObjectOutputStream(client.getOutputStream());
System.out.println("oos.."); // not showing too
System.out.println("Stream,s opened");
My apk has INTERNET premissions. I'm using 4567 port. Any other application doesn't block the port.
What can be wrong?
Try opening the ObjectOutputStream first in your server.
socket = new ServerSocket(4567);
System.out.println("Waiting for connection,,"); // showing
client = socket.accept();
System.out.println("Connected"); //showing
oos = new ObjectOutputStream(client.getOutputStream());
ois = new ObjectInputStream(client.getInputStream());
System.out.println("Stream,s opened");
I don't see any timeouts in there, which is why it just stops.
There's probably some network problem; have you verified the IP address of the device is correct?
It's unlikely, but there could be some firewall rule blocking the connection.
I am having trouble bouncing object between the client and the server.
Create an object. Update some fields. Send to Server. (this part works)
SomeObject thisObject = new SomeObject();
thisObject.setSomeValue(13); // update object to be sent
PrintStream toServer = new PrintStream(sock.getOutputStream());
ObjectOutputStream oos = new ObjectOutputStream(toServer);
oos.writeObject(thisObject);
oos.close();
toServer.println(oos); // send object to server
toServer.flush();
Right after this, server further updates some value and sets it to 1919;
ObjectInputStream objFromClient = new ObjectInputStream(new BufferedInputStream(
sock.getInputStream()));
Served thisObject = (Served) objFromClient.readObject();
thisObject.setSomeValue(1919);
Server then sends the object back to client
toClient = new PrintStream(sock.getOutputStream());
ObjectOutputStream oos = new ObjectOutputStream(toClient);
oos.writeObject(thisObject);
oos.close();
objFromClient.close();
sock.close();
But when the time comes to pick up the object back on the client side .. programs fails with Socket Closed exception
ObjectInputStream objFromServer = new ObjectInputStream(
new BufferedInputStream(sock.getInputStream())); //java.net.SocketException: Socket is closed
thisObject = (Served) objFromServer.readObject();
....
Please help me understand the issue
My guess is you're using the same Socket to both send and receive from the client. When you close the ObjectOutputStream on the client, this closes the underlying OutputStream, which closes sock. Then, when you try to reuse it below, it's been closed and throws an exception.
Instead, wait for the transaction to finish before closing your resources in the client code (which should be done in a finally block by the way). Or, if waiting is problematic, use a new Socket instead.
This is the code that i used on client socket
Socket connected = Server.accept();
ObjectOutputStream oos = new ObjectOutputStream(connected.getOutputStream());
oos.writeObject(dPFPSample.serialize());
This the code that i used on server socket
Socket clientSocket = new Socket("localhost", 5002);
ObjectInputStream ois = new ObjectInputStream(clientSocket.getInputStream());
DPFPSample dpfpSample = (DPFPSample) ois.readObject();
i got an error java.lang.ClassCastException exception on ois.readObject() line
I would assume that your DPFPSamle.serialize() returns something different then DPFPSamle. I'd say you don't need to call any serialization method. The stream will handle it. So just call writeObject(dPFPSample)
In order for this to work your class (the one you are trying to send - i.e. DPFPSample) must implement the java.io.Serializable interface.