Important alert: (current site time 7/15/2013 3:35:49 PM EDT)
 

VB icon

A Chat server

Email
Submitted on: 5/28/2002 2:48:28 PM
By: Ali Ezad Abbas  
Level: Intermediate
User Rating: By 3 Users
Compatibility: Java (JDK 1.2)
Views: 33791
(About the author)
 
     a chat server. use it in conjunction with the chat client also available on this site!!
 
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
 
Terms of Agreement:   
By using this code, you agree to the following terms...   
  1. You may use this code in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.
  2. You MAY NOT redistribute this code (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.   
  3. You may link to this code from another website, but ONLY if it is not wrapped in a frame. 
  4. You will abide by any additional copyright restrictions which the author may have placed in the code or code's description.
				
//**************************************
// Name: A Chat server
// Description:a chat server. use it in conjunction with the chat client also available on this site!!
// By: Ali Ezad Abbas
//
//This code is copyrighted and has// limited warranties.Please see http://www.Planet-Source-Code.com/vb/scripts/ShowCode.asp?txtCodeId=2932&lngWId=2//for details.//**************************************

import java.io.*;
import java.net.*;
import java.util.*;
import java.awt.*;
import java.awt.event.*;
public class server extends Frame {
 public static TextArea display;
 public static server a;
 public static int portNumber, maxClients;
 public static void main (String [] args) {
BufferedReader stdin = new BufferedReader
 (new InputStreamReader (System.in));
try {
 System.out.print("Give the port number to listen for requests: ");
 System.out.flush();
 portNumber = Integer.parseInt(stdin.readLine());
 System.out.print("Give the maximum number of clients for this server: ");
 System.out.flush();
 maxClients = Integer.parseInt(stdin.readLine());
} catch (IOException ioe) {System.out.println(ioe);}
a = new server();
a.show();
a.startServer();
 }
 public ServerSocket connect;
 public Vector sockets;
 public TextField input;
 public server () {
setSize(780,550);
setTitle("server");
setLayout(null);
addWindowListener(new closeListener());
display = new TextArea();
display.setEditable(false);
input = new TextField();
input.addActionListener(new al());
add(input);
add(display);
display.setBounds(10,30,760,489);
input.setBounds(10,520,760,20);
sockets = new Vector();
try {
 connect = new ServerSocket(portNumber,maxClients);
} catch (IOException ioe) {System.out.println(ioe);}
 }
 public void startServer () {
try {
 while (true) {
 sockets.addElement(new ThreadedSocket(connect.accept()));
 Thread.yield();
 }
} catch (Exception e) {System.out.println(e);}
 }
 public static synchronized void echoToAllClients (String s) {
try {
 for (int i=0;i<a.sockets.size();i++) {
 ThreadedSocket temp = (ThreadedSocket)a.sockets.elementAt(i);
 temp.out.writeObject(s);
 }
} catch (Exception e) {}
 }
 private class closeListener extends WindowAdapter {
public void windowClosing (WindowEvent e) {
 System.exit(0);
}
 }
 private class al implements ActionListener {
public void actionPerformed (ActionEvent e) {
 String message = "<Server> "+input.getText();
 input.setText("");
 display.append(message+"\n");
 echoToAllClients(message);
}
 }
}
class ThreadedSocket extends Thread {
 public Socket socket;
 public ObjectInputStream in;
 public ObjectOutputStream out;
 public String nick;
 public ThreadedSocket (Socket s) {
socket = s;
try {
 out = new ObjectOutputStream(socket.getOutputStream());
 out.flush();
 in = new ObjectInputStream(socket.getInputStream());
} catch (Exception e) {System.out.println(e);}
nick = "";
start();
 }
 public void run () {
String message = "";
int flag = 0;
while (!message.endsWith("quit")) {
 try {
 message = (String)in.readObject();
 } catch (Exception e) {System.out.println(e);}
 if (flag == 0) {
 nick = message;
 server.display.append("* "+nick+" ["+socket.getInetAddress().getHostName()+"] has joined the server *\n");
 server.echoToAllClients("* "+nick+" ["+socket.getInetAddress().getHostName()+"] has joined the server *");
 server.display.append("* "+nick+" welcome to Ezad's Chat Server (c) 2000-01 Ali Ezad Abbas *\n");
 server.echoToAllClients("* "+nick+" welcome to Ezad's Chat Server (c) 2000-01 Ali Ezad Abbas *");
 flag = 1;
 } else {
 server.display.append(message+"\n");
 server.echoToAllClients(message);
 }
 Thread.yield();
}
try{
 server.echoToAllClients("* Connection Closed by "+nick+" *");
 in.close();
 out.close();
 socket.close();
 server.display.append("* Connection Closed by "+nick+" *\n");
} catch (Exception e) {System.out.println(e);}
 }
}


Other 18 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Intermediate category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments

6/24/2002 3:30:47 PMFranz

Hi,
Compiled this program with the following error message :
Results of execution with 'javac':

chat.java:28: class server is public, should be declared in a file named server.java
public class server extends Frame {
^
1 error

Why ?
(If this comment was disrespectful, please report it.)

 
7/3/2002 3:00:25 AMHwang

Simple Good, but Why Frame?
System Object is better good.

(If this comment was disrespectful, please report it.)

 
7/10/2002 8:14:17 AMAllen2002

It should explain how to run/use it.
(If this comment was disrespectful, please report it.)

 
7/17/2002 12:06:08 AMTrelan

i am new to this site and i am trying to create a chat server for a class excersize, but God help me because i needed help. i hope this code gives me some ideas
(If this comment was disrespectful, please report it.)

 
12/4/2002 11:40:16 AMJosé Adán Veduzco Amezcua

How i can use it?
(If this comment was disrespectful, please report it.)

 
8/28/2003 6:57:31 AM

Hi Ali Ezad Abbas, I have created a chatting system on the java, application that I used to create is IBM Visual age for java 3.2. When I test this application on IBM visual age it's OK, but when I compile it to class file and run on Internet Explorer, I cann't connect from chat's client to server. I think the problem may be from Socket, so can u explain me about that.
Best Reguard!
(If this comment was disrespectful, please report it.)

 
1/7/2005 4:27:54 AM

The source code examples in this site are very usefull.

(If this comment was disrespectful, please report it.)

 
3/2/2005 10:52:44 PM

ewr
(If this comment was disrespectful, please report it.)

 
3/29/2006 10:30:27 AMeurotech

hy there. i'm asking you help with one of your older apps. the chat server (and client). i dont seem to know how to make them run; see this output, pls.
Give the port number to listen for requests: 8080
Give the maximum number of clients for this server: 10
Exception in thread "main" java.lang.VerifyError: (class: rac_chat/ThreadedSocket, method: run signature: ()V) Incompatible object argument for function call
... what is the problem, pls ?
at rac_chat.server.startServer(server.java:59)

at rac_chat.server.main(server.java:30)

(If this comment was disrespectful, please report it.)

 
5/29/2006 11:58:29 PMLK

Good! Bien!
keep goin! Siga adelante!
(If this comment was disrespectful, please report it.)

 
7/19/2006 12:57:42 PM

how to run this chatserver?pls email me for the answer....?samicole246@yahoo.com
(If this comment was disrespectful, please report it.)

 
7/2/2007 1:20:55 AMhamdi

cool
(If this comment was disrespectful, please report it.)

 
4/30/2010 5:49:03 PMtaher

thank you
(If this comment was disrespectful, please report it.)

 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.