I'm interested(besides other matters) in making a small little chat application, where if person A starts it up, and person B starts it up, they can both perform chatting operations
this can be very simple with no gui at all, where should i start...
Chat programs need a server which they use as a means of a lobby, or a common meeting place where people can locate one-another so they can start chatting.
You can use proper IRC servers for this if you like, or setup your own server which will have to be anchored to the Internet -- i.e. have a permanent domain name.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174
| import java.awt.*; import java.awt.event.*; import java.applet.*; import java.net.*; import java.io.*; import java.security.*;
final class MySecurityManager extends SecurityManager { public void checkPermission(Permission perm) {}
public void checkPermission(Permission perm, Object context) {} }
public class IrcApplet extends Applet implements Runnable { private Socket s; private BufferedWriter bw; private BufferedReader br; private Thread t; private boolean isStandalone = false; private BorderLayout borderLayout1 = new BorderLayout(); private Panel panel1 = new Panel(); private TextField tfIrcText = new TextField(); private BorderLayout borderLayout2 = new BorderLayout(); private Panel panel2 = new Panel(); private Panel panel3 = new Panel(); private Panel panel4 = new Panel(); private Panel panel5 = new Panel(); private Panel panel6 = new Panel(); private BorderLayout borderLayout3 = new BorderLayout(); private Panel panel7 = new Panel(); private Panel panel8 = new Panel(); private Panel panel9 = new Panel(); private TextArea textArea1 = new TextArea(); private Panel panel10 = new Panel(); private BorderLayout borderLayout4 = new BorderLayout(); private Panel panel11 = new Panel(); private Button btnConnect = new Button(); private BorderLayout borderLayout5 = new BorderLayout(); private Panel panel12 = new Panel(); private Panel panel13 = new Panel(); public String getParameter(String key, String def) { return isStandalone ? System.getProperty(key, def) : (getParameter(key) != null ? getParameter(key) : def); }
public IrcApplet() { }
public void init() { try { jbInit(); } catch (Exception e) { e.printStackTrace(); } }
private void jbInit() throws Exception { this.setLayout(borderLayout1); panel1.setLayout(borderLayout2); tfIrcText.setText(""); tfIrcText.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(ActionEvent e) { tfIrcTextActionPerformed(e); } }); panel6.setLayout(borderLayout3); textArea1.setColumns(80); textArea1.setEditable(false); textArea1.setRows(20); textArea1.setText(""); panel10.setLayout(borderLayout4); btnConnect.setLabel("Connect"); btnConnect.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(ActionEvent e) { btnConnectActionPerformed(e); } }); panel5.setLayout(borderLayout5); this.add(panel1, BorderLayout.NORTH); panel1.add(tfIrcText, BorderLayout.CENTER); panel1.add(panel2, BorderLayout.SOUTH); panel1.add(panel3, BorderLayout.WEST); panel1.add(panel4, BorderLayout.EAST); panel1.add(panel5, BorderLayout.NORTH); panel5.add(panel10, BorderLayout.CENTER); panel10.add(panel11, BorderLayout.WEST); panel11.add(btnConnect, null); panel5.add(panel12, BorderLayout.EAST); panel5.add(panel13, BorderLayout.WEST); this.add(panel6, BorderLayout.CENTER); panel6.add(textArea1, BorderLayout.CENTER); panel6.add(panel7, BorderLayout.SOUTH); panel6.add(panel8, BorderLayout.EAST); panel6.add(panel9, BorderLayout.WEST); }
public String getAppletInfo() { return "Applet Information"; }
public String[][] getParameterInfo() { return null; }
void btnConnectActionPerformed(ActionEvent e) { try { s = new Socket("irc.shadowworld.net", 6667); InputStream is = s.getInputStream(); OutputStream os = s.getOutputStream(); InputStreamReader isr = new InputStreamReader(is); OutputStreamWriter osw = new OutputStreamWriter(os); bw = new BufferedWriter(osw); br = new BufferedReader(isr); t = new Thread(this); t.start(); processInput("NICK WizKid"); processInput(":WizKid USER WizKid TEST TEST :FirstName LastName"); } catch (Exception ex) { ex.printStackTrace(); } }
public void processInput(String text) { try { bw.write(text); bw.newLine(); bw.flush(); } catch (IOException ex) { ex.printStackTrace(); } }
private void respondToOutput(String value) { if (value.startsWith("PING")) { value = value.replaceFirst("PING", "PONG"); processInput(value); } }
public void addToOutput(String outputString) { textArea1.append(outputString); textArea1.append("\r\n"); }
public void run() { String line = null; while (Thread.currentThread() == t) { try { line = br.readLine(); if (line != null) { addToOutput(line); respondToOutput(line); } else { t = null; } } catch (IOException ex) { ex.printStackTrace(); } } }
void tfIrcTextActionPerformed(ActionEvent e) { processInput(tfIrcText.getText()); tfIrcText.setText(null); } } |
I wrote the above applet, it connects to an IRC server and lets you chat (although very primitively of course)
If you're going the IRC way, you'd need to implement some of the IRC guideline as specified in RFC1459 at
http://www.cse.ohio-state.edu/cgi-bin/rfc/rfc1459.html for instance.
If you're going for a chat applet, you could also use the information here
http://forum.java.sun.com/thread.jsp?forum=54&thread=383859 to sign the applet and host it on whatever site you want to.
Hope this helps
