Sending Vector over OuputStream and GUI problem
Hey,
Im trying to write a small chat program, I have a server and a client. Ive got so far but Im trying to send a vector of users to display on the client (ie, users currently chatting)...
Also, when the user input their name, the GUI (basically a text box to hold the names so far) is the suppose to appear, this was working a while ago and now isnt, dont know where I'm going wrong.
Any help in sending the Vector across or displaying my GUI would be appreciated muchly. Code to follow:
import java.io.*;
import java.net.*;
import java.util.*;
import java.text.DecimalFormat;
import javax.swing.*;
import java.sql.*;
import java.awt.*;
import java.awt.event.*;
publicclass ChatServer
{
privatestatic ServerSocket servSocket;
privatestaticfinalint PORT = 1234;
publicstatic Scanner keyboard =new Scanner(System.in);
publicstatic Calendar deadlineOne;
publicstatic Calendar deadlineTwo;
publicstaticint clientCount =0;
publicstatic Vector<User> users;
privatestatic Connection link =null;
publicstatic String userName, socketReference;
publicstaticvoid main(String[] args)throws IOException
{
users =new Vector<User>();
// Creates a ServerSocket object associating its service with port 1234
// clients must make a connection here...
System.out.println("Opening port...\n");
try
{
servSocket =new ServerSocket(PORT);
System.out.println("Port Opened...\n");
}
catch (IOException e)
{
System.out.println("\nUnable to set up port!");
System.exit(1);
}
do
{
//puts server into a waiting state - waits indefinately for a client to connect
Socket client = servSocket.accept();
ClientHandler handler =new ClientHandler(client);
handler.start();
}while (true);
}
}
class ClientHandlerextends Thread
{
private Socket client;
int clientNumber;
private Scanner input;
private PrintWriter output;
private ObjectOutputStream outFile;
public ClientHandler(Socket socket)throws IOException, NumberFormatException
{
client = socket;
//setting up input and output streams
input =new Scanner(client.getInputStream());
output =new PrintWriter(client.getOutputStream(),true);
outFile =new ObjectOutputStream (client.getOutputStream());
}
publicvoid run()
{
String userInput ;
ChatServer.userName = input.nextLine();
ChatServer.socketReference = client.getRemoteSocketAddress().toString();
User user =new User(ChatServer.userName, ChatServer.socketReference,true);
ChatServer.users.addElement(user);
ChatServer.clientCount=0;
for (User user1 : ChatServer.users)
{
if (user1.connected ==true)
{
ChatServer.clientCount++;
}
}
System.out.println("User Entered Chat... ");
System.out.println("Number of poeple chatting: " + ChatServer.clientCount);
for (User user1 : ChatServer.users)
{
System.out.println("userName in vector: " + user1.getUserName());
System.out.println("socketReference in vector: " + user1.getSocketReference());
}
//Sends out all item information to the client...
output.println(ChatServer.users);
do
{
{
//Gets the users input
userInput = input.nextLine();
if (userInput.equals("hello"))
{
System.out.println("...");
}else
{
System.out.println("");
}
}
}while (!userInput.equals("QUIT"));
}
//Method to get the current date and time
public String getDateTime()
{
Calendar start = Calendar.getInstance();
String time = start.getTime().toString();
return time;
}
}
class User
{
String userName;
String socketRef;
boolean connected =true;
public User (String userName, String socketRef,boolean connected)
{
this.userName = userName;
this.socketRef = socketRef;
this.connected = connected;
}
public String getUserName()
{
return userName;
}
public String getSocketReference()
{
return socketRef;
}
}
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;
import java.util.*;
import javax.swing.*;
publicclass ChatClientextends JFrameimplements ActionListener
{
private InetAddress host;
privatefinalint PORT = 1234;
private Socket link;
private Scanner networkInput;
private ObjectInputStream inStream ;
private PrintWriter output;
private JTextField msgToSend;
private JButton sendMsg;
private JPanel mainPanel;
private JLabel mainTitle;
String [] chatters =new String [2];
publicstaticvoid main(String[] args)throws IOException, ClassNotFoundException
{
//opens a client in the centre of the screen
ChatClient frame =new ChatClient();
frame.setTitle("Welcome " + System.getProperty("user.name"));
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
int screenWidth = (int)screenSize.getWidth();
int screenHeight = (int)screenSize.getHeight();
int frameWidth=430 ,frameHeight=380;
frame.setSize(frameWidth,frameHeight);
frame.setLocation((screenWidth-frameWidth)/2,(screenHeight-frameHeight)/2);
frame.setVisible(true);
frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
}
public ChatClient ()throws IOException,ClassNotFoundException
{
//closes the client correctly if the user clicks "X"
addWindowListener(
new WindowAdapter()
{
publicvoid windowClosing(WindowEvent e)
{
if (link !=null)
try
{
output.println("QUIT");
link.close();
}
catch (IOException ioEx)
{
System.out.println(
"\n*** Unable to close link!***\n");
System.exit(1);
}
System.exit(0);
}
}
);
//initialisation of GUI
mainPanel =new JPanel(new FlowLayout());
msgToSend =new JTextField(10);
mainPanel.add(msgToSend);
// END OF GUI
//Connect to Server
try
{
host = InetAddress.getLocalHost();
}
catch(UnknownHostException uhEx)
{
System.out.println("\nHost ID not found!\n");
System.exit(1);
}
link =new Socket(host, PORT);
//setting up input and output streams...
networkInput =new Scanner(link.getInputStream());
inStream =new ObjectInputStream(link.getInputStream());
output =new PrintWriter(link.getOutputStream(),true);
System.out.println("Connected To Host\n");
//The user is prompted to enter their name...
String name =JOptionPane.showInputDialog(this,
"Please Enter Your Name:","User Name",
JOptionPane.INFORMATION_MESSAGE);
output.println(name);
//go to method getItems that displays the item info in the GUI
getItems();
}
publicvoid getItems()throws ClassNotFoundException,IOException
{
for (int i = 0; i <2 ; i++)
{
chatters [i] = networkInput.nextLine();
}
}
publicvoid actionPerformed(ActionEvent e)
{
}
}

