1 Replies - 671 Views - Last Post: 20 June 2013 - 07:47 PM Rate Topic: -----

#1 novakasss  Icon User is offline

  • D.I.C Regular

Reputation: 4
  • View blog
  • Posts: 352
  • Joined: 11-July 12

Socket connection reset

Posted 20 June 2013 - 02:41 AM

It is basic server-client chat program. I'm trying to make it work, but i get Connection reset error.
This is server:
public class Server {

	private ServerSocket serverSocket;
	private ArrayList clients;
	
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	public class ClientSupport implements Runnable{

		private Socket sock;
		private InputStreamReader isr;
		private BufferedReader buff;
		
		public ClientSupport(Socket socket){
			sock = socket;
			try {
				isr = new InputStreamReader(sock.getInputStream());
				buff = new BufferedReader(isr);
				
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		
		@Override
		public void run() {
			String message;
			try{
				System.out.println("Im hgere");
				while(true){
					if((message = buff.readLine()) != null){
						flush(message);
					}
				}
			}catch(Exception e){e.printStackTrace();}
		}
	}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	
	public static void main(String[] ar){
		new Server();
	}
	
	public Server(){
		clients = new ArrayList();
		setConnection();
		go();
	}
	
	private void setConnection(){
		try{
			serverSocket = new ServerSocket(5423);
			System.out.println("Server ready");
		}catch(Exception e){e.printStackTrace();}
	}
	
	private void go(){
		while(true){
			try {
				Socket s = serverSocket.accept();
				System.out.println("Someone connected");
				PrintWriter writer = new PrintWriter(s.getOutputStream());
				clients.add(writer);
				Thread t = new Thread(new ClientSupport(s));
				t.start();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
	
	public void flush(String mess){
		for(int i=0; i<clients.size();i++){
			PrintWriter write = (PrintWriter) clients.get(i);
			write.write(mess);
			write.flush();
		}
	}
	
}


And here is client. I deleted gui part to make things clearier.
public class Client implements ActionListener{
	
	//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	public class Reader implements Runnable{
		
		private Socket sock;
		private BufferedReader reader;
		
		public Reader(Socket s){
			sock = s;
			try {
				InputStreamReader isr = new InputStreamReader(sock.getInputStream());
				reader = new BufferedReader(isr);
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

		@Override
		public void run() {
			String msg;
			try{
			while(true){
				if((msg = reader.readLine()) != null){
					textArea.append(msg+"\n");
				}
			}
			}catch(Exception ex){ex.printStackTrace();}
		}
		
	}
    //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	public static void main(String[] args){
		new Client();
	}
	
	private JTextArea textArea;
	private JTextField textfield;
	private JButton button;
	private JPanel panel;
	private Socket socket;
	
	public Client(){
		setConnection();
		
	}
	
	private void setConnection(){
		try{
			socket = new Socket("localhost",5423);
			Thread t = new Thread(new Reader(socket));
			t.start();
			System.out.println("Succesfully connected");
		}catch(Exception ex){ex.printStackTrace();}
	}
	
		@Override
	public void actionPerformed(ActionEvent arg0) {
		String msg = textfield.getText();
		textfield.setText("");
		try {
			PrintWriter writer = new PrintWriter(socket.getOutputStream());
			writer.write(msg);
			writer.flush();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}

Error shows line, where Server or Client reads messages inside run() method using bufferedReader readLine() method. What is wrong here?

Is This A Good Question/Topic? 0
  • +

Replies To: Socket connection reset

#2 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8329
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Socket connection reset

Posted 20 June 2013 - 07:47 PM

Make your ArrayList an ArrayList<PrintWriter>... you hoo... this was introduced with JRE 1.5 and we are almost at 7
You should'nt forward the message to yourself or you will be in an infinite loop
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1