Sie sind auf Seite 1von 41

The Server Code consists of 7 files : 1. 2. 3. 4. 5. 6. 7. ChatServer.java Servicer.java Connections.java Channel.java ChannelList.java Ping.java UserInfo.

java

Previous Page

ChatServer.java
import java.net.*; import java.io.*; import java.util.*; public class ChatServer { ServerSocket serverSocket = null; static Connections conn; static ChannelList clist; public static void main(String args[]) { new ChatServer(); } public ChatServer() { conn = new Connections(); clist = new ChannelList(); clist.addChannel("General Chat", "Talk about all things general, particularly the Universe"); clist.addChannel("Music", "Talk about Music"); clist.addChannel("Science & Technology", "Does anybody want to get on this bus??"); try { serverSocket = new ServerSocket(2000); } catch (IOException e) { System.out.println("Could not listen on port: " + 2000 + ", " + e); System.exit(1); } System.out.println("For ServerSocket: " + serverSocket.toString());

System.out.println("Server stared"); while(true) { Socket clientSocket = null; try { clientSocket = serverSocket.accept(); System.out.println("Client connected"); } catch (IOException e) { System.out.println("Accept failed: " + 2000 + ", " + e); System.exit(1); } new Servicer(clientSocket, conn, clist).start(); } } }

Back to starting of page

Servicer.java
import java.net.*; import java.io.*; import java.util.*; public class Servicer extends Thread { Socket s; Connections cn; ChannelList clist; DataInputStream is; DataOutputStream os; String nameOfUser = new String(""); String loginName = new String(""); boolean isKilled = false; Ping ping; public Servicer(Socket c, Connections con, ChannelList cl) { this.s = c; this.cn = con; this.clist = cl; } public void run() { String[] tokens; String startChannel = new String("General Chat"); String quit = new String("q:"); String mesg = new String("m:");

String String String String String String String String String String String String String String String String String String String String UserInfo

nm change_channels quit_channel whoInMyChannel changeChannelStatus channelDescription channelList bcast success failure pingPacket inputLine outputLine tmp otherInfo list message tmpName channel tmpStr tmpObj;

= = = = = = = = = = = = = = = = = = = =

new new new new new new new new new new new new new new new new new new new new

String("n:"); String("c:"); String("Q:"); String("w:"); String("C:"); String("d:"); String("L:"); String("b:"); String("Y"); String("N"); String("P"); String(); String(); String(); String(); String(); String(); String(); String(); String();

try { is = new DataInputStream(new BufferedInputStream(s.getInputStream())); os = new DataOutputStream(s.getOutputStream()); } catch (IOException e) { System.out.println("While getting Streams:"); System.out.println(e); } catch (NullPointerException npe) { System.out.println("Caught NPE:"); System.out.println(npe); } while(true) { try { inputLine = is.readLine(); System.out.println("inputline: "+inputLine); } catch (IOException e) { break; } if(!inputLine.startsWith("P") && !inputLine.startsWith("U:")) { System.out.println("RECD#" + inputLine); } if(ping != null) { ping.setTrue(); }

if(inputLine.equals(pingPacket)) { ping.setTrue(); continue; } if(isKilled) { break; } if(inputLine.startsWith("$")) { otherInfo=inputLine.substring(2); tokens = split(otherInfo, ":", 2); tmpName=tokens[0]; message=tokens[1]; if((tmpObj=cn.getObjectofUser(tmpName))==null) { continue; } else { tmp="$"+":"+nameOfUser+":"+message; writeNetOutput(tmpObj.input,tmpObj.output,tmp,tmpObj.username); } } if(inputLine.startsWith(mesg)) { message = new String(inputLine.substring(2)); tmp = mesg + nameOfUser + ":" + message; multicast(tmp, channel, nameOfUser); } else if(inputLine.startsWith(nm)) { channel = startChannel; otherInfo = inputLine.substring(2); tokens = split(otherInfo, ":", 2); nameOfUser = tokens[0]; loginName = tokens[1]; if(cn.doesUserExist(nameOfUser)) { tmp = nm + nameOfUser + ":" + failure; writeNetOutput(is, os, tmp, nameOfUser); break; } cn.putUser(nameOfUser, loginName, channel, is, os); tmp = change_channels + nameOfUser; multicast(tmp, channel, nameOfUser); tmp = nm + nameOfUser + ":" + success; writeNetOutput(is, os, tmp, nameOfUser); list = cn.getUsernamesInChannel(channel); tmp = whoInMyChannel + list; writeNetOutput(is, os, tmp, nameOfUser);

tmp = channelDescription + clist.getChannelDescription(channel); writeNetOutput(is, os, tmp, nameOfUser); tmp = channelList + clist.getChannelList(); writeNetOutput(is, os, tmp, nameOfUser); ping = new Ping(cn, nameOfUser, s, this, channel); ping.start(); } else if(inputLine.startsWith(change_channels)) { tmpStr = inputLine.substring(2); tmp = quit_channel + nameOfUser; multicast(tmp, channel, nameOfUser); channel = tmpStr; cn.changeChannel(nameOfUser, channel); tmp = changeChannelStatus + success; writeNetOutput(is, os, tmp, nameOfUser); list = cn.getUsernamesInChannel(channel); tmp = whoInMyChannel + list; writeNetOutput(is, os, tmp, nameOfUser); tmpStr = clist.getChannelDescription(channel); tmp = channelDescription + tmpStr; writeNetOutput(is, os, tmp, nameOfUser); tmp = change_channels + nameOfUser; multicast(tmp, channel, nameOfUser); } else if(inputLine.startsWith(quit)) { ping.stop(); cn.killUser(nameOfUser); tmp = quit + nameOfUser; multicast(tmp, channel, nameOfUser); break; } } try { is.close(); os.close(); s.close(); } catch (IOException e) { System.out.println("While Closing:"); System.out.println(e); } } private void writeNetOutput(DataInputStream is, DataOutputStream os, String str, String toWhom) { String tmp_NL = new String(); String NL = new String("\n"); tmp_NL = str.concat(NL); if(!str.startsWith("U:"))

{ System.out.println("SENT for " + toWhom + "#" + str); } try { os.writeChars(tmp_NL); os.flush(); } catch (IOException e) { cn.killUser(toWhom); isKilled = true; } } void multicast(String multicastMessage, String channel, String name) { Vector users = new Vector(); UserInfo tmpU; users = cn.getAllUsersInChannel(channel); int size = users.size(); for(int i = 0; i < size; i++) { tmpU = (UserInfo)users.elementAt(i); if(!(tmpU.username.equals(name))) { writeNetOutput(tmpU.input, tmpU.output, multicastMessage, tmpU.username); } } } void multicast(String multicastMessage, String channel) { Vector users = new Vector(); UserInfo tmpU; users = cn.getAllUsersInChannel(channel); int size = users.size(); for(int i = 0; i < size; i++) { tmpU = (UserInfo)users.elementAt(i); writeNetOutput(tmpU.input, tmpU.output, multicastMessage, tmpU.username); } } void broadcast(String broadcastMessage) { Vector users = new Vector(); UserInfo tmpU; users = cn.getObjectsOfAllUsers(); int size = users.size(); for(int i = 0; i < size; i++) { tmpU = (UserInfo)users.elementAt(i); writeNetOutput(tmpU.input, tmpU.output, broadcastMessage, tmpU.username);

} } String[] split(String toBeSplit, String delim) { Vector v = new Vector(); StringTokenizer t = new StringTokenizer(toBeSplit, delim); while(t.hasMoreElements()) { v.addElement(t.nextElement()); } v.trimToSize(); String[] s = new String[v.size()]; int i = 0; while(i < v.size()) { s[i] = v.elementAt(i).toString(); i++; } return(s); } String[] split(String toBeSplit, String delim, int howMany) { Vector v = new Vector(); StringTokenizer t = new StringTokenizer(toBeSplit, delim); while(t.hasMoreElements()) { v.addElement(t.nextElement()); } v.trimToSize(); String[] s = new String[howMany]; int i = 0; while(i < (howMany - 1)) { s[i] = v.elementAt(i).toString(); i++; } int length = 0; i = 0; while(i < (howMany - 1)) { length += s[i].length(); i++; } length += (howMany - 1); s[(howMany - 1)] = toBeSplit.substring(length); return(s); } }

Back to starting of page

Connections.java
import java.net.*; import java.io.*; import java.util.*; public class Connections { Vector users; public Connections() { users = new Vector(); } boolean doesUserExist(String name) { UserInfo tmp; int count = 0; int size = users.size(); while(count < size) { tmp = (UserInfo)users.elementAt(count); if(tmp.username.equals(name)) { return(true); } count++; } return(false); } void putUser(String str, String lstr, String channel, DataInputStream in, DataOutputStream out) { users.addElement(new UserInfo(str, lstr, channel, in, out)); users.trimToSize(); } String getUsernamesInChannel(String ch) { UserInfo tmp; String unames = new String(); int count = 0; int size = users.size(); while(count < size) { tmp = (UserInfo)users.elementAt(count); if(tmp.chatroom.equals(ch)) { unames += tmp.username; unames += ":"; }

count++; } return(unames); } UserInfo getObjectofUser(String name) { int size = users.size(); UserInfo tmp; for(int i = 0; i < size; i++) { tmp = (UserInfo)users.elementAt(i); if(tmp.username.equals(name)) { return(tmp); } } return(null); } Vector getAllUsersInChannel(String channel) { UserInfo tmp; Vector u = new Vector(); int size = users.size(); for(int i = 0; i < size; i++) { tmp = (UserInfo)users.elementAt(i); if(tmp.chatroom.equals(channel)) { u.addElement(tmp); } } u.trimToSize(); return(u); } Vector getObjectsOfAllUsers() { return(users); } String getAllUsers() { UserInfo tmp; String list = new String(); int size = users.size(); for(int i = 0; i < size; i++) { tmp = (UserInfo)users.elementAt(i); list = list + tmp.username + "#" + tmp.chatroom + ":"; } return(list); } void changeChannel(String name, String channel) {

UserInfo tmp ; int size = users.size(); for(int i = 0; i < size; i++) { tmp = (UserInfo)users.elementAt(i); if(tmp.username.equals(name)) { tmp.chatroom = channel; System.out.println("For " + name + " New channel is " + tmp.chatroom); users.removeElementAt(i); users.addElement(tmp); break; } } users.trimToSize(); } void killUser(String name) { UserInfo tmp; int size = users.size(); for(int i = 0; i < size; i++) { tmp = (UserInfo)users.elementAt(i); if(tmp.username.equals(name)) { System.out.println("Killing user " + tmp.username); users.removeElementAt(i); break; } } users.trimToSize(); } }

Back to starting of page

Channel.java
import java.net.*; import java.io.*; import java.util.*; public class Channel { public String name; public String description;

public Channel(String n, String d) { this.name = n; this.description = d; } }

Back to starting of page

ChannelList.java
import java.net.*; import java.io.*; import java.util.*; public class ChannelList { Vector Channels; public ChannelList() { Channels = new Vector(); } void addChannel(String n, String d) { Channels.addElement(new Channel(n, d)); Channels.trimToSize(); } String getChannelList() { Channel tmp; int size = Channels.size(); String ret = new String(); for(int i = 0; i < size; i++) { tmp = (Channel)Channels.elementAt(i); ret += tmp.name + ":"; } return(ret); } String getChannelDescription(String c) { Channel tmp; int size = Channels.size(); String ret = new String(); for(int i = 0; i < size; i++) {

tmp = (Channel)Channels.elementAt(i); if(tmp.name.equals(c)) { return(tmp.description); } } return(null); } }

Back to starting of page

Ping.java
import java.net.*; import java.io.*; import java.util.*; public class Ping extends Thread { boolean isPinged; public Connections c; Socket mySocket; Servicer myServicer; String me; String myChannel; public Ping(Connections cs, String name, Socket s, Servicer sr, String c) { this.c = cs; this.me = name; this.mySocket = s; this.myServicer = sr; this.myChannel = c; this.isPinged = true; } public void setTrue() { isPinged = true; } public void setFalse() { isPinged = false; } public boolean isTrue()

{ return(isPinged); } public void run() { while(true) { try { this.sleep(30000); } catch(InterruptedException e) { System.out.println("SLEEP Interrupted"); } isTrue()) { setFalse(); } else { String tmp = new String(); tmp = "q:" + me; System.out.println(me + " has not been pinged"); myServicer.multicast(tmp, myChannel, me); c.killUser(me); try { System.out.println("Before being killed: " + mySocket.toString()); mySocket.close(); } catch (IOException e) { System.out.println("While Closing:"); System.out.println(e); } break; } } } }

Back to starting of page

UserInfo.java
import java.net.*;

import java.io.*; import java.util.*; public class UserInfo { public String username; public String loginname; public String chatroom; public DataInputStream input; public DataOutputStream output; public UserInfo(String name, String lname, String c, DataInputStream in, DataOutputStream out) { this.username = name; this.loginname = lname; this.chatroom = c; this.input = in; this.output = out; } }

Back to starting page

Back to Main page

The Client Code consists of 7 files : 1. 2. 3. 4. 5. 6. 7. Conference.java IntroCanvas.java ConfDraw.java ConnectProcessing.java ConfUsersDraw.java RefreshUsers.java PingChatServer.java

Previous Page

Conference.java
import java.applet.Applet; import java.awt.*; import java.util.*; public class Conference extends Applet { ConfDraw confdraw; IntroCanvas introcanvas; public String host=new String(" "); public void init() { host=getParameter("IP_Address"); this.setBackground(new Color(250,0,0)); introcanvas = new IntroCanvas(this); introcanvas.show(); introcanvas.toFront(); } public void destroy() { introcanvas.dispose(); } }

Back to starting of page

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

public class IntroCanvas extends Frame { public ConfDraw confdraw; public Conference conference; Label label_name; Label label_nick; Label label_logo; Label label_copywrite; Label label_enter; Label label_register; TextField ts0,ts1; Button sbutton; public String Name = new String(""); public String Nick = new String(""); public Color backcolor,stripcolor; public IntroCanvas(Conference c) { super("Chatroom Login"); this.conference = c; setLayout(null); backcolor = new Color(131,146,214); stripcolor= new Color(0,0,160); this.setBackground(backcolor); addNotify(); reshape(insets().left + 45,30,insets().left+insets().right + 498,insets().top + insets().bottom + 335); this.setResizable(false); label_logo = new Label("Cyber Chat",Label.CENTER); add(label_logo); label_logo.setFont(new Font("Helvetica",Font.BOLD+ Font.ITALIC ,45)); label_logo.setForeground(Color.cyan); label_logo.setBackground(stripcolor); label_logo.reshape(insets().left + 0,insets().top + 30,500,50); label_copywrite = new Label("Copyright 1999 Anuja,CV,Hiren,Neeraj",Label.CENTER); add(label_copywrite); label_copywrite.setFont(new Font("Helvetica",Font.PLAIN,11)); label_copywrite.setForeground(Color.black); label_copywrite.reshape(insets().left + 0,insets().top + 90,500,15); label_enter = new Label("Enter your Name and Nick Name",Label.CENTER); add(label_enter); label_enter.setFont(new Font("Helvetica",Font.PLAIN,15));

label_enter.setForeground(Color.black); label_enter.reshape(insets().left + 0,insets().top + 150,500,20); label_register = new Label("To Register yourself",Label.CENTER); add(label_register); label_register.setFont(new Font("Helvetica",Font.PLAIN,15)); label_register.setForeground(Color.black); label_register.reshape(insets().left + 0,insets().top + 175,500,20); label_name= new Label("Name:"); add(label_name); label_name.setFont(new Font("Helvetica",Font.PLAIN,13)); label_name.setForeground(Color.black); label_name.reshape(insets().left + 55,insets().top +230,50,15); ts0=new TextField(45); ts0.setFont(new Font("Dialog",Font.PLAIN,15)); add(ts0); ts0.show(); ts0.reshape(insets().left + 55,insets().top + 250,200,25); ts0.setBackground(Color.black); ts0.setForeground(Color.cyan); label_nick= new Label("Nick Name:"); add(label_nick); label_nick.setFont(new Font("Helvetica",Font.PLAIN,13)); label_nick.setForeground(Color.black); label_nick.reshape(insets().left + 260,insets().top +230,90,15); ts1=new TextField(45); ts1.setFont(new Font("Dialog",Font.PLAIN,15)); add(ts1); ts1.show(); ts1.reshape(insets().left + 260,insets().top + 250,200,25); ts1.setBackground(Color.black); ts1.setForeground(Color.cyan); sbutton=new Button("Login"); add(sbutton); sbutton.show(); sbutton.setFont(new Font("Helvetica",Font.PLAIN,15)); sbutton.reshape(insets().left + 210,insets().top + 290,100,20); sbutton.setBackground(new Color(0,0,160)); sbutton.setForeground(Color.cyan); } public void destroy() { confdraw.cp.sendData("q" + ":"); confdraw.dispose(); conference.destroy(); } public boolean action(Event e,Object SelectedItem) { String buttonText = new String((String)e.arg) ;

int count=0;int count1=0; if(buttonText.equals("Login")) { Name=ts0.getText(); Nick=ts1.getText(); if((Name.length()!=0)&&(Nick.length()!=0)) { this.hide(); confdraw = new ConfDraw(conference,this); confdraw.show(); confdraw.toFront(); return true; } else return false; } else return false; } }

Back to starting of page

ConfDraw.java
import java.awt.*; import java.net.*; import java.util.*; import java.io.*; class ConfDraw extends Frame { public Conference client; public IntroCanvas introcanvas; Label label_UserName; Label label_mesg; Label label_ChannelName; Label label_UserList; Label label_ChannelList; Label label_mood; Label label_sendto; Label label_type; TextField ts; List ls; List lm; TextArea lChat; List lUser; List lCh; Button isend; Button ibye; Button ichangeroom; Button eprivate;

Button isquelch; Button iunsquelch; String UserName = null; String UserName1=null; String ChannelName = "General Chat"; boolean usersOn = false; boolean send = true; int Count = 1; String lastAction; String lastMessage; ConnectProcessing RefreshUsers PingChatServer ConfUsersDraw cp; refreshUsers; ping; confusersdraw;

URL url; DataInputStream inStream; String inputLine; public ConfDraw(Conference client,IntroCanvas i) { super("Welcome to the ChatRoom"); System.out.println("In Constructor of ConfDraw " ); this.client = client; this.introcanvas = i; setLayout(null); this.setBackground(new Color(164,108,153)); addNotify(); reshape(insets().left + 15,0,insets().left + insets().right + 598,insets().top + insets().bottom + 420); this.setResizable(false); label_ChannelName = new Label("Name of Channel : " + ChannelName); add(label_ChannelName); label_ChannelName.show(); label_ChannelName.setForeground(new Color(220,216,39)); label_ChannelName.reshape(insets().left + 120,insets().top + 7,300,15); lChat = new TextArea(50,0); add(lChat); lChat.show(); lChat.setEditable(false); lChat.setBackground((new Color(70,48,129))); lChat.setForeground(Color.white); lChat.reshape(insets().left + 21,insets().top + 30,413,250); label_ChannelList = new Label("Channel List " ,Label.CENTER); add(label_ChannelList); label_ChannelList.show(); label_ChannelList.setForeground(new Color(220,216,39)); label_ChannelList.reshape(insets().left + 476,insets().top + 7,98,15); lCh = new List(6,false);

add(lCh); lCh.show(); lCh.setBackground((new Color(70,48,129))); lCh.setForeground(Color.white); lCh.reshape(insets().left + 470,insets().top + 30,119,98); ichangeroom= new Button("Change Channel"); add(ichangeroom); ichangeroom.show(); ichangeroom.setFont(new Font("Dialog",Font.BOLD,10)); ichangeroom.reshape(insets().left+470,insets().top+133,120,18); ichangeroom.setBackground(new Color(125,38,117)); ichangeroom.setForeground(new Color(160,160,164)); Label label_type = new Label("Type Here.."); add(label_type); label_type.setFont(new Font("Dialog",Font.PLAIN,12)); label_type.setForeground(new Color(220,216,39)); label_type.reshape(insets().left + 21,insets().top + 285,80,15); ts = new TextField(45); ts.setFont(new Font("Dialog",Font.PLAIN,12)); add(ts); ts.show(); ts.setEditable(true); ts.reshape(insets().left + 21,insets().top + 305,413,20); ts.setBackground((new Color(70,48,129))); ts.setForeground(Color.white); ts.setText(" "); label_UserList = new Label("User List " ,Label.CENTER); add(label_UserList); label_UserList.show(); label_UserList.setForeground(new Color(220,216,39)); label_UserList.reshape(insets().left + 474,insets().top +170,115,15); lUser = new List(6, false); add(lUser); lUser.show(); lUser.setBackground((new Color(70,48,129))); lUser.setForeground(Color.white); lUser.reshape(insets().left + 470,insets().top + 192,119,133); Label label_mess = new Label("You are",Label.CENTER); add(label_mess); label_mess.setFont(new Font("Dialog",Font.BOLD+Font.ITALIC,15)); label_mess.setForeground(new Color(220,216,39)); label_mess.reshape(insets().left + 30,insets().top + 355,80,17); label_UserName = new Label("",Label.CENTER); add(label_UserName); label_UserName.setFont(new Font("Dialog",Font.BOLD+Font.ITALIC,15)); label_UserName.setForeground(new Color(220,216,39)); label_UserName.reshape(insets().left + 30,insets().top + 375,80,17);

label_mood = new Label("Choose Your Mood"); add(label_mood); label_mood.setFont(new Font("Dialog",Font.PLAIN,12)); label_mood.setForeground(new Color(220,216,39)); label_mood.reshape(insets().left + 125,insets().top + 335,150,15); ls = new List(3,false); add(ls); ls.setForeground(Color.white); ls.show(); ls.reshape(insets().left + 125,insets().top + 360,150,35); ls.setBackground((new Color(70,48,129))); ls.addItem("Select an action"); ls.addItem("shouts at"); ls.addItem("yells"); ls.addItem("grins"); ls.addItem("sneezes"); ls.select(0); /*Adding buttons */ isend= new Button("Send"); add(isend); isend.show(); isend.setFont(new Font("Dialog",Font.BOLD,10)); isend.reshape(insets().left+300,insets().top+350,120,18); isend.setBackground(new Color(125,38,117)); isend.setForeground(new Color(160,160,164)); isquelch= new Button("(Un)squelch User"); add(isquelch); isquelch.show(); isquelch.setFont(new Font("Dialog",Font.BOLD,10)); isquelch.reshape(insets().left+300,insets().top+375,120,18); isquelch.setBackground(new Color(125,38,117)); isquelch.setForeground(new Color(160,160,164)); eprivate= new Button("Private Chat"); add(eprivate); eprivate.show(); eprivate.setFont(new Font("Dialog",Font.BOLD,10)); eprivate.reshape(insets().left+470,insets().top+350,120,18); eprivate.setBackground(new Color(125,38,117)); eprivate.setForeground(new Color(160,160,164)); ibye= new Button("Exit Chat"); add(ibye); ibye.show(); ibye.setFont(new Font("Dialog",Font.BOLD,10)); ibye.reshape(insets().left+470,insets().top+375,120,18); ibye.setBackground(new Color(125,38,117)); ibye.setForeground(new Color(160,160,164)); UserName = introcanvas.Nick; label_UserName.setText(UserName); UserName1 = introcanvas.Name;

/** drawing the user(s) online frame **/ confusersdraw = new ConfUsersDraw(this); /** call the thread which will make the socket communications **/ cp = new ConnectProcessing(this,this.client); cp.setPriority(10); cp.readFile(); cp.start(); /* This is the first message that client sends to the Server */ cp.sendData("n" + ":" + UserName1 + ":" +UserName ); /** call another thread which will update the total users online **/ refreshUsers = new RefreshUsers(this); refreshUsers.setPriority(10); refreshUsers.start(); lCh.select(0); /* call the ping thread.*/ ping = new PingChatServer(this); ping.setPriority(10); ping.start(); } public boolean action(Event e,Object selectedItem) { String buttonText= new String((String) e.arg); if(buttonText.equals("Exit Chat")) { confusersdraw.dispose(); introcanvas.destroy(); return true; } /* for squelch Button */ if(buttonText.equals("(Un)squelch User")) { int a=0; if(lUser.getSelectedItem().equals(UserName1)) { lChat.appendText("-- You cannot (Un) Squelch yourself -"+"\n"); return true; } if( lUser.getSelectedItem().endsWith("**") == false) { for(a=0;a < confusersdraw.list_Chat1.countItems();a++)

{ if(confusersdraw.list_Chat1.getItem(a).equals(lUser.getSelectedItem())) { confusersdraw.list_Chat1.replaceItem((lUser.getSelectedItem() + "**"), a); } } lUser.replaceItem(( lUser.getSelectedItem() + "**"), lUser.getSelectedIndex()); lUser.select(0); } else { /*unsquelch the squelched user*/ int i = lUser.getSelectedItem().indexOf("**"); String tmp = new String(""); tmp = lUser.getSelectedItem().substring(0,i); for(a=0;a < confusersdraw.list_Chat1.countItems();a++) { if(confusersdraw.list_Chat1.getItem(a).equals(lUser.getSelectedItem())) { confusersdraw.list_Chat1.replaceItem(tmp,a); } } lUser.replaceItem(tmp, lUser.getSelectedIndex()); lUser.select(0); } return true; } /* End of handling for squelch button */ /* for Send Button */ if(buttonText.equals("Send")) { if(lUser.countItems()==1) { lChat.appendText("-- You are alone in this channel. --" + "\n"); ts.setText(""); } else if(lUser.countItems() > 1) { if(ts.getText() != null) { String xx = new String(ts.getText()); if(xx.length() > 55) { lChat.appendText("-- Do not type in long messages --" + "\n"); lChat.appendText("-- TRY AGAIN... --" + "\n"); ts.setText(""); return true; } /*To Check for same user name*/

if(lUser.getSelectedItem().equals(UserName1)) { lChat.appendText("-- You can not send message to yourself -"+"\n"); ts.setText(""); return true; } if(ls.getSelectedItem().compareTo("Select an action") == 0) { if(ts.getText().length() > 0) { lastMessage = ts.getText(); lastAction = "m" + ":" + ts.getText(); if(send) { cp.sendData(lastAction); } lChat.appendText("(" + UserName + ") " + ts.getText() + "\n"); ts.setText(""); } } else if(ls.getSelectedItem().compareTo("Select an action") != 0) { if(lUser.getSelectedItem() == null) { } else if(lUser.getSelectedItem() != null) { /** if user has been selected **/ for(int i = 0; i < ls.countItems(); i++) { if(ls.getItem(i).compareTo(ls.getSelectedItem()) == 0) { if(cp.slang_value[i] == 1) { if( lUser.getSelectedItem().endsWith("**") == true) { int j = lUser.getSelectedItem().indexOf("**"); String tmp1 = new String(""); tmp1 = lUser.getSelectedItem().substring(0,j); lastMessage = ls.getSelectedItem().toUpperCase() + " " + lUser.getSelectedItem(); lastAction = "m" + ":" + " " + ls.getSelectedItem().toUpperCase() + " " + tmp1; } else { lastMessage = ls.getSelectedItem().toUpperCase() + " " + lUser.getSelectedItem(); lastAction = "m" + ":" + " " + ls.getSelectedItem().toUpperCase() + " " + lUser.getSelectedItem(); } if(send)

{ cp.sendData(lastAction); } lChat.appendText("(" + UserName + ") " + ls.getSelectedItem().toUpperCase() + " " + lUser.getSelectedItem() + "\n"); ls.select(0); } else if(cp.slang_value[i] == 0) { lastMessage = ls.getSelectedItem().toUpperCase() + " "; lastAction = "m" + ":" + " " + ls.getSelectedItem().toUpperCase() + " "; if(send) { cp.sendData(lastAction); } lChat.appendText("(" + UserName + ") " + ls.getSelectedItem().toUpperCase() + " " + "\n"); ls.select(0); } } } /** end of user selected **/ } } } /** end of send data **/ } return true; } /* End of handling for Send Button */ if(buttonText.equals("Private Chat")) { confusersdraw.list_Chat1.clear(); int count=lUser.countItems(); if(count>1) { for(int i=0;i < count;i++) { confusersdraw.list_Chat1.addItem(new String(lUser.getItem(i))); } confusersdraw.list_Chat1.select(0); confusersdraw.show(); confusersdraw.toFront(); return true; } else { lChat.appendText("-- You are alone in this chat room --"+"\n"); return true; } }

if(buttonText.equals("Change Channel")) { /** handling the change of channels. **/ if(lCh.getSelectedItem() != null) { ChannelName = lCh.getSelectedItem(); label_ChannelName.setText("Name of Channel : "+ ChannelName); cp.sendData("c" + ":" + ChannelName); } return true; } if(e.id == Event.WINDOW_DESTROY) { this.dispose(); client.destroy(); return true; } /* for Enter Button */ if(e.key == 0) { if(lUser.countItems() == 1) { lChat.appendText("-- You are alone in this channel. --" + "\n"); ts.setText(""); } else if(lUser.countItems() > 1) { if(ts.getText() != null) { String xx = new String(ts.getText()); if(xx.length() > 55) { lChat.appendText("-- Do not type in long messages --" + "\n"); lChat.appendText("-- TRY AGAIN... --" + "\n"); ts.setText(""); return true; } /* to Check for same user name */ if(lUser.getSelectedItem().equals(UserName1) ) { lChat.appendText("-- You can not send message to yourself -"+"\n"); ts.setText(""); return true; } if(ls.getSelectedItem().compareTo("Select an action") == 0) { if(ts.getText().length() > 0) { lastMessage = ts.getText(); lastAction = "m" + ":" + ts.getText(); if(send) {

cp.sendData(lastAction); } lChat.appendText("(" + UserName + ") " + ts.getText() + "\n"); ts.setText(""); } } else if(ls.getSelectedItem().compareTo("Select an action") != 0) { if(lUser.getSelectedItem() == null) { } else if(lUser.getSelectedItem() != null) { /** if user has been selected **/ for(int i = 0; i < ls.countItems(); i++) { if(ls.getItem(i).compareTo(ls.getSelectedItem()) == 0) { if(cp.slang_value[i] == 1) { if( lUser.getSelectedItem().endsWith("**") == true) { int j = lUser.getSelectedItem().indexOf("**"); String tmp1 = new String(""); tmp1 = lUser.getSelectedItem().substring(0,j); lastMessage = ls.getSelectedItem().toUpperCase() + " " + lUser.getSelectedItem(); lastAction = "m" + ":" + " " + ls.getSelectedItem().toUpperCase() + " " + tmp1; } else { lastMessage = ls.getSelectedItem().toUpperCase() + " " + lUser.getSelectedItem(); lastAction = "m" + ":" + " " + ls.getSelectedItem().toUpperCase() + " " + lUser.getSelectedItem(); } if(send) { cp.sendData(lastAction); } lChat.appendText("(" + UserName + ") " + ls.getSelectedItem().toUpperCase() + " " + lUser.getSelectedItem() + "\n"); ls.select(0); } else if(cp.slang_value[i] == 0) { lastMessage = ls.getSelectedItem().toUpperCase() + " "; lastAction = "m" + ":" + " " + ls.getSelectedItem().toUpperCase() + " "; if(send)

{ cp.sendData(lastAction); } lChat.appendText("(" + UserName + ") " + ls.getSelectedItem().toUpperCase() + " " + "\n"); ls.select(0); } } } /** end of user selected **/ } } } /** end of send data **/ } return true; } /* End of handling for Enter Button */ return false; } }

Back to starting of page

ConnectProcesssing.java
import java.awt.*; import java.net.*; import java.util.*; import java.io.*; class ConnectProcessing extends Thread { int retryCount = 0; ConfDraw client; Conference appletRef; String host = new String(" "); final int port = 2000; Socket socket; int i = 0; DataInputStream dataURL; DataInputStream dataIn; PrintStream dataOut; String inputLine; int[] slang_value = new int[20]; public ConnectProcessing(ConfDraw client,Conference appletRef) { this.client = client; this.appletRef = appletRef;

this.host = appletRef.host; try { socket = new Socket(host,2000); dataIn = new DataInputStream(socket.getInputStream()); dataOut = new PrintStream(socket.getOutputStream()); } catch (UnknownHostException e) { System.out.println("ERROR "+e+"\n"); } catch(IOException e) { System.out.println("ERROR "+e+"\n"); } } public synchronized void run() { while(chat()) { } } public String readData() { StringBuffer buffer = new StringBuffer(); char tmp = ' '; try { while(true) { tmp = dataIn.readChar(); if(tmp == '\n') { return buffer.toString(); } else { buffer.append(tmp); } } } catch (IOException e) { } return buffer.toString(); } public boolean chat() { String recievedMessage = null; StringTokenizer tokens; StringTokenizer userList; try { recievedMessage = readData();

System.out.println("RECD=" + recievedMessage); } catch(Exception e) { } if(recievedMessage != null) { try { tokens = new StringTokenizer(recievedMessage,":"); String firstToken = tokens.nextToken(); if(firstToken.equals("$")) { String from=new String(" "); String mesg=null; from=tokens.nextToken(); mesg=tokens.nextToken(); String temp=null; int c = 0,d=0; for(d= 0;d < client.lUser.countItems();d++) { if(client.lUser.getItem(d).endsWith("**")) { c=client.lUser.getItem(d).indexOf("**"); temp=client.lUser.getItem(d).substring(0,c); if(temp.equals(from)) return true; } } client.confusersdraw.show(); client.confusersdraw.toFront(); client.confusersdraw.ts1.appendText("("+from+")"+":"+mesg+"\n"); client.confusersdraw.list_Chat1.clear(); int count=client.lUser.countItems(); if(count > 1) { for(int i=0;i < count;i++) { client.confusersdraw.list_Chat1.addItem(new String(client.lUser.getItem(i))); } } client.confusersdraw.list_Chat1.select(0); return true; } else if(firstToken.equals("c")) { while(tokens.hasMoreTokens()) { String useradd = tokens.nextToken(); client.lChat.appendText("-- " + useradd + " has entered channel " + client.ChannelName + " --" + "\n"); client.lUser.addItem(useradd); }

client.label_UserList.setText(" " + client.lUser.countItems() + " user(s) in channel"); return true; } else if(firstToken.equals("L")) { while(tokens.hasMoreTokens()) { client.lCh.addItem(tokens.nextToken()); } return true; } else if(firstToken.equals("d")) { while(tokens.hasMoreTokens()) { client.lChat.appendText("-- " + tokens.nextToken() + " --" + "\n"); } return true; } else if(firstToken.equals("Q")) { while(tokens.hasMoreTokens()) { String userdel = tokens.nextToken(); client.lChat.appendText("-- " + userdel + " has left this channel. --" + "\n"); for(int i = 0; i < client.lUser.countItems(); i++) { if(client.lUser.getItem(i).compareTo(userdel) == 0) { client.lUser.delItem(i); } } } client.label_UserList.setText(" " + client.lUser.countItems() + " user(s) in channel"); return true; } else if(firstToken.equals("C")) { String valid = tokens.nextToken(); if(valid.compareTo("Y") == 0) { client.lChat.appendText("--The Channel change to " + client.ChannelName + " was successful--\n"); } else if(valid.compareTo("N") == 0) { client.lChat.appendText("--The Channel change to " + client.ChannelName + " was not successful--\n");

} return true; } else if(firstToken.equals("n")) { String name = new String(tokens.nextToken()); String valid = new String(tokens.nextToken()); if(valid.equals("Y") == true) { client.lChat.appendText("-- " + name + " has just entered channel " + client.ChannelName + " --" + "\n"); return true; } if(valid.equals("N") == true) { client.lChat.appendText("-- Same login name exists --" + "\n"); client.lChat.appendText("-- Please Exit Chat and TRY AGAIN --" + "\n"); return true; } if(valid.equals("F") == true) { appletRef.destroy(); return true; } return true; } else if(firstToken.equals("m")) { String message = null; String userName = null; while(tokens.hasMoreTokens()) { userName = new String(tokens.nextToken()); message = new String(tokens.nextToken()); } System.out.println(userName+" :" + message); String tmp = null; int k= 0; for(int j= 0;j < client.lUser.countItems();j++) { if(client.lUser.getItem(j).endsWith("**")) { k=client.lUser.getItem(j).indexOf("**"); tmp=client.lUser.getItem(j).substring(0,k); if(tmp.equals(userName)) return true; } } client.lChat.appendText("--" + "(" + userName + ")" + " " + message + "--" + "\n"); return true; } /** end of handling **/

/** handling routine for a w: **/ else if(firstToken.equals("w")) { for(int i = 0; i < 6; i++) { client.lUser.clear(); } while(tokens.hasMoreTokens()) { String member = new String(tokens.nextToken()); client.lUser.addItem(member); } client.label_UserList.setText(" " + client.lUser.countItems() + " user(s) in channel"); client.lUser.select(0); return true; } /** end of handling **/ /** handling routine for a q:**/ else if(firstToken.equals("q")) { while(tokens.hasMoreTokens()) { String User = new String(tokens.nextToken()); client.lChat.appendText("-- " + User + " has quit the chat. --" + "\n"); client.confusersdraw.ts1.appendText("-- " + User + " has quit the chat. --" + "\n"); for(int i = 0; i < client.lUser.countItems(); i++) { try { if(client.lUser.getItem(i).equals(User)) { client.lUser.delItem(i); } else if(client.lUser.getItem(i).endsWith("**")) { int z = client.lUser.getItem(i).indexOf("**"); String tp = new String(""); tp = client.lUser.getItem(i).substring(0,z); if(tp.equals(User)) { client.lUser.delItem(i); } } } catch(ArrayIndexOutOfBoundsException e) { System.out.println("Handle the error :"+e); } } for(i = 0; i < client.confusersdraw.list_Chat1.countItems(); i++) {

try { if(client.confusersdraw.list_Chat1.getItem(i).endsWith("**")) { int z = client.confusersdraw.list_Chat1.getItem(i).indexOf("**"); String tp = new String(""); tp = client.confusersdraw.list_Chat1.getItem(i).substring(0,z); if(tp.equals(User)) { client.confusersdraw.list_Chat1.delItem(i); } } else if(client.confusersdraw.list_Chat1.getItem(i).equals(User)) { client.confusersdraw.list_Chat1.delItem(i); } } catch(ArrayIndexOutOfBoundsException e) { System.out.println("Handle the error :"+e); } } client.label_UserList.setText(" " + client.lUser.countItems() + " user(s) in channel"); return true; } return true; }/* end of else*/ } /* end of try */ catch(NoSuchElementException ne) { } /** end of routine **/ } else { client.lChat.appendText("Connect request timed out...\n"); client.lChat.appendText("Retrying after 10 seconds...\n"); try { this.sleep(10000); } catch(InterruptedException e) { } retryCount++; if(retryCount == 3) { client.lChat.appendText("Connection to Chat server site failed...\n"); return true; } return false;

} return false; } public void readFile() { slang_value[0] = 1; slang_value[1] = 1; slang_value[2] = 0; slang_value[3] = 0; } public void sendData(String message) { try { dataOut.println(message); dataOut.flush(); } catch(Exception e) { System.out.println("Dataout Exception :" + e.toString()); } } }

Back to starting of page

ConfUsersDraw.java
import java.awt.*; import java.util.*; class ConfUsersDraw extends Frame { ConfDraw client; /** reference to the main frame class **/ Label label_mes; Label label_Username; Label label_Ulist; Label label_tmesg; Label label_pmesg; TextField ts; TextArea ts1; List list_Chat1; Button iprivate; Button icancel; StringTokenizer tokens; public ConfUsersDraw(ConfDraw client)

{ super("Private Chatroom."); this.client = client; /** reference to the main frame class **/ setLayout(null); this.setBackground(new Color(72,96,80)); addNotify(); reshape(insets().left +15,0, insets().left + insets().right + 450, insets().top + insets().bottom + 420); this.setResizable(false); Label label_pmesg = new Label("Private Message:"); add(label_pmesg); label_pmesg.setFont(new Font("Dialog",Font.BOLD,12)); label_pmesg.setForeground(Color.white); label_pmesg.reshape(insets().left + 30,insets().top + 30,100,15); ts1 = new TextArea(15,0); add(ts1); ts1.show(); ts1.setEditable(false); ts1.setBackground(new Color(144,128,112)); ts1.setForeground(Color.black); ts1.reshape(insets().left + 21,insets().top + 50,310,250); Label label_Ulist = new Label("User(s) List"); add(label_Ulist); label_Ulist.setFont(new Font("Dialog",Font.BOLD,12)); label_Ulist.setForeground(Color.white); label_Ulist.reshape(insets().left + 342,insets().top + 30,80,15); list_Chat1 = new List(12,false); add(list_Chat1); list_Chat1.show(); list_Chat1.setBackground(new Color(144,128,112)); list_Chat1.setForeground(Color.black); list_Chat1.reshape(insets().left + 342,insets().top + 50,80,250); Label label_tmesg = new Label("Type Here.."); add(label_tmesg); label_tmesg.setFont(new Font("Dialog",Font.BOLD,12)); label_tmesg.setForeground(Color.white); label_tmesg.reshape(insets().left + 21,insets().top + 310,100,15); ts = new TextField(""); add(ts); ts.show(); ts.setBackground(new Color(144,128,112)); ts.setForeground(Color.black); ts.reshape(insets().left + 21,insets().top + 335,400,20); Label label_mes = new Label("You are",Label.CENTER); add(label_mes); label_mes.setFont(new Font("Dialog",Font.BOLD+Font.ITALIC,15)); label_mes.setForeground(Color.white); label_mes.reshape(insets().left + 21,insets().top + 365,70,18);

label_Username = new Label("",Label.CENTER); add(label_Username); label_Username.setFont(new Font("Dialog",Font.BOLD+Font.ITALIC,15)); label_Username.setForeground(Color.white); label_Username.reshape(insets().left + 21,insets().top + 385,70,18); iprivate= new Button("Send"); add(iprivate); iprivate.show(); iprivate.setFont(new Font("Dialog",Font.BOLD,10)); iprivate.reshape(insets().left+120,insets().top+370,120,18); iprivate.setBackground(new Color(144,128,112)); iprivate.setForeground(Color.black); icancel= new Button("Cancel"); add(icancel); icancel.show(); icancel.setFont(new Font("Dialog",Font.BOLD,10)); icancel.reshape(insets().left+260,insets().top+370,120,18); icancel.setBackground(new Color(144,128,112)); icancel.setForeground(Color.black); client.UserName = client.introcanvas.Nick; client.UserName1 = client.introcanvas.Name; label_Username.setText(client.UserName); } public boolean action(Event e,Object obj) { String buttonText= new String((String) e.arg); if(buttonText.equals("Send")) { if(list_Chat1.countItems()==1) { ts1.appendText("-- You are alone in this chat room --" + "\n"); list_Chat1.select(0); ts.setText(""); return true; } else if(list_Chat1.countItems()>1) { if(ts.getText()!=null) { String xx = new String(ts.getText()); if(xx.length() > 55) { ts1.appendText("-- Do not type in long messages --" + "\n"); ts1.appendText("-- TRY AGAIN... --" + "\n"); ts.setText(""); return true; } if(list_Chat1.getSelectedItem().equals(client.UserName1)) {

ts1.appendText("-- You can not send message to yourself -"+"\n"); ts.setText(""); return true; } int z=0; String sendto= null; if(list_Chat1.getSelectedItem().endsWith("**")) { z=list_Chat1.getSelectedItem().indexOf("**"); sendto=list_Chat1.getSelectedItem().substring(0,z); } else { sendto=list_Chat1.getSelectedItem(); } if(sendto!=null) { String message=new String(ts.getText()); String lastmesg="$"+":"+sendto+":"+message; client.cp.sendData(lastmesg); ts1.appendText(" ("+client.UserName1+") "+message+" to ("+sendto+") "+"\n"); ts.setText(" "); } } return true; } } if(buttonText.equals("Cancel")) { ts1.setText(""); this.dispose(); client.show(); return true; } if(e.id == Event.WINDOW_DESTROY) { this.dispose(); return true; } if(e.key == 0) { if(list_Chat1.countItems()==1) { ts1.appendText("-- You are alone in this chat room --" + "\n"); list_Chat1.select(0); ts.setText(""); return true; } else if(list_Chat1.countItems()>1) { if(ts.getText()!=null) { String xx = new String(ts.getText()); if(xx.length() > 55)

{ ts1.appendText("-- Do not type in long messages --" + "\n"); ts1.appendText("-- TRY AGAIN... --" + "\n"); ts.setText(""); return true; } if(list_Chat1.getSelectedItem().equals(client.UserName1)) { ts1.appendText("-- You can not send message to yourself -"+"\n"); ts.setText(""); return true; } int z=0; String sendto= null; if(list_Chat1.getSelectedItem().endsWith("**")) { z=list_Chat1.getSelectedItem().indexOf("**"); sendto=list_Chat1.getSelectedItem().substring(0,z); } else { sendto=list_Chat1.getSelectedItem(); } if(sendto!=null) { String message=new String(ts.getText()); String lastmesg="$"+":"+sendto+":"+message; client.cp.sendData(lastmesg); ts1.appendText(" ("+client.UserName1+") "+message+" to ("+sendto+") "+"\n"); ts.setText(" "); } } return true; } } return false; } }

Back to starting of page

RefreshUsers.java
class RefreshUsers extends Thread { ConfDraw client;

public RefreshUsers(ConfDraw client) { this.client = client; } public void run() { while(true) { /** sending a U: to get the users online after every 15 seconds **/ client.cp.sendData("U" + ":"); try { this.sleep(15000); } catch(InterruptedException e) { } } } }

Back to starting of page

PingChatServer.java
class PingChatServer extends Thread { ConfDraw client; public PingChatServer(ConfDraw client) { this.client = client; } public void run() { while(true) { client.cp.sendData("P"); try { this.sleep(15000); } catch(InterruptedException e) { } } } }

Back to starting of page

Back to Main page

Das könnte Ihnen auch gefallen