Results 1 to 3 of 3
Thread: MultiThreading Chat Program
- 12-08-2009, 02:02 PM #1
Member
- Join Date
- Dec 2009
- Posts
- 12
- Rep Power
- 0
MultiThreading Chat Program
Hi i am creating a java chat application i have a client and a server however the chat application has to host many clients, i was just wondering where do i need to insert the multithreading code and what code do i need to use for this to work in the following code.
Server
public class ChatServer extends javax.swing.JFrame {
private ServerSocket server;
private Socket connection;
int counter = 1;
private ObjectInputStream input;
private ObjectOutputStream output;
/** Creates new form ChatServer */
public ChatServer() {
initComponents();
jTextField1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent event){
sendData(event.getActionCommand());
jTextField1.setText("");
}
}
);
// runServer();
}
public void runServer(){
try
{
server = new ServerSocket( 12345, 2 ); // set up ServerSocket
while (true){
try{
waitForConnection();
getStreams();
processConnection();
}//end try
catch (EOFException eofException)
{
displayMessage("\nServer terminated connection");
}
finally
{
closeConnection();
counter++;
}
}
} // end try
catch ( IOException ioException )
{
ioException.printStackTrace();
} // end catch
//jTextArea1.setText( "Server awaiting connections\n" );
}
private void waitForConnection() throws IOException{
displayMessage("Server awaiting connections\n");
connection= server.accept();
displayMessage("Connection" +counter+"received from: " + connection.getInetAddress().getHostName());
}
private void getStreams() throws IOException{
output = new ObjectOutputStream(connection.getOutputStream());
output.flush();
input = new ObjectInputStream(connection.getInputStream());
displayMessage("\nGot I/O streams\n");
}
private void processConnection() throws IOException{
String message = "Connection Successful";
sendData(message);
do
{
try{
message = (String)input.readObject();
displayMessage("\n"+message);
} catch(ClassNotFoundException cnfException){
displayMessage("\nunknown object type");
}
}while (!message.equals("Terminate"));
}
private void closeConnection(){
displayMessage("\nterminating Connection");
try{
output.close();
input.close();
connection.close();
}catch(IOException ioException){
ioException.printStackTrace();
}
}
private void sendData(String message){
try{
output.writeObject("Server: "+message);
output.flush();
displayMessage("\nServer>>>"+message);
}catch(IOException ioException){
jTextArea1.append("\nError writing object");
}
}
private void displayMessage(final String messageToDisplay){
SwingUtilities.invokeLater(new Runnable(){
public void run(){
jTextArea1.append(messageToDisplay);
}
});
}
/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
public static void main(String args[]) {
ChatServer s= new ChatServer();
s.setVisible(true);
s.runServer();
/* java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
// new ChatServer().setVisible(true);
}
});*/
}
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jLabel1 = new javax.swing.JLabel();
jScrollPane1 = new javax.swing.JScrollPane();
jTextArea1 = new javax.swing.JTextArea();
jTextField1 = new javax.swing.JTextField();
setDefaultCloseOperation(javax.swing.WindowConstan ts.EXIT_ON_CLOSE);
setTitle("Server");
jLabel1.setText("Enter Your Message Here:");
jTextArea1.setColumns(20);
jTextArea1.setRows(5);
jScrollPane1.setViewportView(jTextArea1);
jTextField1.setText("jTextField1");
org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(org.jdesktop.layout.Gro upLayout.LEADING)
.add(layout.createSequentialGroup()
.add(layout.createParallelGroup(org.jdesktop.layou t.GroupLayout.LEADING)
.add(layout.createSequentialGroup()
.addContainerGap()
.add(jScrollPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 380, Short.MAX_VALUE))
.add(layout.createSequentialGroup()
.add(18, 18, 18)
.add(layout.createParallelGroup(org.jdesktop.layou t.GroupLayout.LEADING)
.add(jTextField1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 352, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
.add(jLabel1))))
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(org.jdesktop.layout.Gro upLayout.LEADING)
.add(layout.createSequentialGroup()
.addContainerGap()
.add(jLabel1)
.add(18, 18, 18)
.add(jTextField1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 35, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
.add(45, 45, 45)
.add(jScrollPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 166, Short.MAX_VALUE)
.addContainerGap())
);
pack();
}// </editor-fold>
- 12-23-2009, 11:01 AM #2
Member
- Join Date
- Sep 2009
- Posts
- 41
- Rep Power
- 0
Writing the Server Side of a Socket (The Java™ Tutorials > Custom Networking > All About Sockets) check that out, should have the info you need.
- 12-23-2009, 11:49 AM #3
Member
- Join Date
- Dec 2009
- Location
- Rio de Janeiro
- Posts
- 38
- Rep Power
- 0
Similar Threads
-
Multithreaded Client/Server Chat program
By f0ns in forum Threads and SynchronizationReplies: 3Last Post: 10-21-2009, 06:26 PM -
Help with chat client program
By srivigneshwar in forum New To JavaReplies: 1Last Post: 04-03-2009, 07:32 PM -
simple chat program
By munishmhr in forum NetworkingReplies: 2Last Post: 03-25-2009, 05:00 PM -
Develop chat program using SCTP protocol
By rams in forum NetworkingReplies: 0Last Post: 11-03-2008, 06:07 AM -
Java Program chat
By susan in forum Advanced JavaReplies: 1Last Post: 07-25-2007, 10:05 PM
Bookmarks