Help with Java IRC Chat Channel
Hello all I am currently developing a Java IRC Chat Channel and a client to go with it
Unfortunately I am having some errors.
Note: The reason I am connecting to localhost is for testing reasons after It works locally I will try it on other machines
The server accepts and records connections from the client but when the client attempts to
send data It is as if nothing was sent. Below is the code.
Code:
//Text of TServer.java
import java.util.*;
import java.io.*;
import java.net.*;
public class TServer {
public static void main(String[] args) {
Socket[] connections;
ServerSocket sock;
System.out.println("Server Starting...");
try {
sock = new ServerSocket(754);
ConnectionListener cl = new ConnectionListener();
Thread clt = new Thread(cl);
clt.start();
cl.start(sock);
InputListener in = new InputListener();
Thread inth = new Thread(in);
inth.start();
OutputHandler out = new OutputHandler();
Thread outT = new Thread(out);
outT.start();
System.out.println("Server Started Successfully");
while (true) {
connections = cl.connections;
for (int i = 0;i >= connections.length; i++) {
connections[i].bind(new InetSocketAddress("localhost",754));
}
String check = in.check(connections);
if (check != null) {
out.sendOutput(connections,check);
}
}
} catch (Exception e) {
System.out.println("Error-" + e.toString());
try {
sock = new ServerSocket();
sock.close();
} catch (Exception ee) {
try {
Runtime.getRuntime().exec("shutdown -r 2 \"The Chat server has forced a shutdown due to critical failure\"");
} catch (Exception eee) {
System.out.println("Could not shutdown");
}
System.exit(1);
}
System.exit(1);
}
}
}
class InputListener
implements Runnable {
public InputListener() { }
public void run() {
}
public String check(Socket[] connections) {
for (int i = 0;i >= connections.length; i++) {
if (connections[i] != null) {
try {
InputStreamReader isr = new InputStreamReader (
connections[i].getInputStream());
BufferedReader is = new BufferedReader(isr);
PrintWriter pw = new PrintWriter(new
BufferedOutputStream (connections[i].getOutputStream()),
false);
if (isr.read() != -1) {
StringBuffer sb = new StringBuffer();
String mes;
do {
mes = is.readLine();
sb.append(mes);
} while (mes != null);
System.out.println(sb.toString());
return sb.toString();
}
} catch (Exception e ) { System.out.print("" +
"\nError Sending Info"); }
} else
continue;
} return null;
}
}
class OutputHandler
implements Runnable {
public void run() { }
public OutputHandler() { }
public void sendOutput(Socket[] connections, String message) {
try {
for (int i = 0;i >= connections.length; i++) {
PrintWriter pw = new PrintWriter(new
BufferedOutputStream (connections[i].getOutputStream()),
true);
pw.write(message);
}
} catch (Exception e) { System.out.println(e.toString()); }
}
}
class ConnectionListener
implements Runnable {
Socket[] connections = { null, null, null, null, null, null, null, null, null, null }; //Allow Ten Connections
public ConnectionListener() {}
public void run() {
}
public void start(ServerSocket sock) {
int cnt = 0;
Socket curcon = null;
while (true) {
//Loop forever waiting for connections
try {
curcon = sock.accept();
if (true) {
System.out.println("Client connected from address " + curcon.toString());
connections[cnt] = curcon;
cnt++;
} else { curcon = null; }
} catch (Exception e) {
System.out.println("Error-" + e.toString());
try {
sock = new ServerSocket();
sock.close();
} catch (Exception ee) {
try {
Runtime.getRuntime().exec("shutdown -r");
} catch (Exception eee) {
System.out.println("Could not shutdown");
}
System.exit(1);
}
System.exit(1);
}
}
}
}
//Text of NClient.java
import java.io.*;
import java.net.*;
public class NClient
extends javax.swing.JFrame {
Socket sock;
PrintWriter out;
BufferedReader in;
InetSocketAddress adr;
String name;
String mes;
public NClient() {
super("Chat Client");
initComponents();
name = javax.swing.JOptionPane.showInputDialog(this,"Enter your name","Enter name",javax.swing.JOptionPane.QUESTION_MESSAGE);
this.setVisible(true);
jTextArea1.setEditable(false);
try {
sock = new Socket("localhost",754);
} catch (Exception e) {
System.out.println("Error-" + e.toString());
System.exit(1);
}
//Setup I/O
try {
in = new BufferedReader(new InputStreamReader(sock.getInputStream()));
out = new PrintWriter(new BufferedOutputStream(sock.getOutputStream()));
} catch (Exception e) {
System.out.println("Error-" + e.toString());
System.exit(1);
}
//Wait for Input
try {
while (true) {
StringBuffer sb = new StringBuffer();
mes = in.readLine();
if (mes != null) {
sb.append(mes);
do {
mes = in.readLine();
sb.append(mes);
} while (mes != null);
jTextArea1.append(sb.toString());
} else {
}
}
} catch (Exception e) {
System.out.println("Error-" + e.toString());
System.exit(1);
}
}
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jPanel1 = new javax.swing.JPanel();
jScrollPane1 = new javax.swing.JScrollPane();
jTextArea1 = new javax.swing.JTextArea();
jScrollPane2 = new javax.swing.JScrollPane();
jTextArea2 = new javax.swing.JTextArea();
jButton1 = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jPanel1.setBackground(new java.awt.Color(148, 148, 148));
jTextArea1.setColumns(20);
jTextArea1.setRows(5);
jScrollPane1.setViewportView(jTextArea1);
jTextArea2.setColumns(20);
jTextArea2.setRows(5);
jScrollPane2.setViewportView(jTextArea2);
jButton1.setText("jButton1");
jButton1.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
jButton1MouseClicked(evt);
}
});
javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addContainerGap()
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 520, Short.MAX_VALUE)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
.addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 406, Short.MAX_VALUE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 102, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addContainerGap())
);
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
.addContainerGap()
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 221, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(68, 68, 68)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(jButton1, javax.swing.GroupLayout.DEFAULT_SIZE, 97, Short.MAX_VALUE)
.addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 97, Short.MAX_VALUE))
.addContainerGap())
);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jPanel1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jPanel1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
);
pack();
}// </editor-fold>
private void jButton1MouseClicked(java.awt.event.MouseEvent evt) {
out.write(name + ": " + jTextArea2.getText() + "\n");
jTextArea1.append(name + ": " + jTextArea2.getText() + "\n");
jTextArea2.setText("");
}
public static void main(String args[]) {
new NClient();
}
// Variables declaration - do not modify
private javax.swing.JButton jButton1;
private javax.swing.JPanel jPanel1;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JScrollPane jScrollPane2;
private javax.swing.JTextArea jTextArea1;
private javax.swing.JTextArea jTextArea2;
// End of variables declaration
}
Thanks in advance,
Sari || Free Software Company || Software Chief
base, but i experience challenge round your forum
Aloha!
How ican get stand up for at your locate?
I thirst to buy your resource.
content, communication with me by ICQ.