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

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

perdoname_Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

CEHJCommented:
Make sure you aren't writing to a stream/socket that's been closed. As i mentioned before, you don't want to be using DataOutputStream but PrintWriter
0
perdoname_Author Commented:
Thanks to you the Server works fine but after each message the connections stops and prints the above error :S
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;
 
 
/**
 * DOCUMENT ME!
 *
 * @author $author$
 * @version $Revision$
  */
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 = new PrintWriter(System.out); //For standard output
		private OutputStream outstream;
 
		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 {
					outstream = 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();
				}
			}
		}
	}
}

Open in new window

0
CEHJCommented:
Your server simply reads the message, echos it, then closes. Don't you want it to respond continously? If so, you need to loop in run()
0
OWASP Proactive Controls

Learn the most important control and control categories that every architect and developer should include in their projects.

perdoname_Author Commented:
I've added a while (true) but that leads again to:
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.writeBytes(DataOutputStream.java:259)
      at Client.main(Client.java:41)


plus that:

java.net.SocketException: Socket is closed
      at java.net.Socket.getInputStream(Socket.java:766)
      at ClientServerMsg$ConnectionHandler.run(ClientServerMsg.java:112)
Exception in thread "Thread-0" java.lang.NullPointerException
      at java.io.Reader.<init>(Reader.java:61)
      at java.io.InputStreamReader.<init>(InputStreamReader.java:55)
      at java.util.Scanner.<init>(Scanner.java:575)
      at ClientServerMsg$ConnectionHandler.run(ClientServerMsg.java:117)

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;
 
 
/**
 * DOCUMENT ME!
 *
 * @author $author$
 * @version $Revision$
 */
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 = new PrintWriter(System.out); //For standard output
		private OutputStream outstream;
 
		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 = "";
			while (true)
			{
				try {
					InputStream instream = null;
 
					try {
						instream = connection.getInputStream();
					} catch (IOException e) {
						e.printStackTrace();
					}
 
					Scanner in = new Scanner(instream);
 
					try {
						outstream = 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();
					}
				}
			}
		}
	}
}

Open in new window

0
CEHJCommented:
Should be more like the following:
                public void run() {
			String command = "";
 
			try {
				InputStream instream = null;
 
				try {
					instream = connection.getInputStream();
				} catch (IOException e) {
					e.printStackTrace();
				}
 
				Scanner in = new Scanner(instream);
 
				try {
					outstream = connection.getOutputStream();
				} catch (IOException e) {
					e.printStackTrace();
				}
 
				//command = in.nextLine();
				while ("QUIT".equals(command) == false) {
					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();
				}
			}
		}

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
perdoname_Author Commented:
Thank you very much but unfortunately there still a problem
After the first message there is no response for further messages
e.g
java Client
What host would you like to connect to?
localhost
What port would you like to connect to?
1025
Connecting to port 1025 of localhost....

Connection establised
POSTMESSAGE something
READMESSAGE
POSTMESSAGE some




java ClientServerMsg localhost 1025
Port: 1025
OK

0
CEHJCommented:
I would actually include debugging messages so you can trace the execution
0
perdoname_Author Commented:
I didn't understand what do you mean :S
0
CEHJCommented:
System.out.println("This is a debugging message");
0
perdoname_Author Commented:
And how this will make the application to run with no problems :S ? ?
0
CEHJCommented:
It won't. It will help you find out why it's not doing so
0
perdoname_Author Commented:
But i cant see anything wrong in the code :S :S
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;
 
 
/**
 * DOCUMENT ME!
 *
 * @author $author$
 * @version $Revision$
 */
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;
		@SuppressWarnings("unchecked")
		Vector msgsList = new Vector();
		Socket connection;
		PrintWriter out = new PrintWriter(System.out); //For standard output
		@SuppressWarnings("unused")
		private OutputStream outstream;
 
		ConnectionHandler(Socket conn) {
			connection = conn;
			start();
		}
 
		@SuppressWarnings("unchecked")
		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 {
					outstream = connection.getOutputStream();
				} catch (IOException e) {
					e.printStackTrace();
				}
 
				//command = in.nextLine();
				while ("QUIT".equals(command) == false) {
					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();
				}
			}
		}
	}
}

Open in new window

0
CEHJCommented:
I would also recommend you run the server and test it with telnet, leaving the client until you know the server works as expected
0
CEHJCommented:
Also change

>>PrintWriter out = new PrintWriter(System.out); //For standard output

to

PrintWriter out = new PrintWriter(System.out, true); //For standard output
0
perdoname_Author Commented:
Still seems fine so the problem probably is logical (?)  :S
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;
 
 
/**
 * DOCUMENT ME!
 *
 * @author $author$
 * @version $Revision$
 */
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;
		@SuppressWarnings("unchecked")
		Vector msgsList = new Vector();
		Socket connection;
		PrintWriter out = new PrintWriter(System.out, true); //For standard output
		@SuppressWarnings("unused")
		private OutputStream outstream;
 
		ConnectionHandler(Socket conn) {
			connection = conn;
			start();
		}
 
		@SuppressWarnings("unchecked")
		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 {
					outstream = connection.getOutputStream();
				} catch (IOException e) {
					e.printStackTrace();
				}
 
				//command = in.nextLine();
				while ("QUIT".equals(command) == false) {
					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();
				}
			}
		}
	}
}

Open in new window

0
CEHJCommented:
Yes, it probably is
0
perdoname_Author Commented:
Have you got any ideas what might be the problem ?
0
CEHJCommented:
It seems to work fine for me using telnet
0
perdoname_Author Commented:
So the problem is in the client ??
//Client:
 
import java.io.*;
import java.net.*;
 
public class Client {
 
	public static void main(String argv[]) throws Exception
	{
		String sentence;
		String modifiedSentence = null;
		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

0
CEHJCommented:
Probably, yes. I gather you're now dealing with that in another question
0
perdoname_Author Commented:
If you could also be some assistance about the client problem would be more than grateful !
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.