D
Dan
Hello all,
I am having some serious issues trying to control multiple sockets.
Currently, I have it working so that the server piece can broadcast to
multiple clients, I just can't receive any messages when there are
more than one clients connected. I am probably explaining this
poorly, so I will let the code speak for itself:
package myxserver;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.beans.*;
import java.io.*;
import java.awt.*;
import java.net.*;
public class TheXServer extends JFrame implements Runnable {
private final static int MAX_CONNECTIONS = 100;
private static boolean REMOVING = false;
protected WindowManager m_wm;
protected JTextArea m_consoletext, m_consolechat;
protected JTextField m_chatText;
protected JLabel m_status;
protected static int m_count;
protected static boolean m_connected;
protected static DataInputStream[] m_input; // input
array
protected static DataOutputStream[] m_output; // output
array
protected static Socket[] m_clients; // Client
array
protected static ServerSocket m_server; // this is
what waits for messages
protected static Thread[] m_listenthread; // an
array of listeners
protected static ConThread m_conthread; //
strangely, this needs to be here
public TheXServer() {
m_clients = new Socket[MAX_CONNECTIONS];
m_listenthread = new Thread[MAX_CONNECTIONS];
m_output = new DataOutputStream[MAX_CONNECTIONS];
m_input = new DataInputStream[MAX_CONNECTIONS];
setTitle("The X Server");
m_count = 0;
m_status = new JLabel("No Client");
JScrollPane scroller = new JScrollPane();
m_wm = new WindowManager(this);
getContentPane().setLayout(new BorderLayout());
getContentPane().add("Center", setupConsole());
getContentPane().add("South", m_status);
Dimension dim = getToolkit().getScreenSize();
setSize(520,350);
setLocation(dim.width/2-getWidth()/3, dim.height/2-getHeight()/2);
WindowListener l = new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
};
addWindowListener(l);
setVisible(true);
}
public JInternalFrame setupConsole() {
JInternalFrame console = new JInternalFrame ("The X Server",
false, false, false, false);
console.setBounds(2, 2, 500, 300);
console.setVisible(true);
JPanel chatPanel = new JPanel();
JLabel chatLabel = new JLabel(" CHAT ");
chatPanel.setLayout(new BorderLayout());
m_consoletext = new JTextArea();
m_consoletext.setPreferredSize(new Dimension(500, 50));
m_consoletext.setLineWrap(true);
m_consoletext.setText("Server Started.\nWaiting for client ...");
m_consoletext.setEditable(false);
m_consolechat = new JTextArea();
m_consolechat.setLineWrap(true);
m_consolechat.setEditable(false);
m_chatText = new JTextField();
m_chatText.addActionListener(new ChatAdapter());
JButton chatSend = new JButton("Send");
chatSend.addActionListener(new ChatAdapter());
JPanel sendPanel = new JPanel();
sendPanel.setLayout(new BorderLayout());
sendPanel.add("Center", m_chatText);
sendPanel.add("West", chatSend);
JScrollPane cscroller1 = new JScrollPane(m_consoletext);
JScrollPane cscroller2 = new JScrollPane(m_consolechat);
chatPanel.add("North", chatLabel);
chatPanel.add("Center", cscroller2);
chatPanel.add("South", sendPanel);
JSplitPane splitter = new JSplitPane(JSplitPane.VERTICAL_SPLIT,
true, cscroller1, chatPanel);
console.getContentPane().add(splitter);
try {
m_server = new ServerSocket(5000,500);
}
catch (Exception e) {
m_consoletext.append("\n"+e);
}
m_conthread = new ConThread();
return console;
}
public void run() {
while (m_connected) {
int i = 0;
try {
for (; i < m_count; i++)
processMessage(m_input.readUTF());
}
catch (Exception e) {
removeSocketConnections(i);
keepRunning(i);
}
}
}
public void keepRunning(int i) {
try {
for (; i < m_count; i++)
processMessage(m_input.readUTF());
}
catch (Exception e) {
removeSocketConnections(i);
keepRunning(i);
}
}
private static void removeSocketConnections(int x) {
REMOVING = true;
for (int i = x; i < m_count; i++) {
if (i + 1 < MAX_CONNECTIONS) {
m_input = m_input[i+1];
m_output = m_output[i+1];
m_clients = m_clients[i+1];
m_listenthread = m_listenthread[i+1];
}
else {
m_input = null;
m_output = null;
m_clients = null;
m_listenthread = null;
}
}
m_count--;
if (m_count <= 0) m_connected = false;
REMOVING = false;
}
public void processMessage(String s) {
if (s.startsWith("cc")) {
m_consolechat.append("CLIENT: " + s.substring(2) + "\n");
m_consolechat.setCaretPosition(m_consolechat.getText().length());
}
}
public static void main(String[] args) {
new TheXServer();
}
class ChatAdapter implements ActionListener {
public void actionPerformed (ActionEvent e) {
m_wm.sendMessage("cc" + m_chatText.getText());
m_consolechat.append("SERVER: " + m_chatText.getText() + "\n");
m_chatText.setText("");
}
}
class ConThread extends Thread {
ConThread() { start(); }
public void run() {
while (true) {
if (!REMOVING) {
try {
if (m_count+1 < MAX_CONNECTIONS) {
m_clients[m_count] = m_server.accept();
m_connected = true;
m_input[m_count] = new
DataInputStream(m_clients[m_count].getInputStream());
m_output[m_count] = new
DataOutputStream(m_clients[m_count].getOutputStream());
m_consoletext.append("\nStreams established ... ");
m_listenthread[m_count] = new Thread(TheXServer.this);
m_listenthread[m_count].start();
m_status.setText("Client Connected from: " +
m_clients[m_count].getInetAddress());
m_consoletext.append("\nClient Connected from: " +
m_clients[m_count].getInetAddress());
m_count++;
}
}
catch (Exception e) {
m_consoletext.append("\n" + e);
}
}
}
}
}
public class WindowManager extends DefaultDesktopManager {
protected JFrame m_desktop;
public WindowManager(JFrame desktop) { m_desktop = desktop; }
public WindowWatcher getWindowWatcher() { return null; }
public void sendMessage(String s) {
int i = 0;
try {
if (m_output != null) {
for (; i < m_count; i++)
m_output.writeUTF(s);
}
}
catch (Exception e) {
removeSocketConnections(i);
sendMessage(s, i);
}
}
public void sendMessage(String s, int i) {
try {
if (m_output != null) {
for (; i < m_count; i++)
m_output.writeUTF(s);
}
}
catch (Exception e) {
removeSocketConnections(i);
sendMessage(s, i);
}
}
public String getStringIndex(Component f) {
String s = f.toString();
while (s.length() < 3)
s = ("0").concat(s);
return s;
}
public String getString(int num) {
String s;
if (num < 0)
s = "" + (-num);
else
s = "" + num;
while (s.length() < 6)
s = ("0").concat(s);
if (num < 0)
s = "-" + s.substring(1, 6);
return s;
}
}
}
Now, this is my first time playing with sockets, so I am sure that
there are MANY problems with my code. This code is hacked together by
looking over several socket examples and pulling out all the
unnecessary code. My core problem that I need resolved is the fact
that when more than one client piece is connected to it, the server
never spits out a message. It seems to me that it is stuck in the Run
loop, but I can't really tell. By the way, the client piece looks
almost exactly like this, with some very minor differences ... like a
way to connect to the server and it doesn't have arrays of the input,
output and socket structures. If anyone has had much experience with
sockets and can point me to some good examples, I would be more than
happy to encorporate some more foreign code to try to get the jumble
to work. Thanks in advance.
Dan
I am having some serious issues trying to control multiple sockets.
Currently, I have it working so that the server piece can broadcast to
multiple clients, I just can't receive any messages when there are
more than one clients connected. I am probably explaining this
poorly, so I will let the code speak for itself:
package myxserver;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.beans.*;
import java.io.*;
import java.awt.*;
import java.net.*;
public class TheXServer extends JFrame implements Runnable {
private final static int MAX_CONNECTIONS = 100;
private static boolean REMOVING = false;
protected WindowManager m_wm;
protected JTextArea m_consoletext, m_consolechat;
protected JTextField m_chatText;
protected JLabel m_status;
protected static int m_count;
protected static boolean m_connected;
protected static DataInputStream[] m_input; // input
array
protected static DataOutputStream[] m_output; // output
array
protected static Socket[] m_clients; // Client
array
protected static ServerSocket m_server; // this is
what waits for messages
protected static Thread[] m_listenthread; // an
array of listeners
protected static ConThread m_conthread; //
strangely, this needs to be here
public TheXServer() {
m_clients = new Socket[MAX_CONNECTIONS];
m_listenthread = new Thread[MAX_CONNECTIONS];
m_output = new DataOutputStream[MAX_CONNECTIONS];
m_input = new DataInputStream[MAX_CONNECTIONS];
setTitle("The X Server");
m_count = 0;
m_status = new JLabel("No Client");
JScrollPane scroller = new JScrollPane();
m_wm = new WindowManager(this);
getContentPane().setLayout(new BorderLayout());
getContentPane().add("Center", setupConsole());
getContentPane().add("South", m_status);
Dimension dim = getToolkit().getScreenSize();
setSize(520,350);
setLocation(dim.width/2-getWidth()/3, dim.height/2-getHeight()/2);
WindowListener l = new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
};
addWindowListener(l);
setVisible(true);
}
public JInternalFrame setupConsole() {
JInternalFrame console = new JInternalFrame ("The X Server",
false, false, false, false);
console.setBounds(2, 2, 500, 300);
console.setVisible(true);
JPanel chatPanel = new JPanel();
JLabel chatLabel = new JLabel(" CHAT ");
chatPanel.setLayout(new BorderLayout());
m_consoletext = new JTextArea();
m_consoletext.setPreferredSize(new Dimension(500, 50));
m_consoletext.setLineWrap(true);
m_consoletext.setText("Server Started.\nWaiting for client ...");
m_consoletext.setEditable(false);
m_consolechat = new JTextArea();
m_consolechat.setLineWrap(true);
m_consolechat.setEditable(false);
m_chatText = new JTextField();
m_chatText.addActionListener(new ChatAdapter());
JButton chatSend = new JButton("Send");
chatSend.addActionListener(new ChatAdapter());
JPanel sendPanel = new JPanel();
sendPanel.setLayout(new BorderLayout());
sendPanel.add("Center", m_chatText);
sendPanel.add("West", chatSend);
JScrollPane cscroller1 = new JScrollPane(m_consoletext);
JScrollPane cscroller2 = new JScrollPane(m_consolechat);
chatPanel.add("North", chatLabel);
chatPanel.add("Center", cscroller2);
chatPanel.add("South", sendPanel);
JSplitPane splitter = new JSplitPane(JSplitPane.VERTICAL_SPLIT,
true, cscroller1, chatPanel);
console.getContentPane().add(splitter);
try {
m_server = new ServerSocket(5000,500);
}
catch (Exception e) {
m_consoletext.append("\n"+e);
}
m_conthread = new ConThread();
return console;
}
public void run() {
while (m_connected) {
int i = 0;
try {
for (; i < m_count; i++)
processMessage(m_input.readUTF());
}
catch (Exception e) {
removeSocketConnections(i);
keepRunning(i);
}
}
}
public void keepRunning(int i) {
try {
for (; i < m_count; i++)
processMessage(m_input.readUTF());
}
catch (Exception e) {
removeSocketConnections(i);
keepRunning(i);
}
}
private static void removeSocketConnections(int x) {
REMOVING = true;
for (int i = x; i < m_count; i++) {
if (i + 1 < MAX_CONNECTIONS) {
m_input = m_input[i+1];
m_output = m_output[i+1];
m_clients = m_clients[i+1];
m_listenthread = m_listenthread[i+1];
}
else {
m_input = null;
m_output = null;
m_clients = null;
m_listenthread = null;
}
}
m_count--;
if (m_count <= 0) m_connected = false;
REMOVING = false;
}
public void processMessage(String s) {
if (s.startsWith("cc")) {
m_consolechat.append("CLIENT: " + s.substring(2) + "\n");
m_consolechat.setCaretPosition(m_consolechat.getText().length());
}
}
public static void main(String[] args) {
new TheXServer();
}
class ChatAdapter implements ActionListener {
public void actionPerformed (ActionEvent e) {
m_wm.sendMessage("cc" + m_chatText.getText());
m_consolechat.append("SERVER: " + m_chatText.getText() + "\n");
m_chatText.setText("");
}
}
class ConThread extends Thread {
ConThread() { start(); }
public void run() {
while (true) {
if (!REMOVING) {
try {
if (m_count+1 < MAX_CONNECTIONS) {
m_clients[m_count] = m_server.accept();
m_connected = true;
m_input[m_count] = new
DataInputStream(m_clients[m_count].getInputStream());
m_output[m_count] = new
DataOutputStream(m_clients[m_count].getOutputStream());
m_consoletext.append("\nStreams established ... ");
m_listenthread[m_count] = new Thread(TheXServer.this);
m_listenthread[m_count].start();
m_status.setText("Client Connected from: " +
m_clients[m_count].getInetAddress());
m_consoletext.append("\nClient Connected from: " +
m_clients[m_count].getInetAddress());
m_count++;
}
}
catch (Exception e) {
m_consoletext.append("\n" + e);
}
}
}
}
}
public class WindowManager extends DefaultDesktopManager {
protected JFrame m_desktop;
public WindowManager(JFrame desktop) { m_desktop = desktop; }
public WindowWatcher getWindowWatcher() { return null; }
public void sendMessage(String s) {
int i = 0;
try {
if (m_output != null) {
for (; i < m_count; i++)
m_output.writeUTF(s);
}
}
catch (Exception e) {
removeSocketConnections(i);
sendMessage(s, i);
}
}
public void sendMessage(String s, int i) {
try {
if (m_output != null) {
for (; i < m_count; i++)
m_output.writeUTF(s);
}
}
catch (Exception e) {
removeSocketConnections(i);
sendMessage(s, i);
}
}
public String getStringIndex(Component f) {
String s = f.toString();
while (s.length() < 3)
s = ("0").concat(s);
return s;
}
public String getString(int num) {
String s;
if (num < 0)
s = "" + (-num);
else
s = "" + num;
while (s.length() < 6)
s = ("0").concat(s);
if (num < 0)
s = "-" + s.substring(1, 6);
return s;
}
}
}
Now, this is my first time playing with sockets, so I am sure that
there are MANY problems with my code. This code is hacked together by
looking over several socket examples and pulling out all the
unnecessary code. My core problem that I need resolved is the fact
that when more than one client piece is connected to it, the server
never spits out a message. It seems to me that it is stuck in the Run
loop, but I can't really tell. By the way, the client piece looks
almost exactly like this, with some very minor differences ... like a
way to connect to the server and it doesn't have arrays of the input,
output and socket structures. If anyone has had much experience with
sockets and can point me to some good examples, I would be more than
happy to encorporate some more foreign code to try to get the jumble
to work. Thanks in advance.
Dan