initial import of provided code
git-svn-id: svn://anubis/gvsu@14 45c1a28c-8058-47b2-ae61-ca45b979098e
This commit is contained in:
parent
e1360651fd
commit
55e67017fc
69
cs654/lab5/partI/ChatClient.java
Normal file
69
cs654/lab5/partI/ChatClient.java
Normal file
@ -0,0 +1,69 @@
|
||||
import java.net.*;
|
||||
import java.io.*;
|
||||
import java.awt.event.*;
|
||||
|
||||
public class ChatClient {
|
||||
|
||||
public ChatFrame gui;
|
||||
|
||||
private Socket socket;
|
||||
private DataInputStream in;
|
||||
private DataOutputStream out;
|
||||
|
||||
public ChatClient(String name, String server, int port) {
|
||||
|
||||
// GUI Create GUI and handle events:
|
||||
// After text input, sendTextToChat() is called,
|
||||
// When closing the window, disconnect() is called.
|
||||
|
||||
gui = new ChatFrame("Chat with Sockets");
|
||||
gui.input.addKeyListener (new EnterListener(this,gui));
|
||||
gui.addWindowListener(new ExitListener(this));
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
// create a socket, register and listen t the server
|
||||
|
||||
try {
|
||||
|
||||
socket = new Socket(server,port);
|
||||
?
|
||||
?
|
||||
out.writeUTF(name);
|
||||
while (true) {
|
||||
gui.output.append("\n"+in.readUTF());
|
||||
};
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
protected void sendTextToChat(String str) {
|
||||
try {
|
||||
?
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
protected void disconnect() {
|
||||
try {
|
||||
?
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
public static void main (String args[])throws IOException {
|
||||
if (args.length!=3)
|
||||
throw new RuntimeException ("Syntax: java ChatClient <name> <serverhost> <port>");
|
||||
int port=Integer.parseInt(args[2]);
|
||||
ChatClient c=new ChatClient(args[0], args[1], port);
|
||||
}
|
||||
}
|
||||
|
27
cs654/lab5/partI/ChatFrame.java
Normal file
27
cs654/lab5/partI/ChatFrame.java
Normal file
@ -0,0 +1,27 @@
|
||||
import java.awt.*;
|
||||
|
||||
public class ChatFrame extends Frame {
|
||||
|
||||
protected TextArea output;
|
||||
protected TextField input;
|
||||
|
||||
protected Thread listener;
|
||||
|
||||
public ChatFrame (String title){
|
||||
super (title);
|
||||
|
||||
setLayout (new BorderLayout ());
|
||||
add ("Center", output = new TextArea ());
|
||||
output.setEditable (false);
|
||||
add ("South", input = new TextField ());
|
||||
|
||||
pack ();
|
||||
show ();
|
||||
input.requestFocus ();
|
||||
}
|
||||
|
||||
public static void main (String args[]) {
|
||||
new ChatFrame("Chat ");
|
||||
}
|
||||
}
|
||||
|
60
cs654/lab5/partI/ChatHandler.java
Normal file
60
cs654/lab5/partI/ChatHandler.java
Normal file
@ -0,0 +1,60 @@
|
||||
import java.net.*;
|
||||
import java.io.*;
|
||||
import java.util.*;
|
||||
|
||||
public class ChatHandler extends Thread {
|
||||
|
||||
Socket socket;
|
||||
DataInputStream in;
|
||||
DataOutputStream out;
|
||||
String name;
|
||||
protected static Vector handlers = new Vector ();
|
||||
|
||||
public ChatHandler (String name, Socket socket) throws IOException {
|
||||
this.name = name;
|
||||
this.socket = socket;
|
||||
in = new DataInputStream (?);
|
||||
out = new DataOutputStream (?);
|
||||
}
|
||||
|
||||
public void run () {
|
||||
|
||||
try {
|
||||
broadcast(name+" entered");
|
||||
handlers.addElement (this);
|
||||
|
||||
while (true) {
|
||||
?
|
||||
broadcast(name+": "+message);
|
||||
}
|
||||
|
||||
} catch (IOException ex) {
|
||||
System.out.println("-- Connection to user lost.");
|
||||
} finally {
|
||||
handlers.removeElement (this);
|
||||
?
|
||||
try {
|
||||
?
|
||||
} catch (IOException ex) {
|
||||
System.out.println("-- Socket to user already closed ?");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
protected static void broadcast (String message) {
|
||||
synchronized (handlers) {
|
||||
Enumeration e = handlers.elements ();
|
||||
while (e.?) {
|
||||
ChatHandler handler = (ChatHandler) e.nextElement ();
|
||||
try {
|
||||
?
|
||||
?
|
||||
} catch (IOException ex) {
|
||||
handler.stop ();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
25
cs654/lab5/partI/ChatServer.java
Normal file
25
cs654/lab5/partI/ChatServer.java
Normal file
@ -0,0 +1,25 @@
|
||||
import java.net.*;
|
||||
import java.io.*;
|
||||
import java.util.*;
|
||||
|
||||
public class ChatServer {
|
||||
|
||||
public ChatServer (int port) throws IOException {
|
||||
ServerSocket server = new ServerSocket (port);
|
||||
while (true) {
|
||||
Socket client = server.accept();
|
||||
?
|
||||
String name = in.readUTF();
|
||||
|
||||
System.out.println ("New client "+name+" from " + ?);
|
||||
ChatHandler c = new ChatHandler (name, client);
|
||||
c.start ();
|
||||
}
|
||||
}
|
||||
|
||||
public static void main (String args[]) throws IOException {
|
||||
if (args.length != 1)
|
||||
throw new RuntimeException ("Syntax: java ChatServer <port>");
|
||||
new ChatServer (Integer.parseInt (args[0]));
|
||||
}
|
||||
}
|
19
cs654/lab5/partI/EnterListener.java
Normal file
19
cs654/lab5/partI/EnterListener.java
Normal file
@ -0,0 +1,19 @@
|
||||
import java.awt.event.*;
|
||||
|
||||
public class EnterListener extends KeyAdapter {
|
||||
|
||||
ChatClient client;
|
||||
ChatFrame gui;
|
||||
|
||||
public EnterListener (ChatClient client, ChatFrame gui) {
|
||||
this.client = client;
|
||||
this.gui = gui;
|
||||
}
|
||||
|
||||
public void keyPressed(KeyEvent e) {
|
||||
if (e.getKeyCode()==KeyEvent.VK_ENTER) {
|
||||
client.sendTextToChat(gui.input.getText());
|
||||
gui.input.setText("");
|
||||
}
|
||||
}
|
||||
}
|
15
cs654/lab5/partI/ExitListener.java
Normal file
15
cs654/lab5/partI/ExitListener.java
Normal file
@ -0,0 +1,15 @@
|
||||
import java.awt.event.*;
|
||||
|
||||
public class ExitListener extends WindowAdapter {
|
||||
|
||||
ChatClient client;
|
||||
|
||||
public ExitListener(ChatClient client) {
|
||||
this.client = client;
|
||||
}
|
||||
|
||||
public void windowClosing(WindowEvent e) {
|
||||
client.disconnect();
|
||||
System.exit(0);
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user