/ Published in: Java
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
In a nutshell, I just recently starting attempting to use Sockets and decided I wanted to give a go at a chat program. I have some experience with threads and based on the weird deadlock issues I have come to the conclusion that I must have some non thread-safe calls somewhere. Any help would be appreciated. *Client-Side Files* {code} { public Client() { conn = null; out = null; in = null; } { obsList = new ArrayList<IClientObs>(); try { if (conn.isConnected()) { } notify("Server: Welcome! Connected to '" + conn.getRemoteSocketAddress() + "'.\n"); } { notify("Console: Error while attempting to connect to the server.\n"); } } { obsList = new ArrayList<IClientObs>(); try { conn = pConn; if (conn.isConnected()) { } notify("Server: Welcome! Connected to '" + conn.getRemoteSocketAddress() + "'.\n"); } { notify("Console: Error while attempting to connect to the server.\n"); } } public void run() { while (conn.isConnected()) { nextMsg(); } } { try { notify(msg); } { } { } finally { //notify("Console: Error while attempting to read messages from server.\n"); } return(msg); } { boolean sentMsg = false; try { if (out != null) { out.writeObject(msg); out.flush(); sentMsg = true; } } { notify("Console: Error while attempting to send message to server.\n"); } return(sentMsg); } public void closeConn() { try { if (conn != null && in != null && out != null) { notify("Server: you have been disconnected from '" + conn.getRemoteSocketAddress() + "'.\n"); sendMsg("~~QUIT~~"); conn.close(); in.close(); out.close(); } } { notify("Console: Error while closing connection."); } } { return(conn.getRemoteSocketAddress().toString()); } public boolean isConnected() { return(conn.isConnected()); } private ArrayList<IClientObs> obsList; public void addObserver(IClientObs obs) { if (obs != null) { obsList.add(obs); } } public void delObserver(IClientObs obs) { if (obs != null) { obsList.remove(obs); } } { for(IClientObs obs : obsList) obs.update(msg); } } //////////////////////////////////////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////////////////////////////////////// public interface IClientObs { } //////////////////////////////////////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////////////////////////////////////// { private Client client; public ClientGui() { init("Client Chat"); frame.setBounds(100,100,600,500); frame.setVisible(true); } { cont = frame.getContentPane(); txtArea.setEditable(false); boxS.add(chatTB); boxS.add(submitBtn); boxE.add(ipTB); boxI.add(connectBtn); boxI.add(disconnBtn); boxE.add(boxI); txtArea.setEditable(false); connectBtn.addActionListener(this); disconnBtn.addActionListener(this); submitBtn.addActionListener(this); chatTB.addKeyListener(this); } private void connect() { client = new Client(ip); client.addObserver(this); client.start(); connectBtn.setEnabled(false); disconnBtn.setEnabled(true); submitBtn.setEnabled(true); } private void disconnect() { client.closeConn(); client = null; connectBtn.setEnabled(true); disconnBtn.setEnabled(false); submitBtn.setEnabled(false); } private void sendData() { if (client != null && client.isConnected()) { client.sendMsg(msg); } } { if (src == submitBtn) { sendData(); } else if (src == connectBtn) { connect(); } else if (src == disconnBtn) { disconnect(); } } { } { } { int key = e.getKeyChar(); sendData(); } { txtArea.append(msg); } } {code} *Server-Side Files* {code} { public ConnClient() { conn = null; out = null; in = null; } { try { conn = pConn; if (conn.isConnected()) { } } { } } { try { } { } { } { } return(msg); } { boolean sentMsg = false; try { if (out != null) { out.writeObject(msg); out.flush(); sentMsg = true; } } { } return(sentMsg); } { if (conn != null && in != null && out != null) { conn.close(); in.close(); out.close(); } } { return(conn.getRemoteSocketAddress().toString()); } public boolean isConnected() { return(conn.isConnected()); } } //////////////////////////////////////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////////////////////////////////////// public interface IServerObs { } //////////////////////////////////////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////////////////////////////////////// { private Server server; public ServerGui() { cont = frame.getContentPane(); strBtn.addActionListener(this); stpBtn.addActionListener(this); b.add(strBtn); b.add(stpBtn); frame.setBounds(d.width/4, d.height/4, ((int)(d.width/1.75)), ((int)(d.height/1.75))); frame.setVisible(true); } { if (src == strBtn) { if (server == null) { strBtn.setEnabled(false); stpBtn.setEnabled(true); server = new Server(); server.addObserver(this); server.start(); } else { console.append("Console: Server is alread initiated.\n"); } } else if (src == stpBtn) { strBtn.setEnabled(true); stpBtn.setEnabled(false); server.shutdown(); server = null; console.append("Console: Server has been stopped.\n"); } } { console.append("Console: " + msg + "\n"); } } {code} lol, sorry for so much code but I figured it would be necessary for context and figuring out where the problem is.