first commit
This commit is contained in:
@@ -0,0 +1,28 @@
|
||||
import java.io.Serializable;
|
||||
|
||||
|
||||
public abstract class AbstractNetworkMessage implements Serializable {
|
||||
|
||||
//////////////////////////////// Data Members ///////////////////////////////
|
||||
private String messageName;
|
||||
|
||||
//////////////////////////////// Constructors ///////////////////////////////
|
||||
public AbstractNetworkMessage() {
|
||||
messageName = "AbstractMessage";
|
||||
}
|
||||
|
||||
public AbstractNetworkMessage(String name) {
|
||||
messageName = name;
|
||||
}
|
||||
|
||||
////////////////////////////////// Methods //////////////////////////////////
|
||||
|
||||
public String getMessageName() {
|
||||
return messageName;
|
||||
}
|
||||
|
||||
public String tokenize() {
|
||||
return messageName;
|
||||
}
|
||||
|
||||
}// end of class AbstractNetworkMessage
|
||||
32
CS2335/lab4/Lab_files/Examples/Networking/ClientExample.java
Normal file
32
CS2335/lab4/Lab_files/Examples/Networking/ClientExample.java
Normal file
@@ -0,0 +1,32 @@
|
||||
import java.io.*;
|
||||
import java.net.*;
|
||||
|
||||
class ClientExample {
|
||||
public static final int PORT = 1234;
|
||||
|
||||
public static void main(String argv[]) {
|
||||
try {
|
||||
Socket s = new Socket("localhost", PORT);
|
||||
BufferedReader br = new BufferedReader(new
|
||||
InputStreamReader(s.getInputStream()));
|
||||
PrintWriter pr = new PrintWriter(new
|
||||
OutputStreamWriter(s.getOutputStream()));
|
||||
ObjectOutputStream oos = new ObjectOutputStream(s.getOutputStream());
|
||||
|
||||
System.out.println(s.getLocalAddress().toString());
|
||||
System.out.println(s.getLocalSocketAddress().toString());
|
||||
|
||||
String message = null;
|
||||
|
||||
BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in));
|
||||
|
||||
while((message = stdIn.readLine()) != null) {
|
||||
System.out.println("Sending message: " + message);
|
||||
oos.writeObject(new TextMessage(message));
|
||||
oos.flush();
|
||||
}
|
||||
} catch (IOException e) {
|
||||
System.out.println("IO Exception: " + e.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
23
CS2335/lab4/Lab_files/Examples/Networking/NumberMessage.java
Normal file
23
CS2335/lab4/Lab_files/Examples/Networking/NumberMessage.java
Normal file
@@ -0,0 +1,23 @@
|
||||
|
||||
public class NumberMessage extends AbstractNetworkMessage {
|
||||
|
||||
//////////////////////////////// Data Members ///////////////////////////////
|
||||
private int myNumber;
|
||||
|
||||
//////////////////////////////// Constructors ///////////////////////////////
|
||||
/**
|
||||
* Creates a new <code>NumberMessage</code> instance.
|
||||
*
|
||||
*/
|
||||
public NumberMessage(int number) {
|
||||
super("NumberMessage");
|
||||
|
||||
myNumber = number;
|
||||
}
|
||||
|
||||
////////////////////////////////// Methods //////////////////////////////////
|
||||
public String tokenize() {
|
||||
return super.tokenize() + ": " + Integer.toString(myNumber);
|
||||
}
|
||||
|
||||
}// end of class NumberMessage
|
||||
@@ -0,0 +1,24 @@
|
||||
import java.io.*;
|
||||
import java.net.*;
|
||||
|
||||
class ClientExample {
|
||||
public static final int PORT = 1234;
|
||||
|
||||
public static void main(String argv[]) {
|
||||
try {
|
||||
Socket s = new Socket("localhost", PORT);
|
||||
BufferedReader br = new BufferedReader(new
|
||||
InputStreamReader(s.getInputStream()));
|
||||
PrintWriter pr = new PrintWriter(new
|
||||
OutputStreamWriter(s.getOutputStream()));
|
||||
|
||||
System.out.println(s.getLocalAddress().toString());
|
||||
System.out.println(s.getLocalSocketAddress().toString());
|
||||
pr.println("Hello, world!");
|
||||
pr.flush();
|
||||
System.out.println(br.readLine());
|
||||
} catch (IOException e) {
|
||||
System.out.println("IO Exception: " + e.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
import java.io.*;
|
||||
import java.net.*;
|
||||
|
||||
class ServerExample extends Thread {
|
||||
|
||||
Socket socket;
|
||||
public static final int PORT = 1234;
|
||||
|
||||
public ServerExample(Socket socket) {
|
||||
this.socket = socket;
|
||||
}
|
||||
|
||||
public void run() {
|
||||
try {
|
||||
BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
|
||||
PrintWriter pr = new PrintWriter(new OutputStreamWriter(socket.getOutputStream()));
|
||||
|
||||
String s = br.readLine();
|
||||
System.out.println("Received " + s);
|
||||
pr.println("Thank you for your input");
|
||||
pr.flush();
|
||||
} catch (IOException ioe) {
|
||||
System.out.println("Got IO Exception: " + ioe.getMessage());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public static void main(String argv[]) {
|
||||
try {
|
||||
ServerSocket ss = new ServerSocket(PORT);
|
||||
while (true) {
|
||||
Socket s = ss.accept();
|
||||
new ServerExample(s).start();
|
||||
}
|
||||
} catch (IOException ioe) {
|
||||
System.out.println("Got IO Exception: " + ioe.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
45
CS2335/lab4/Lab_files/Examples/Networking/ServerExample.java
Normal file
45
CS2335/lab4/Lab_files/Examples/Networking/ServerExample.java
Normal file
@@ -0,0 +1,45 @@
|
||||
import java.io.*;
|
||||
import java.net.*;
|
||||
|
||||
class ServerExample extends Thread {
|
||||
|
||||
Socket socket;
|
||||
public static final int PORT = 1234;
|
||||
|
||||
public ServerExample(Socket socket) {
|
||||
this.socket = socket;
|
||||
}
|
||||
|
||||
public void run() {
|
||||
try {
|
||||
BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
|
||||
PrintWriter pr = new PrintWriter(new OutputStreamWriter(socket.getOutputStream()));
|
||||
ObjectInputStream ois = new ObjectInputStream(socket.getInputStream());
|
||||
AbstractNetworkMessage anm;
|
||||
|
||||
while((anm = ((AbstractNetworkMessage)(ois.readObject()))) != null) {
|
||||
String s = anm.tokenize();
|
||||
System.out.println("Received " + s);
|
||||
pr.println("Thank you for your input");
|
||||
pr.flush();
|
||||
}
|
||||
} catch (IOException ioe) {
|
||||
System.out.println("Got IO Exception: " + ioe.getMessage());
|
||||
} catch(Exception e) {
|
||||
System.out.println("Something aint workin': " + e.getMessage());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public static void main(String argv[]) {
|
||||
try {
|
||||
ServerSocket ss = new ServerSocket(PORT);
|
||||
while (true) {
|
||||
Socket s = ss.accept();
|
||||
new ServerExample(s).start();
|
||||
}
|
||||
} catch (IOException ioe) {
|
||||
System.out.println("Got IO Exception: " + ioe.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
19
CS2335/lab4/Lab_files/Examples/Networking/TextMessage.java
Normal file
19
CS2335/lab4/Lab_files/Examples/Networking/TextMessage.java
Normal file
@@ -0,0 +1,19 @@
|
||||
|
||||
public class TextMessage extends AbstractNetworkMessage {
|
||||
|
||||
//////////////////////////////// Data Members ///////////////////////////////
|
||||
private String myMessage;
|
||||
|
||||
//////////////////////////////// Constructors ///////////////////////////////
|
||||
public TextMessage(String msg) {
|
||||
super("TextMessage");
|
||||
|
||||
myMessage = msg;
|
||||
}
|
||||
|
||||
////////////////////////////////// Methods //////////////////////////////////
|
||||
public String tokenize() {
|
||||
return super.tokenize() + ": " + myMessage;
|
||||
}
|
||||
|
||||
}// end of class TextMessage
|
||||
Reference in New Issue
Block a user