Is there any way possible to extract data after writing to outputstream? - java

I am trying to create several sockets and connecting it with a single client using socket programming. Every time a client tries to communicate with server a new socket gets created and messages can be sent but the problem comes when i try to pass more than one message through server. the first message gets sent and the for the rest, i face stream corrupted exceptions.Following is my code for the class that tries to read object from output stream.

Related

Java won't deserialize object after it is sent through socket stream

I'm trying to create application that will send object through local network using Sockets. When i run server and client code in Intellij Idea they work fine, but when i run server code on one pc and client code on another pc i get errors like java.io.StreamCorruptedException: invalid type code: 00 or java.io.StreamCorruptedException: invalid stream header: 6C69656E
byte[] readBuffer = new byte[4096];
int num = inStream.read(readBuffer); //inStream is socket input stream
ByteArrayInputStream bis = new ByteArrayInputStream(readBuffer);
ObjectInput in = new ObjectInputStream(bis);
Object o = in.readObject(); //this line throws error
The thing is that writing and reading object to socket stream works on server (which is on pc where i created project) but reading from input stream on client (another pc where i copied project) throws error.
Can someone help me with this? I searched everywhere for solution but i can't figure out what is problem with serializing, because it works on same pc but won't on another. Is there any way that i can make this pc independent? This also happens when i create jar files and run it on same pc where it works in Intellij Idea.
It can because that client didnt read message fully.
But the real mistake is that you work with TCP socket like a message protocol transport but TCP is a stream protocol so you have to create your own message protocol on top of TCP.
Why it works fine on local system?
Because transport data between client and server happen too fast in local test and maybe in just one frame so all the message transported in just one IO-call but in internet or a network it doesn't work like you think.
There is 2 way to handle this mistake:
1- Pass SocketInputStream directly to ObjectInputStream instance and let it handle read objects.
2- Create a message protocol for example you can put the size of message in 2 or more first bytes. Then you can workd like this :
Read 2(or more) first bytes and detect size of packet.
Create a buffer for this size and read packet bytes.(make sure you read all of packet data from socket . You can use return value of SocketInputStream.read(byte[]) method to calculate it)
Pass the packet to ObjectInputStream and read object !

How can I make a server push

I developed my own client and server in java und I implemented a long-polling communication. So my client will always ask for new operations to be executed. If a server has a new operation, then he will send it to the client. The whole communication is based on java-sockets. Now I am thinking to renounce on long-polling-communication and implement a server-push. In this case the client will not ask my server anymore. He will just wait for notifications. If my server has a new operation to be executed, then he will make a server-push. My Goal is to save time on the client-side. So he will not send any request anymore
Question1: is it possible to implement a server-push on my model based on sockets in Java ?
Question2: if yes,how can I implement a server-push?
On server side:
...
DataOutputStream toClient = new DataOutputStream(socket.getOutputStream());
outToClient.writeBytes("event_xyz");
...
On client side you should listen for events
If I understood your question in the right way, you just have to wait for incoming data on the client. For example with a DataInputStream.readUTF(); this method waits until the ServerSocket pushes some data through the stream with the method DataOutputStream.writeUTF(String s) .

Why did parseFrom() function hang using protobuf in java socket?

I just want to create echo server/client using protobuf and java.
I tested with protobuf-java-2.4.1 and jdk1.7.
I wrote echo server code like below
// create server socket and accept for client connection.
// ...
link = servSock.accept();
Person person = Person.parseFrom(link.getInputStream()); // blocking position
person.writeTo(link.getOutputStream());
I think it is not necessary to note Person.proto.
The client code is only send Person object using socket input stream and receive echo Person object.
// socket connect code was omitted.
Person person = Person.newBuilder().setId(1).setName("zotiger").build();
person.writeTo(echoSocket.getOutputStream());
person = Person.parseFrom(echoSocket.getInputStream());
But server was blocked in parseFrom function when the server and client both run.
I found if i use writeDelimitedTo() and parseDelimitedFrom(), then that is ok. I don't understand why the writeTo() and parseFrom() function does not working.
Why did the server blocking in there?
Is it necessary to send some end signal from client side?
The reason you have to use writeDelimitedTo()/parseDelimitedFrom() is that otherwise protocol buffers may have no idea how much data it needs to read from the socket. That presents a problem (I say may because you could of course create a message with only fixed length fields that wouldn't require this ... but protocol buffers has to deal with both cases)
The writeDelimitedTo() method writes the length of the message to the OutputStream then the message itself. Its counterpart parseDelimitedFrom() reads the length, then the message.
You can use writeTo() and pasrseFrom() with streams but only if you want to write/read a single message and are closing the stream after writing. The reader will then get an EOF to indicate the end of the message (also the case when reading from a file that contains only a single message).
Don't write your own Client/Server, ie. RPC solution. There is one here......https://code.google.com/p/protobuf-rpc-pro/ which has some nice features already for java.

Network output from server can't be read by client

So, I have a simple socket server and a socket. I run the socket server, successfully. The client socket connects and sends a string - this works. I want the server to write back different information based on this string. I can check what the string is and get an OutputStream to the client, but whenever I write to it and flush, the InputStream client-side is NEVER in a ready state, and will never get a message back... I just don't see what I'm doing wrong.
All the code is at http://pastebin.com/u/omegazero
NetworkAgent.java is the client, SimbadAgent.java is the server, and UserAgent.java is the actual implementation of said server (the server is abstract for other reasons).
Compile everything, then run UserAgent followed by NetworkAgent and you will see what happens.
Executed your code (after commenting the reference to StringQueue in SimbadAgent) and I got the following output.
wrote get_cmd
Input shutdown? false
iS()iI()iM()iB()iA()iD()i ()iB()iO()iO()iY()iA()NETWORKAGENT: Response to "get_cmd": "SIMBAD BOOYA"

to read the packet of bytes on client(client Socket) from server(ServerSocket) using java

i m a new .
i m a java developer(fresher) and currently i m working on BSE project and i m facing problem to read the packet of bytes on the client(client socket) from the server(server socket). if u can help me then please help me.
Thanks in advance
Well, if you want to interact directly with packets, then you need to use a DatagramSocket instead of the regular Socket and ServerSocket.
Then, you should visit this link to see a good tutorial on how to get started with sending and receiving individual packets.
The basic idea is that the Client or Server will block on the recieve() call while it waits for its partner to send a packet using send().
If you aren't interested in the individual packets like you indicated in your question, then you will want to use Socket and ServerSocket. The first step to communicating between the two involves code that will look similar to the following:
//Server
// this call will block until the client tries to connect to the server
Socket cientConn = new ServerSocket(8878).accept();
// now you can use the connection's input and output streams to send data
/******************/
// Client
Socket serverConn = new Socket(addressOfServer, 8878);
// now you can use the connections input and output streams
After you get connections set up, you will have basically 2 read/write loops. One on the client, and one on the server.
while(true) [
// check for data from an input stream
...
// respond with message back
}
You will need a similar loop for the client and the server.

Categories