2 Replies - 247 Views - Last Post: 30 December 2012 - 10:57 AM Rate Topic: -----

#1 novakasss  Icon User is offline

  • D.I.C Regular

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

IO problem

Posted 30 December 2012 - 06:36 AM

I'm trying to create a chat program. But I have some problems. Here is my server and client classes:
import java.io.*;
import java.net.*;
import java.util.*;
import javax.swing.*;

public class Server {

	public int port;
	public ServerSocket server;
	public ArrayList<Client> connectedUsers;
	public boolean keep;
	
	public Server ( int port ) {
		
		this.port = port;
		openConnection();
		
	}
	
	private void openConnection() {
		
		// Creating server
		try {
			server = new ServerSocket(port);
		    JOptionPane.showMessageDialog(null, "Server created!");
		    connectedUsers = new ArrayList<Client>();
		    keep = true;
		    
		    // Accepting clients
		    while(keep) {
		    	
		    	display("Waiting for connection...");
		    	Socket user = server.accept();
		    	
		    	
		    	display("Someone connected.");
		    	
		    	Client c = new Client(user);
		    	
		    	connectedUsers.add(c);
		    	c.start();
		    }
		    
		    // Closing everything at the end
		    try {
		    	
		    	server.close();
		    	for(int i = 0; i<connectedUsers.size(); i++){
		    		Client tc = connectedUsers.get(i);
		    		try {
		    		    
		    		}catch(Exception e){}
		    	}
		    	
		    }catch(Exception e){
		    	
		    	display("Exception closing the server " + e);
		    	
		    }
		    
		} catch (IOException e) {
			display("Can't create server");
			e.printStackTrace();
		}
		
	}
	
	//Message displayer
	private void display(String message){
		
		JOptionPane.showMessageDialog(null, message);
		
	}

	public static void main(String[] args) {
		
		String port =  JOptionPane.showInputDialog("Enter port number");
		int portNumber = Integer.parseInt(port);

		Server server = new Server(portNumber);
	}
	
	
	// Inner class for each user
	private class Client extends Thread{

		public Socket socket;
		public ObjectInputStream sInput;
		public ObjectOutputStream sOutput;
		public String username;
		
		public Client (Socket s) {
			socket = s;
			
			try{
				
				sInput = new ObjectInputStream(socket.getInputStream());
				sOutput = new ObjectOutputStream(socket.getOutputStream());
				
			    username = (String) sInput.readObject();
			    display(username+" has connected");
				
			}catch(Exception e){}
		}
	}

}

and client:
import java.net.*;
import java.io.*;
import javax.swing.*;

public class Client {

	public String nickname;
	public String server;
	public int port;
	public Socket socket;
	public ObjectInputStream sInput;
	public ObjectOutputStream sOutput;
	public JFrame frame;
	public ClientGUI gui;
	
	public Client () {
		
		nickname = JOptionPane.showInputDialog("Choose a nickname");
		server = JOptionPane.showInputDialog("Enter the server name you want to connect");
		port = Integer.parseInt(JOptionPane.showInputDialog("Enter port"));
		
		try{
			socket = new Socket(server, port);
			JOptionPane.showMessageDialog(null, "Successfully connected to the server");
			
			try {
				sInput  = new ObjectInputStream(socket.getInputStream());
			 	sOutput = new ObjectOutputStream(socket.getOutputStream());
			 
		    }catch (IOException eIO) {
		    	eIO.printStackTrace();
			}
			JOptionPane.showMessageDialog(null, "Successfully created IO part");
		}catch(Exception e){
			e.printStackTrace();
			JOptionPane.showMessageDialog(null, "Can't connect to the server");
		}
		
		setFrame();
		
	}
	
	private void setFrame() {
		
		frame = new JFrame(nickname + " - Chatter v1.0");
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		gui = new ClientGUI();
		frame.add(gui);
		frame.setSize(600,400);
		frame.setVisible(true);
	}
	
	
	public static void main(String[] args) {
		
		Client client = new Client();

	}

}



I can connect. Messages show that client connected, server messages show that too. But when I want to initialize ObjectStreams in client class, the program stops. I don't know why. What's wrong?

This post has been edited by novakasss: 30 December 2012 - 06:37 AM


Is This A Good Question/Topic? 0
  • +

Replies To: IO problem

#2 CasiOo  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1407
  • View blog
  • Posts: 3,123
  • Joined: 05-April 11

Re: IO problem

Posted 30 December 2012 - 08:00 AM

Read the documentation

Quote

This constructor will block until the corresponding ObjectOutputStream has written and flushed the header.

When you initialize a new ObjectInputStream, it will block until it has received the header, right now you are making both of them block waiting for the header

The solution is very simple
All you have to do is switch it around, so that you initialize the ObjectOutputStream before the ObjectInputStream in your server (or in both)
				sOutput = new ObjectOutputStream(socket.getOutputStream());
				sInput = new ObjectInputStream(socket.getInputStream());


This post has been edited by CasiOo: 30 December 2012 - 08:02 AM

Was This Post Helpful? 1
  • +
  • -

#3 novakasss  Icon User is offline

  • D.I.C Regular

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

Re: IO problem

Posted 30 December 2012 - 10:57 AM

View PostCasiOo, on 30 December 2012 - 08:00 AM, said:

Read the documentation

Quote

This constructor will block until the corresponding ObjectOutputStream has written and flushed the header.

When you initialize a new ObjectInputStream, it will block until it has received the header, right now you are making both of them block waiting for the header

The solution is very simple
All you have to do is switch it around, so that you initialize the ObjectOutputStream before the ObjectInputStream in your server (or in both)
				sOutput = new ObjectOutputStream(socket.getOutputStream());
				sInput = new ObjectInputStream(socket.getInputStream());


Thank you very much. Everything works fine now. :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1