Topic
  • 2 replies
  • Latest Post - ‏2011-06-08T11:41:20Z by gauz
SystemAdmin
SystemAdmin
2768 Posts

Pinned topic chat server

‏2003-06-12T07:18:59Z |
ok i can run the chat server....but however when run the server and let other user to connect to my chat server...they cannot connect...from the internet. they said they only see a grey box and the status bar display done at the bottom statusbar...what is the problem??

here my code:

Server.java

import java.net.*;
import java.io.*;
import java.util.*;

public class Server {

public static void main ( String args[] ) throws IOException
{
ServerSocket server = new ServerSocket( 10000 );
while( true )
{
System.out.println( "Waiting for client connection..." );
Socket client = server.accept();
System.out.println( "New connection from " + client.getInetAddress());

ThreadHandler t = new ThreadHandler( client );
t.start();
}
}
}

ChatClient.java

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

public class ChatClient extends JApplet implements ActionListener, Runnable {
// Gui components
Container c;
JPanel chatPanel, chatPanel_west;
JTextField chatText;
JTextArea chatArea;
JList userList;

// Network components
private Socket client;
private DataOutputStream dout;
private DataInputStream din;

public void init()
{
c = getContentPane();

chatPanel = new JPanel();
chatPanel_west = new JPanel();
chatPanel.setLayout( new BorderLayout() );
chatPanel_west.setLayout( new BorderLayout() );
chatText = new JTextField();
chatText.addActionListener( this );
chatText.setActionCommand( "Chat Text" );
chatArea = new JTextArea();
chatArea.setEditable( false );
userList = new JList();
userList.setFixedCellWidth( 100 );
chatPanel_west.add( BorderLayout.SOUTH, chatText );
chatPanel_west.add( BorderLayout.CENTER, new JScrollPane( chatArea ) );
chatPanel.add( BorderLayout.CENTER, chatPanel_west );
chatPanel.add( BorderLayout.EAST, new JScrollPane( userList ) );

c.add( chatPanel );
setSize( 800, 550 );
show();

connToServer();
}

private void connToServer()
{
// Connecting to the server
try
{
// Initiate the connection
client = new Socket( "203.117.52.79", 10000 );

// Connection successful
System.out.println( "Successfully connected to " + client );

// Create DataInput/Output stream
din = new DataInputStream( client.getInputStream() );
dout = new DataOutputStream( client.getOutputStream() );

// Start a background thread for receiving messages
new Thread( this ).start();
} catch( IOException ie ) { System.out.println( ie ); }
}

private void processMessage( String message )
{
try
{
// Send data to the server
dout.writeUTF( message );

// Clear out text in textfield
chatText.setText( "" );
} catch( IOException ie ) { System.out.println( ie ); }
}

public void run()
{
try
{
while( true )
{
// Get the next message
String message = din.readUTF();

if( message.startsWith( "USERLIST" ) )
{
String theUserList = message.substring( 9 );
String userName;
Vector userListing = new Vector();
for( int i = 0; i <= theUserList.length(); i++ )
{
if( theUserList.regionMatches( i, ",", 0, 1 ) )
{
userName = theUserList.substring( 0, i );
userListing.addElement( userName );
theUserList = theUserList.substring( i + 1 );
}

}

userList.setListData( userListing );
}

chatArea.append( message + "\n");

chatArea.setCaretPosition( chatArea.getText().length() );
}
} catch( IOException ie ) { System.out.println( ie ); }
}

public void actionPerformed( ActionEvent e )
{
if( e.getActionCommand().equals( "Chat Text" ) )
{
processMessage( chatText.getText() );
}
}
}

ThreadHandler.java

import java.net.*;
import java.io.*;
import java.util.*;

public class ThreadHandler extends Thread {
protected Socket client;
protected DataInputStream in;
protected DataOutputStream out;
String name, preName, result, toUser;
protected static int i;

public ThreadHandler ( Socket client ) throws IOException
{
this.client = client;
in = new DataInputStream( new BufferedInputStream( client.getInputStream() ) );
out = new DataOutputStream( new BufferedOutputStream( client.getOutputStream() ) );
}

protected static List userInfo = new Vector();

public void run()
{
try
{
name = "no_nick";
userInfo.add( new UserInfo(this, name, "" ) );

while( true )
{
preName = name;
result = "";
String message = in.readUTF();
if( message.startsWith( "NICKNAME" ) )
{
name = message.substring( 9 );

for( int ii = 0; ii < userInfo.size(); ii ++ )
{
if ( ( ( UserInfo )userInfo.get( ii) ).userName.equals( name ) )
{
out.writeUTF( "nickname have been chosen.Please enter another nickname" );
out.flush();
result = "no";
name = preName;
break;
}
}
if( result != "no" )
{
broadcast ( preName + " nickname changed to " + name );
for( int ii = 0; ii < userInfo.size(); ii ++ )
{
if ( ( ( UserInfo )userInfo.get( ii) ).handler.equals( this ) )
{
userInfo.remove( ii );
}
}
userInfo.add( new UserInfo(this, name, "" ) );
}
}
else if( message.startsWith( "WHISPER" ) )
{
for( int i = 8; i <= message.length(); i++ )
{
if( message.regionMatches( i, ":", 0, 1 ) )
{
toUser = message.substring( 8, i );
// put private chat code here
message = message.substring( i + 1 );
synchronized( userInfo )
{
int numUsers = userInfo.size();
for( int ii = 0; ii < numUsers; ii ++ )
{
if( ( ( UserInfo )userInfo.get( ii) ).userName.equals( toUser ) )
{
ThreadHandler handler = ( ( UserInfo )userInfo.get( ii) ).handler;

ThreadHandler th = (ThreadHandler) handler;
try
{
synchronized( th.out)
{
th.out.writeUTF( message );
}
th.out.flush();
} catch( IOException ie ) { th.stop(); }
break;
}
}
}
out.writeUTF( message );
out.flush();
break;
}

}
}
else if( message.equals( "USERLIST" ) )
{
String userList = "USERLIST:";

for( int ii = 0; ii < userInfo.size(); ii ++ )
{
userList = userList + ( ( UserInfo )userInfo.get( ii) ).userName + ",";
}

out.writeUTF( userList );
out.flush();
}
else
{
broadcast( name + ":" + message );
}
}
} catch( IOException ie ) { ie.printStackTrace(); }
//finally { userInfo.remove( i - 1 ); }
}

private static void broadcast ( String message )
{

synchronized( userInfo )
{
int numUsers = userInfo.size();
for( int i = 0; i < numUsers; i ++ )
{
ThreadHandler handler = ( ( UserInfo )userInfo.get( i ) ).handler;

ThreadHandler th = (ThreadHandler) handler;
try
{
synchronized( th.out)
{
th.out.writeUTF( message );
}
th.out.flush();
} catch( IOException ie ) { th.stop(); }
}
}
}
}

UserInfo.java

import java.net.*;

public class UserInfo {

ThreadHandler handler;
String userName, channel;

public UserInfo( ThreadHandler handler, String userName, String channel )
{
this.handler = handler;
this.userName = userName;
this.channel = channel;
}
}
Updated on 2011-06-08T11:41:20Z at 2011-06-08T11:41:20Z by gauz
  • SystemAdmin
    SystemAdmin
    2768 Posts

    Re: chat server

    ‏2003-06-16T15:15:05Z  
    I did a quick test changing your ChatClient class, so that the JApplet runs inside a JFrame and uses localhost, for testing. Actually, this was not so quick because you didn't use the code tags to format your source. That made for work. Regardless, the ChatClient was able to connect to your Server class.

    Therefore, I suspect that either your HTML or the clients aren't set up properly to run JApplets. So, my suggestion is to read over How to Make Applets and Running Swing Applets in The Java Tutorial. These sections discuss, and have links for dealing with, the Java Plug-in and setting up for it on both ends. If you've already ensured these parts, the other thing I can think of is that applets can only connect to the server from which they originated. Is that 203.117.52.79? And have you tested this locally?
  • gauz
    gauz
    1 Post

    Re: chat server

    ‏2011-06-08T11:41:20Z  
    I did a quick test changing your ChatClient class, so that the JApplet runs inside a JFrame and uses localhost, for testing. Actually, this was not so quick because you didn't use the code tags to format your source. That made for work. Regardless, the ChatClient was able to connect to your Server class.

    Therefore, I suspect that either your HTML or the clients aren't set up properly to run JApplets. So, my suggestion is to read over How to Make Applets and Running Swing Applets in The Java Tutorial. These sections discuss, and have links for dealing with, the Java Plug-in and setting up for it on both ends. If you've already ensured these parts, the other thing I can think of is that applets can only connect to the server from which they originated. Is that 203.117.52.79? And have you tested this locally?
    since this post is related to chat server,
    can someone pleasesuggest mesome tutorials or quick reference guide for making an efficient chat server.