Go Back   CodingForums.com > :: Server side development > Java and JSP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 01-12-2012, 05:02 PM   PM User | #1
thornt5748
New to the CF scene

 
Join Date: Oct 2011
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
thornt5748 is an unknown quantity at this point
TCP/IP Client-Server Chat Program

Hi Guys,

I'm quite new here and am new to Java Programming, but i could use some help. I have a client-serve chat program and have almost finished it to my standards however I am trying to enable the server to accept multiple clients, how would i do this? what do i have to add or change on the server to allow this?

Server
Code:
import java.awt.*;
import java.net.*;
import java.awt.*;          
import java.awt.event.*;    
import javax.swing.*;       
import javax.swing.event.*;
import java.io.*;

public class SwingChatServer extends SwingChatGUI
{
	public String outline;
    public String fromClient, s = "";
	PrintWriter out;
	BufferedReader in;
	BufferedReader stdin;
	String inputLine, outputLine;
	public ButtonHandler bHandler = new ButtonHandler();
    
	public SwingChatServer (String title)
	{
		super (title);
		bHandler = new ButtonHandler ();
		sendButton.addActionListener (bHandler);
	}

	private class ButtonHandler implements ActionListener
	{
		public void actionPerformed (ActionEvent event)
		{
			outputLine = txArea.getText ();
			System.out.println ("Server :> " + outputLine);
			out.println (outputLine);
			outline = txArea.getText ();
			s = "Server :>"+ outline+"\n";
			rxArea.append(s);
			txArea.setText("");
    	}
	}
    
	public void run () throws IOException
	{
		ServerSocket serverSocket = null;
       
		try
        {
        	serverSocket = new ServerSocket (4444);
        }
        catch (IOException e)
        {
        	System.err.println ("Could not listen on port: 4444.");
        	System.exit (1);
        }

        Socket clientSocket = null;
        try
        {
        	clientSocket = serverSocket.accept ();
        }
        catch (IOException e)
        {
        	System.err.println ("Accept failed.");
        	System.exit(1);
        }

    	out = new PrintWriter (clientSocket.getOutputStream (), true);
    	in = new BufferedReader (new InputStreamReader (clientSocket.getInputStream ()));
    	//stdin = new BufferedReader (new InputStreamReader (System.in));

        out.println ("Welcome to the Chat Server");

		
		while ((fromClient = in.readLine ()) != null) //allows a new line to not over write the chat
		{
			System.out.println ("Client :> " + fromClient);
			s = "Client :>" + fromClient + "\n";
			rxArea.append (s);
			
			if (fromClient.equals ("Bye"))System.exit(0);

		}
		
        out.close();
        in.close();
        clientSocket.close();
        serverSocket.close();
    }

	public static void main(String[] args) //throws IOException
    {

 		SwingChatServer frame = new SwingChatServer ("Chat Server Program");
		
		frame.pack ();
		frame.setVisible(true);
		frame.setIconImage(Toolkit.getDefaultToolkit().getImage("chat.jpg"));
        frame.setLocationRelativeTo(null);
        frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
		try
		{
			frame.run ();
		}
		catch (IOException e)
		{
			System.err.println("Couldn't get I/O for the connection to: 194.81.104.118!\n");
			System.exit(1);
		}

    } 
}
thornt5748 is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 11:06 PM.


Advertisement
Log in to turn off these ads.