Last active
October 15, 2018 04:05
-
-
Save NicMcPhee/2060037163d0d7fb475b5e4395b9ec32 to your computer and use it in GitHub Desktop.
Simple Date client/server example. Start the server, and then running the client gets the date from the server and prints it.
This file contains hidden or 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.net.*; | |
import java.io.*; | |
public class DateClient { | |
public static final int portNumber = 6013; | |
public static void main(String[] args) throws IOException { | |
String server; | |
// Use "127.0.0.1", i.e., localhost, if no server is specified. | |
if (args.length == 0) { | |
server = "127.0.0.1"; | |
} else { | |
server = args[0]; | |
} | |
try { | |
// Connect to the server | |
Socket socket = new Socket(server, portNumber); | |
// Get the input stream so we can read from that socket | |
InputStream input = socket.getInputStream(); | |
BufferedReader reader = new BufferedReader(new InputStreamReader(input)); | |
// Print all the input we receive from the server | |
String line; | |
while ((line = reader.readLine()) != null) { | |
System.out.println(line); | |
} | |
// Close the socket when we're done reading from it | |
socket.close(); | |
// Provide some minimal error handling. | |
} catch (ConnectException ce) { | |
System.out.println("We were unable to connect to " + server); | |
System.out.println("You should make sure the server is running."); | |
} catch (IOException ioe) { | |
System.out.println("We caught an unexpected exception"); | |
System.err.println(ioe); | |
} | |
} | |
} |
This file contains hidden or 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.net.*; | |
import java.io.*; | |
public class DateServer { | |
public static final int portNumber = 6013; | |
public static void main(String[] args) { | |
try { | |
// Start listening on the specified port | |
ServerSocket sock = new ServerSocket(portNumber); | |
// Run forever, which is common for server style services | |
while (true) { | |
// Wait until someone connects, thereby requesting a date | |
Socket client = sock.accept(); | |
System.out.println("Got a request!"); | |
// Construct a writer so we can write to the socket, thereby | |
// sending something back to the client. | |
PrintWriter writer = new PrintWriter(client.getOutputStream(), true); | |
// Send the current date back tothe client. | |
writer.println(new java.util.Date().toString()); | |
// Close the client socket since we're done. | |
client.close(); | |
} | |
// *Very* minimal error handling. | |
} catch (IOException ioe) { | |
System.out.println("We caught an unexpected exception"); | |
System.err.println(ioe); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment