Java SocketException Broken pipe - java

I am doing a client to server Log-in communication.
I met a java.net.SocketException: broke Pipe at Server end. And I have narrowed the
problem to one single line at the client end. If I move a position for this line,
the code works. plese see the following code.
Client End:
Socket socket = new Socket(Const.destIp, 12101);
ObjectOutputStream out = new ObjectOutputStream(socket.getOutputStream());
out.writeObject(this.message);
out.close();//Line that cause problem
ObjectInputStream in = new ObjectInputStream(socket.getInputStream());
ServerToClientLogin msg = (ServerToClientLogin) in.readObject();
//out.close();//move it to here, problem solved
in.close();
socket.close();
Server end:
while (true) {
socket = _serverSocket.accept();
in = new ObjectInputStream(socket.getInputStream());
msg = (ClientToServerLogin) in.readObject();
ServerToClientLogin msgToSend = null;
out = new ObjectOutputStream(socket.getOutputStream());
msgToSend = handleLoginRequest(msg);
if(msgToSend != null) out.writeObject(msgToSend);
try { in.close(); } catch (IOException e) {e.printStackTrace();}
try { out.close();} catch (IOException e) {e.printStackTrace(); }
try { socket.close();} catch (IOException e) {e.printStackTrace();}
}
Since readObject and writeObject are blocking call, I have no idea why close it earlier would case such problem.

out.close();: Closes this (out) output stream and releases any system resources associated with this stream.
See the API here.

Related

Sockets not communicate even if no exception are launched

I'm trying to send a string from the client to the server, but even if all the code compiles without errors and no exception are thrown, nothing is sent. This is my server:
String nomeAccount = "";
try {
//PHASE 1: The server receives the email
try {
InputStream inStream = incoming.getInputStream();
BufferedReader in = new BufferedReader(new InputStreamReader(incoming.getInputStream()));
//if I print something here it works
nomeAccount = in.readLine();
//here nothing get printed
System.out.println("Nome: "+nomeAccount);
} catch (IOException ex) {
System.out.println("Not works");
Logger.getLogger(ServerController.class.getName()).log(Level.SEVERE, null, ex);
} finally {
try {
incoming.close();
} catch (IOException ex) {
System.out.println("Not works");
Logger.getLogger(ServerController.class.getName()).log(Level.SEVERE, null, ex);
}
}
This is the client:
//PHASE 1: The client sends a string to the server
try {
InputStream inStream = s.getInputStream();
OutputStream outStream = s.getOutputStream();
PrintWriter out = new PrintWriter(outStream, true);
out.write(account+"\n");
A couple of problems are
PrintWriter eats any Exceptions throws. You wouldn't see them if they were thrown.
The purpose of using PrintWriter instead of a plain Writer is to use println, print or printf
unless you print a new line it won't flush the data (which should work with write() but println is clearer)

Server: Socket hangs within unpredictable period time at read stream function

I wrote a Java socket server which will keep connection alive until client disconnected. And my client code will keep pushing message to this server app.
But when I run those programs a while, I also seem an unusual condition that Server will hangs while reading input stream from client within unpredictable period. It always hang at inData.read(b) because I see it printed "receiving..." on log when this problem occurred"; even I killed my client, server app still hangs right there.
But when I press Ctrl+C at the console which runs server app after this problem occurred, it will continue to work. This is really annoying.
Is there anyway to solve this Unusual problem nicely?
Server Code:
static ServerSocket server;
try {
server = new ServerSocket("1234");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Socket socket = null;
String inIp = null;
BufferedInputStream inData;
BufferedOutputStream outData;
while (true) {
try {
synchronized (server) {
socket = server.accept();
}
inIp = String.valueOf(socket.getInetAddress());
if (Log4j.log.isEnabledFor(Level.INFO)) {
Log4j.log.info("Incoming connection " + inIp);
}
while (true) {
inData = new BufferedInputStream(socket.getInputStream());
outData = new BufferedOutputStream(socket.getOutputStream());
String reply = "Hey";
byte[] b = new byte[10240];
String data = "";
int length;
if (Log4j.log.isEnabledFor(Level.INFO)) {
Log4j.log.info("InetAddr = " + inIp + ", receiving...");
}
// read input stream
length = inData.read(b);
data += new String(b, 0, length);
if (Log4j.log.isEnabledFor(Level.INFO)) {
Log4j.log.info("Data Length: " + length + ", Received data: " + data);
}
// output result
outData.write(reply.getBytes());
outData.flush();
}
} catch (Exception e) {
String tempStr = e.toString();
Log4j.log.error("Service error during executing: " + tempStr);
}
}
Client Code:
Socket client = new Socket();
InetSocketAddress isa = new InetSocketAddress("127.0.0.1", "1234");
String data = "Hi";
while(true) {
try {
if(!client.isConnected())
client.connect(isa, 30000);
BufferedOutputStream out = new BufferedOutputStream(client.getOutputStream());
BufferedInputStream in = new BufferedInputStream(client.getInputStream());
// send msg
out.write(data.getBytes());
out.flush();
System.out.println("Message sent, receiving return message...");
// get return msg
int length;
byte[] b = new byte[10240];
// read input stream
length = in.read(b);
retMsg = new String(b, 0, length);
System.out.println("Return Msg: " + retMsg);
Thread.sleep(60000);
} catch (java.io.IOException | InterruptedException e) {
System.out.println("Socket Error!");
System.out.println("IOException :" + e.toString());
}
}
try {
server = new ServerSocket("1234");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Don't write code like this. The catch block should be at the end, and all the code that depends on the success of new ServerSocket should be inside the try block.
synchronized (server) {
socket = server.accept();
}
Synchronization is not necessary here.
while (true) {
inData = new BufferedInputStream(socket.getInputStream());
outData = new BufferedOutputStream(socket.getOutputStream());
A large part of the problem, if not all of it, is here. You keep creating new buffered streams, every time around this loop, which means that anything the previous streams have buffered is thrown away. So you are losing input. You should create both these streams before the loop.
while(true) {
try {
if(!client.isConnected())
client.connect(isa, 30000);
This is pointless. Remove. You haven't shown how the client socket was created, but if you created it unconnected you should have connected it before entering this loop.
BufferedOutputStream out = new BufferedOutputStream(client.getOutputStream());
BufferedInputStream in = new BufferedInputStream(client.getInputStream());
Here again you must create these streams ahead of the loop.

one single ObjectOutputStream for multiple use with sockets

I'm trying to make a very basic example of a network connection where I use an ObjectOutputStream, ObjectInputStream and sockets. The client sends the string "Hello" to the server. As you see this string is sent 10 times with the very same ObjectOutputStream. Then the server sends the string "Goodbye" to the client. The console should display 10 times the string "hello" and 10 times the string "Goodbye". But they are shown only one time. I read in many threads about the method reset() of the ObjectOutputStream. But it does not seem to work for me. Any ideas what the problem is? I've already tried many different things without success.
Here is the client:
try
{
Socket socket = new Socket("localhost", 5555);
ObjectOutputStream out = new ObjectOutputStream(socket.getOutputStream());
ObjectInputStream in = new ObjectInputStream(socket.getInputStream());
int i = 10;
while (i > 0)
{
//send String
out.writeObject("Hello");
out.flush();
out.reset();
//receive String
String result = (String) in.readObject();
System.out.println(result);
i--;
}
} catch (IOException e)
{
e.printStackTrace();
} catch (ClassNotFoundException e)
{
e.printStackTrace();
} finally
{
out.close();
in.close();
socket.close();
}
Here is the server:
try
{
ServerSocket server = new ServerSocket(5555);
while(true)
{
try
{
Socket client = server.accept();
ObjectInputStream in = new ObjectInputStream(client.getInputStream());
ObjectOutputStream out = new ObjectOutputStream(client.getOutputStream());
//receive String
String input = (String) in.readObject();
System.out.println(input);
out.writeObject("Goodbye");
out.flush();
out.reset();
} catch(IOException e1)
{
e1.printStackTrace();
} catch (ClassNotFoundException e)
{
e.printStackTrace();
}
}
} catch(IOException e2)
{
e2.printStackTrace();
}
Your server is only reading and writing once to each accepted socket. So the client can't possibly read an object ten times.
Your server also isn't closing the stream when it's finished.

Not receiving final data from stream

I am currently not receiving the last object from my object stream until another set of data is sent from the server. The objects sent have either a 1,2 or 3 int to state whether they are the first middle or last packets. I have sent these objects to an array and analysed this in the debugger, it shows that the last packet does not come through until the first one is sent again.
This is the server code:
public void telleveryone(Object message){
Iterator it = clientOutputStream.iterator();
while(it.hasNext()){
try{
ObjectOutputStream everyone = (ObjectOutputStream)it.next();
everyone.writeObject(message);
everyone.flush();
}catch(Exception ex){
ex.printStackTrace();
}
}
}
This is the receiving code on the client:
public void run() {
try{
sock = new Socket("10.42.34.46", 1337);
InputStream is = sock.getInputStream();
ois = new ObjectInputStream(new BufferedInputStream(is));
OutputStream os = sock.getOutputStream();
oops = new ObjectOutputStream(os);
while(true){
serverDraw = (com.DrawTastic.Drawring) ois.readObject();
test.add(serverDraw);
}
}catch(IOException ex){
ex.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
You need to flush the header:
callers may wish to flush the stream immediately to ensure that
constructors for receiving ObjectInputStreams will not block when
reading the header
oops = new ObjectOutputStream(os);
oops.flush();
I didn't read over the code thoroughly, but I've run into this problem with Python. I noticed a lot of my commands to the server were not executing until my program pinged the server again. My solution was to ensure there was a newline at the end of each command to the server, or you could flush the buffer.

ObjectOutputStream not sending data

Client code:
try {
Socket socket = new Socket(ip, port);
OutputStream output = socket.getOutputStream();
ObjectOutputStream out = new ObjectOutputStream(output);
InputStream input = socket.getInputStream();
ObjectInputStream in = new ObjectInputStream(input);
out.writeByte(1);
FileHandler fh = (FileHandler) in.readObject();
//processing stuff
out.flush();
out.close();
output.flush();
output.close();
input.close();
in.close();
socket.close();
} catch (Exception e) {
e.printStackTrace();
}
Server code:
try {
ServerSocket server = new ServerSocket(port);
Socket socket = server.accept();
InputStream input = socket.getInputStream();
ObjectInputStream in = new ObjectInputStream(input);
int type = in.readByte();
//processing stuff (which includes closing the streams and sending FileHandler object)
socket.close();
} catch (Exception e) {
e.printStackTrace();
}
The server never receives the byte. It just waits for the byte from the client, and it never comes. I'm not sure why it isn't sending, or getting received. Any help is much appreciated.
If I had to make a guess it's because in your client you block on in.readObject(); waiting for the server to send you something thus never flush the output stream thus ... nothing ever gets sent.
Move your read to after you flush your output stream.
Try to use the writeObject and readObject methods. Also write an Integer not an int to the stream. Read this really good lecture before proceeding any further.
This is also a good lecture for your problem.
Regards!

Categories

Resources