Home arrow static arrow Java Programming [Archive] - Detecting Connections on a socket
Warning: Creating default object from empty value in /www/htdocs/w008deb8/wiki/components/com_staticxt/staticxt.php on line 51
Java Programming [Archive] - Detecting Connections on a socket
5 Duke Stars available
This topic has 2 replies on 1 page.

Posts:16
Registered: 7/31/04
Detecting Connections on a socket  
Aug 1, 2004 2:59 PM



 
Im very sorry if all this sounds confusing but there are several questions i have to know the answer to

I have been working the past hour or two on a chat program. When a client connects ot the server the client sends an object over a stream that contains the value of the client's username and an empy variable for an object output stream. The server this deserializes this information, adds the object to a vector and adds the user's name to another vector. The vector that stores the object is mainly used to keep track of the output streams on the socket so i can output to all the clients. The other vector that stores the user name only is used to show a list of all the suers currently logged in with a JList. After the server does all this it sends back to the client the vector with all the user names so that the client can also have a list of all the people logged in. I am now in a bit of a snag. I want to know if there is any easier/more efficient way to do what i described above and if there is any way to tell when a client logs out unexpectedly. I also have a bit of a problem when it comes to accessing the object sent from the client to the server. I have to cast the object in order to obtain access to its variables/methods so I want to know if i have to have a copy of the class on the server or if there is a cast without having the class there.

------------------------------------------Here is the code for the client-------------------------------------------------

import java.io.*;
import java.net.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;

class ChatGUI implements ActionListener{

JFrame frame = new JFrame();
JPanel pane = new JPanel();
JButton login;

JTextField userNameF = new JTextField();
JTextField ipF = new JTextField("127.0.0.1");

JTextField message = new JTextField();
JButton submit = new JButton("Submit");
ObjectOutputStream objOut;

static ObjectInputStream objIn;
static Vector usersV;
static JList userList;

ChatGUI(){
login = new JButton("Login");

JPanel info = new JPanel();
JLabel userNameL = new JLabel("User Name");

JLabel ipL = new JLabel("IP Addy");

login.addActionListener(this);

info.setLayout(new GridLayout(4,1));
info.add(userNameL);
info.add(userNameF);
info.add(ipL);
info.add(ipF);

pane.setLayout(new BorderLayout());
pane.add(info,"North");
pane.add(login,"South");

frame.setContentPane(pane);
frame.setBounds(450,300,150,150);

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
try{
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
SwingUtilities.updateComponentTreeUI(frame);
}
catch(Exception e){

}
frame.show();
}

ChatGUI(String name){
JPanel textP = new JPanel();

JTextArea text = new JTextArea();
text.setEditable(false);

JScrollPane scroll = new JScrollPane(text);

usersV = new Vector();

userList = new JList(usersV);

pane.setLayout(new BorderLayout());
pane.add(textP,"Center");
pane.add(submit,"South");

textP.setLayout(new BorderLayout());

textP.add(userList,"East");
textP.add(scroll,"Center");
textP.add(message,"South");

frame.setTitle(name + " is currently logged in");
frame.setContentPane(pane);
frame.setLocation(250,150);
frame.setSize(500,500);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
try{
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
SwingUtilities.updateComponentTreeUI(frame);
}
catch(Exception e){

}
frame.show();

}

public void actionPerformed(ActionEvent e){
Object arg = e.getSource();

if(arg==login){

try{
Socket client = new Socket(ipF.getText(),4321);

ClientInfo userInfo = new ClientInfo(userNameF.getText());
frame.hide();
ChatGUI chat = new ChatGUI(userInfo.userName);
objOut = new ObjectOutputStream(client.getOutputStream());
objOut.writeObject(userInfo);
objOut.flush();

objIn = new ObjectInputStream(client.getInputStream());
}
catch(IOException ioe){

}
}
if(arg==submit){

}

}

public static void main(String args[]){
ChatGUI login = new ChatGUI();

while(true){
try{
userList.setListData((Vector)objIn.readObject());
}
catch(Exception exp){

}
}

}
}

class ClientInfo implements Serializable{
String userName;
PrintWriter out;
ObjectOutputStream objOut;
ClientInfo(String n){
userName= n;
}
}


Here is the code for the Server
import java.io.*;
import java.net.*;
import javax.swing.*;
import java.awt.*;
import java.util.*;

class ServerGUI implements Runnable{
//One vector for user names one for output streams... this may change later
static Vector usersV = new Vector(0);
static Vector userNamesV = new Vector(0);
static ServerSocket server;
static JList userList = new JList(usersV);

Socket client;
ObjectInputStream objIn;

ServerGUI(Socket c){
client = c;
}

static void makeGUI(){

JFrame frame = new JFrame();
JPanel pane = new JPanel();
JLabel status = new JLabel("Server ready");

JPanel textP = new JPanel();

JTextArea text = new JTextArea();
text.setEditable(false);

JScrollPane scroll = new JScrollPane(text);

pane.setLayout(new BorderLayout());
pane.add(textP,"Center");
pane.add(status,"South");
textP.setLayout(new BorderLayout());

textP.add(userList,"East");
textP.add(scroll,"Center");

frame.setTitle("Server Program");
frame.setContentPane(pane);
frame.setLocation(250,150);
frame.setSize(500,500);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

try{
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
SwingUtilities.updateComponentTreeUI(frame);
}
catch(Exception e){

}
frame.show();
}

public void run(){
try{
objIn = new ObjectInputStream(client.getInputStream());

ClientInfo x = (ClientInfo)objIn.readObject();
x.out = new PrintWriter(client.getOutputStream());
x.objOut = new ObjectOutputStream(client.getOutputStream());
usersV.add(x);
userNamesV.add(x.userName);
userList.setListData(userNamesV);

//send the vector to the clients

for(int y = 0; y < usersV.size(); y++){
x = (ClientInfo)usersV.get(y);
x.objOut.writeObject(userNamesV);
}


}
catch(Exception ex){
ex.printStackTrace();
System.out.println("sdfsdf");
}
}

public static void main(String args[]) throws IOException{
ServerGUI clientT;
server = new ServerSocket(4321);
makeGUI();
while(true){

try{
clientT = new ServerGUI(server.accept());

Thread serverT = new Thread(clientT);
serverT.start();
}
catch(Exception e){
}

}

}

public void finalize(){

try{
server.close();
}
catch(IOException i){
}
}

}


Im sorry if it all sounds complex the way i described it... if you have questions about it please respond i will be checking back here every 10 minutes or so

Thanks... sorry if im a bit newbish
 

Posts:8,813
Registered: 10/4/00
Re: Detecting Connections on a socket  
Aug 1, 2004 3:20 PM (reply 1 of 2)



 
I normally don't do this, but you seem to have done enough work yourself to 'cheat'. You seem pretty lost. If it was me, I go look at other people code and see how they did it. Please don't use this as an excuse not to learn anything more.
http://www.google.com/search?sourceid=navclient&ie=UTF-8&oe=UTF-8&q=chatserver+chatclient+java
 

Posts:16
Registered: 7/31/04
Re: Detecting Connections on a socket  
Aug 1, 2004 3:23 PM (reply 2 of 2)



 
heh well thats why i came here... figured this was a common program that people make... didnt know if anyone had suggestions... im doing this mainly to learn more about streams and multithreading... i made a chat program once before but it didnt list the client logged in... thats what im trying to accomplish now...
 
This topic has 2 replies on 1 page.