Created
May 31, 2024 21:50
-
-
Save theepicsnail/d52a8975686c914b2cc6a8a4f8c70022 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.awt.event.ActionEvent; | |
import java.awt.event.ActionListener; | |
import javax.swing.JFrame; | |
import javax.swing.JTextArea; | |
import javax.swing.JTextField; | |
/* | |
* Chatroom.java | |
* | |
* Created on February 9, 2006, 12:03 PM | |
* | |
* To change this template, choose Tools | Template Manager | |
* and open the template in the editor. | |
*/ | |
/** | |
* | |
* @author TheEpicSnail | |
*/ | |
public class Chatroom { | |
public static class Client extends Internet{ | |
Connection c = new Connection(this); | |
JFrame frame = new JFrame("Client"); | |
JTextArea area = new JTextArea(""); | |
JTextField field = new JTextField(); | |
String name = ""; | |
public Client(String n){ | |
name = n; | |
c.connect("127.0.0.1",1000); | |
field.addActionListener(new ActionListener(){ | |
public void actionPerformed(ActionEvent ae){ | |
c.broadcast(name+":"+field.getText()); | |
field.setText(""); | |
} | |
}); | |
frame.getContentPane().add("Center",area); | |
frame.getContentPane().add("South",field); | |
frame.setSize(200,200); | |
frame.setVisible(true); | |
} | |
public void received(String s){ | |
area.append(s+"\n");} | |
} | |
public static class Server extends Internet{ | |
Connection c = new Connection(this); | |
JFrame frame = new JFrame("Server"); | |
JTextArea area = new JTextArea(""); | |
public Server(){ | |
c.listen(1000); | |
frame.getContentPane().add("Center",area); | |
frame.setSize(200,200); | |
frame.setVisible(true); | |
} | |
public void received(String s){ | |
area.append(s+"\n"); | |
c.broadcast(s); | |
} | |
} | |
public static void main(String[] args) { | |
new Chatroom.Server(); | |
new Chatroom.Client("person 1"); | |
new Chatroom.Client("person 2"); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.IOException; | |
import java.io.InputStream; | |
import java.io.OutputStream; | |
import java.net.ServerSocket; | |
import java.net.Socket; | |
import java.util.ArrayList; | |
/* | |
* Connection | |
* | |
* Created on February 9, 2006, 11:09 AM | |
* | |
* To change this template, choose Tools | Template Manager | |
* and open the template in the editor. | |
*/ | |
/** | |
* | |
* @author TheEpicSnail | |
*/ | |
public class Connection { | |
private Internet inter = null; | |
private ServerSocket ss = null; | |
private Socket sock = null; | |
private boolean created = false; | |
private ArrayList<listener> Listeners = new ArrayList<listener>(); | |
public Connection(Internet i){ | |
inter = i; | |
} | |
public void listen(int port){ | |
if(created){ | |
System.out.println("Connection already being used."); | |
return; | |
} | |
try{ | |
ss = new ServerSocket(port); | |
new server(this,ss); | |
created = true; | |
}catch (Exception e){ | |
e.printStackTrace(); | |
} | |
} | |
public void connect(String ip , int port){ | |
if(created){ | |
System.out.println("Connection already being used."); | |
return; | |
} | |
try{ | |
Socket s = new Socket(ip,port); | |
Listeners.add(new listener(this,s)); | |
created = true; | |
System.out.println("Connected:"+this); | |
}catch (Exception e){ | |
e.printStackTrace(); | |
} | |
} | |
public void reset(){ | |
for (listener elem : Listeners) { | |
elem.close(); | |
} | |
while(Listeners.size()!=0)Listeners.remove(0); | |
created = false; | |
ss = null; | |
sock = null; | |
} | |
public void broadcast(String s){ | |
for (listener elem : Listeners) { | |
elem.send(s); | |
} | |
} | |
public String toString(){ | |
String s = "Connection[Created="+created; | |
if(created){ | |
s+=" Type="; | |
if(ss==null) | |
s+="Client"; | |
else { | |
s+="Server Connections="+Listeners.size(); | |
} | |
} | |
s+="]"; | |
return s; | |
} | |
private class server implements Runnable{ | |
ServerSocket serv; | |
Connection conn; | |
Thread self ; | |
public server(Connection c,ServerSocket ss){ | |
conn=c; | |
serv=ss; | |
self = new Thread(this); | |
self.start(); | |
} | |
public void run(){ | |
while(true){ | |
try { | |
Socket s = serv.accept(); | |
conn.Listeners.add(new listener(conn,s)); | |
System.out.println("Connected:"+this); | |
} catch (IOException ex) { | |
ex.printStackTrace(); | |
} | |
} | |
} | |
} | |
private class listener implements Runnable{ | |
Socket sock; | |
InputStream is; | |
OutputStream os; | |
Connection conn = null; | |
Thread self ; | |
public listener(Connection c ,Socket s){ | |
try{ | |
is = s.getInputStream(); | |
os = s.getOutputStream(); | |
sock = s; | |
conn = c; | |
self = new Thread(this); | |
self.start(); | |
}catch (Exception e){ | |
e.printStackTrace(); | |
} | |
} | |
public void run(){ | |
try { | |
while (true){ | |
String buffer = ""; | |
for(;is.available()>0;buffer+=(char)is.read()); | |
if(buffer.length()>0){conn.inter.received(buffer);buffer="";} | |
} | |
} catch (Exception ex) { | |
ex.printStackTrace(); | |
close(); | |
} | |
} | |
public void send(String s){ | |
try{ | |
os.write(s.getBytes()); | |
}catch (Exception e){ | |
e.printStackTrace(); | |
} | |
} | |
public void close(){ | |
try{ | |
sock.close(); | |
}catch (Exception e){} | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Internet.java | |
* | |
* Created on February 9, 2006, 11:50 AM | |
* | |
* To change this template, choose Tools | Template Manager | |
* and open the template in the editor. | |
*/ | |
/** | |
* | |
* @author TheEpicSnail | |
*/ | |
public abstract class Internet { | |
public abstract void received(String s); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment