Initial commit
This commit is contained in:
BIN
Problem 3.16/SocketClient.class
Normal file
BIN
Problem 3.16/SocketClient.class
Normal file
Binary file not shown.
24
Problem 3.16/SocketClient.java
Normal file
24
Problem 3.16/SocketClient.java
Normal file
@ -0,0 +1,24 @@
|
||||
import java.net.*;
|
||||
import java.io.*;
|
||||
|
||||
public class SocketClient {
|
||||
public static void main(String[] args) {
|
||||
try {
|
||||
// make connection to server socket
|
||||
Socket sock = new Socket("127.0.0.1", 6017);
|
||||
InputStream in = sock.getInputStream();
|
||||
BufferedReader bin = new BufferedReader(new InputStreamReader(in));
|
||||
|
||||
// get a qoute from the socket
|
||||
String line;
|
||||
while ( (line = bin.readLine()) != null)
|
||||
System.out.println(line);
|
||||
|
||||
// close the socket connection
|
||||
sock.close();
|
||||
}
|
||||
catch (IOException ioe) {
|
||||
System.err.println(ioe);
|
||||
}
|
||||
}
|
||||
}
|
BIN
Problem 3.16/SocketServer.class
Normal file
BIN
Problem 3.16/SocketServer.class
Normal file
Binary file not shown.
36
Problem 3.16/SocketServer.java
Normal file
36
Problem 3.16/SocketServer.java
Normal file
@ -0,0 +1,36 @@
|
||||
import java.net.*;
|
||||
import java.io.*;
|
||||
|
||||
public class SocketServer {
|
||||
public static String[] qoutes = {
|
||||
"Don't cry because it's over, smile because it happened.",
|
||||
"Be yourself; everyone else is already taken.",
|
||||
"Two things are infinite: the universe and human stupidity; and I'm not sure about the universe.",
|
||||
"So many books, so little time.",
|
||||
"Be who you are and say what you feel, because those who mind don't matter, and those who matter don't mind."
|
||||
};
|
||||
|
||||
public static void main(String[] args) {
|
||||
try {
|
||||
int i = 0;
|
||||
ServerSocket sock = new ServerSocket(6017);
|
||||
|
||||
// Listening for connections
|
||||
while (true) {
|
||||
Socket client = sock.accept();
|
||||
PrintWriter pout = new PrintWriter(client.getOutputStream(), true);
|
||||
|
||||
// write a qoute to the socket
|
||||
pout.println(qoutes[i]);
|
||||
i = (i + 1) % qoutes.length;
|
||||
|
||||
// close the socket and resume
|
||||
// listening for connections
|
||||
client.close();
|
||||
}
|
||||
}
|
||||
catch (IOException ioe) {
|
||||
System.err.println(ioe);
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user