Contents
- CapsEchoServer.java
- ClientBiDirectionalSocketTest.java
- EchoServer.java
- InetAddressTest.java
- MultiThreadedServer.java
- SocketTest.java
- ThreadedEchoHandler.java
- TimeoutSockets.java
CapsEchoServer.java 1/8
[top][prev][next]
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
/**
* Server that listens on port 1999, prints out the client's message
* (uppercase), and then sends the client's message, uppercased, back
* to the client.
* @author Sara Sprenkle
*/
public class CapsEchoServer {
/**
* @param args
*/
public static void main(String[] args) {
try {
ServerSocket server = new ServerSocket(1999);
Socket incoming = server.accept();
BufferedReader br = new BufferedReader(new InputStreamReader(
incoming.getInputStream()));
String line = null;
PrintWriter out = new PrintWriter(incoming.getOutputStream());
while ((line = br.readLine()) != null) {
String upperLine = line.toUpperCase();
System.out.println(upperLine);
out.println(upperLine);
}
} catch (IOException exc) {
System.out.println("Error:" + exc);
}
}
}
ClientBiDirectionalSocketTest.java 2/8
[top][prev][next]
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
/**
* This test program opens both input and output streams on the same socket -- to
* both read from and write to the server.
*
* For more on time servers: http://tf.nist.gov/tf-cgi/servers.cgi
*
* @author sprenkle
*
*/
public class ClientBiDirectionalSocketTest {
/**
* @param args
*/
public static void main(String[] args) {
try {
Socket s = new Socket("time-d.nist.gov", 13);
BufferedReader in = new BufferedReader(new InputStreamReader(s
.getInputStream()));
PrintWriter out = new PrintWriter(s.getOutputStream(), true); // auto-flush
// TODO: read from in (input received from server)
// TODO: write to out (output send to server)
} catch (IOException exp) {
System.out.println("Error:" + exp);
}
}
}
EchoServer.java 3/8
[top][prev][next]
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
/**
* Server listens on port 1999, echoes client's message until the
* client enters "BYE".
*
* @author sprenkle
*/
public class EchoServer {
/**
* @param args
*/
public static void main(String[] args) {
try {
ServerSocket server = new ServerSocket(1999);
// will block until a client connects
Socket incoming = server.accept();
BufferedReader in = new BufferedReader(new InputStreamReader(
incoming.getInputStream()));
PrintWriter out = new PrintWriter(incoming.getOutputStream(), true);
out.println("Echo Server. Type BYE to exit");
String line = null;
while ((line = in.readLine()) != null) {
if (line.trim().equals("BYE")) {
break;
}
else {
out.println("Echo:" + line.trim());
}
}
incoming.close();
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
InetAddressTest.java 4/8
[top][prev][next]
import java.net.InetAddress;
import java.net.UnknownHostException;
/**
* Practice using InetAddress class
*
* @author sprenkle
*
*/
public class InetAddressTest {
/**
* @param args
*/
public static void main(String[] args) {
try {
InetAddress addr = InetAddress.getByName("www.cs.wlu.edu");
System.out.println(addr);
InetAddress[] addresses = InetAddress.getAllByName("www.cnn.com");
for (InetAddress ia : addresses) {
System.out.println(ia);
}
addresses = InetAddress.getAllByName("www.espn.com");
for (InetAddress ia : addresses) {
System.out.println(ia);
}
InetAddress address = InetAddress.getLocalHost();
System.out.println(address);
} catch (UnknownHostException e) {
e.printStackTrace();
}
}
}
MultiThreadedServer.java 5/8
[top][prev][next]
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
/**
* An example of a multi-threaded server that spawns a thread for every incoming
* request.
*
* @author sprenkle
*
*/
public class MultiThreadedServer {
/**
* @param args
*/
public static void main(String[] args) {
ServerSocket server;
try {
server = new ServerSocket(1999);
while (true) {
Socket incoming = server.accept();
Thread clientThread = new ThreadedEchoHandler(incoming);
clientThread.start();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
SocketTest.java 6/8
[top][prev][next]
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.Socket;
/**
* Practice using a socket to connect to a server and read from the socket.
*
* @author sprenkle
*
*/
public class SocketTest {
public static void main(String argv[]) {
try {
Socket socket = new Socket("time-d.nist.gov", 13);
BufferedReader in = new BufferedReader(new InputStreamReader(socket
.getInputStream()));
String line = null;
while ((line = in.readLine()) != null) {
System.out.println(line);
}
} catch (IOException exc) {
System.out.println("Error:" + exc);
}
}
}
ThreadedEchoHandler.java 7/8
[top][prev][next]
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
/**
* Implementation of the EchoServer's handling of clients. The
* MultiThreadedServer delegates to this class.
*
* Alternatively, you could have the class implement Runnable
* interface. (Tradeoffs for extending Thread class versus
* implementing Runnable interface?)
*
* @author sprenkle
*/
public class ThreadedEchoHandler extends Thread {
private Socket incoming;
public ThreadedEchoHandler(Socket incoming) {
this.incoming = incoming;
}
public void run() {
try {
BufferedReader in = new BufferedReader(new InputStreamReader(
incoming.getInputStream()));
PrintWriter out = new PrintWriter(incoming.getOutputStream(), true);
out.println("CAPS Echo Server. Type BYE to exit");
String line = null;
while ((line = in.readLine()) != null) {
if (line.trim().equals("BYE")) {
break;
}
else {
out.println("Echo:" + line.trim().toUpperCase());
}
}
incoming.close();
out.close();
} catch (IOException exc) {
System.out.println("Error:" + exc);
}
}
}
TimeoutSockets.java 8/8
[top][prev][next]
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;
/**
* Demonstrates use of timeouts
*
* @author sprenkle
*
*/
public class TimeoutSockets {
/**
* @param args
*/
public static void main(String[] args) {
try {
Socket sckt1 = new Socket("host", 1998);
sckt1.setSoTimeout(10000); // 10 second timeout
BufferedReader in = new BufferedReader(new InputStreamReader(sckt1
.getInputStream()));
String line;
while ((line = in.readLine()) != null) {
System.out.println("Handling " + line);
}
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
Generated by GNU Enscript 1.6.6.