We help IT Professionals succeed at work.
Get Started

Exception in thread "main" java.net.SocketException: Broken pipe   ???

perdoname_
perdoname_ asked
on
5,087 Views
Last Modified: 2010-07-27
Hello Experts,

I need your assistance about the following problem which occurs during the communication of the server and the client

Exception in thread "main" java.net.SocketException: Broken pipe
      at java.net.SocketOutputStream.socketWrite0(Native Method)
      at java.net.SocketOutputStream.socketWrite(SocketOutputStream.java:92)
      at java.net.SocketOutputStream.write(SocketOutputStream.java:115)
      at java.io.DataOutputStream.write



Thanks in advance for any assistance !
//Server code:
 
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Scanner;
import java.util.Vector;
 
public class ClientServerMsg {
 
	static final int LISTENING_PORT = 1025;
 
	public static void main(String[] args) throws Exception {
 
		ServerSocket listener;
		listener = new ServerSocket(LISTENING_PORT);
		System.out.println("Port: " + LISTENING_PORT);
		while (true) {
			Socket connection = listener.accept();
			new ConnectionHandler(connection);
		}
 
	}
 
	static class ConnectionHandler extends Thread {
 
		int MAX = 10;
		Vector msgsList = new Vector();
 
		Socket connection;
		PrintWriter out;
		//out = System.out;
		
		private OutputStream outputstream;
		
		  ConnectionHandler(Socket conn) throws IOException {
	            connection = conn;
	            outputstream = conn.getOutputStream();
	            out = new PrintWriter(outputstream, true);
	            start();
	        }
		/*
		ConnectionHandler(Socket conn) {
			connection = conn;
			start();
		}
		*/
 
		void sendPOSTMESSAGE(String msg) throws Exception {
			try {
				if (msgsList.size() < MAX) {
					msgsList.addElement(msg);
					out.println("OK");
					out.flush();
				} else {
					out.println("ERROR");
					out.flush();
				}
			} catch (Exception ex) {
				out.println("ERROR");
				out.flush();
			}
 
			if (out.checkError())
				throw new Exception("Error while receiving the message");
		}
 
		void sendREADMESSAGE() throws Exception {
			if (msgsList.size() == 0) {
				out.println("ERROR");
				out.flush();
			} else if (msgsList.size() > 0) 
			{
				for (int i = 0; i < msgsList.size(); i++) {
					if (((String) msgsList.elementAt(i)).startsWith("URGENT:")) {
						out.write((String) msgsList.elementAt(i));
						msgsList.remove(i);
					} else {
						out.write((String) msgsList.elementAt(0));
						msgsList.remove(0);
					}
				}
			} else {
				if (out.checkError())
					throw new Exception("Error");
			}
		}
 
		void sendREMOVEMESSAGE(String remvmsg) throws Exception {
			for (int i = 0; i < msgsList.size(); i++) {
				if (((String) msgsList.elementAt(i)).equals(remvmsg)) {
					msgsList.remove(i);
					out.println("OK");
					out.flush();
				} else {
					out.println("ERROR");
					out.flush();
				}
			}
		}
 
		void sendQUIT() throws Exception {
			out.close();
			connection.close();
		}
 
		public void run() {
 
			String command = "";
			try {
				InputStream instream = null;
				try {
					instream = connection.getInputStream();
				} catch (IOException e) {
					e.printStackTrace();
				}
				Scanner in = new Scanner(instream);
				try {
					outputstream = connection.getOutputStream();
				} catch (IOException e) {
					e.printStackTrace();
				}
				//command = in.nextLine();
				if (in.hasNextLine()) {
					command = in.nextLine();
				}
				if (command.startsWith("POSTMESSAGE")) {
					try {
						sendPOSTMESSAGE(command);
					} catch (Exception e) {
						e.printStackTrace();
					}
				} else if (command.equals("READMESSAGE")) {
					try {
						sendREADMESSAGE();
					} catch (Exception e) {
						e.printStackTrace();
					}
				} else if (command.startsWith("REMOVEMESSAGE")) {
					try {
						sendREMOVEMESSAGE(command);
					} catch (Exception e) {
						e.printStackTrace();
					}
				} else if (command.equals("QUIT")) {
					try {
						sendQUIT();
					} catch (Exception e) {
						e.printStackTrace();
					}
				} else {
					out.println("UNKNOWN COMMAND");
					out.flush();
				}
			} finally {
				try {
					connection.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
 
	}
 
}
 
 
 
//Client code:
 
import java.io.*;
import java.net.*;
 
public class Client {
 
	public static void main(String argv[]) throws Exception
	{
		String sentence;
		String modifiedSentence;
		String hostname;
		int portNumber;
		String portString;
 
		BufferedReader inFromUser =
			new BufferedReader(new InputStreamReader(System.in));
		System.out.println("What host would you like to connect to?");
		hostname = inFromUser.readLine();
		
		System.out.println("What port would you like to connect to?");
		portString = inFromUser.readLine();
 
		portNumber = Integer.parseInt(portString);
 
		System.out.println("Connecting to port "+ portNumber+ " of "+ hostname +"....\n");
		Socket clientSocket = new Socket(hostname, portNumber);
 
		DataOutputStream outToServer =
			new DataOutputStream(clientSocket.getOutputStream());
 
		BufferedReader inFromServer =
			new BufferedReader(new
					InputStreamReader(clientSocket.getInputStream()));
 
		System.out.println("Connection establised");
 
 
		sentence = inFromUser.readLine();
		while (sentence != null)
		{
			/* send the sentence to the server */
		        outToServer.writeBytes(sentence + '\n'); 
			/* read response from the server */
			modifiedSentence = inFromServer.readLine();
			
			outToServer.flush();
		}
 
		clientSocket.close();
 
	}
}

Open in new window

Comment
Watch Question
CERTIFIED EXPERT
Distinguished Expert 2020
Commented:
This problem has been solved!
Unlock 1 Answer and 21 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE