Skip to content

Instantly share code, notes, and snippets.

@aurorapar
Created September 21, 2017 01:40
Show Gist options
  • Save aurorapar/2d94a59ebd1ea5cecc540ceb4c76e870 to your computer and use it in GitHub Desktop.
Save aurorapar/2d94a59ebd1ea5cecc540ceb4c76e870 to your computer and use it in GitHub Desktop.
/**
* Client program requesting current date from server.
*
* Figure 3.27
*
* @author Gagne, Galvin, Silberschatz
* Operating System Concepts with Java - Eighth Edition
* Copyright John Wiley & Sons - 2010.
*/
import java.net.*;
import java.io.*;
public class DateClient
{
public static void main(String[] args) throws IOException {
InputStream clientInput = null;
OutputStream clientOutput = null;
BufferedReader bin = null;
Socket sock = null;
String sendData = "CLIENT: Marco";
System.out.println("Trying to connect to server " + args[0] + ":" + args[1]);
try
{
sock = new Socket(args[0],Integer.parseInt(args[1]));
System.out.println("Socket has been established.");
clientOutput = sock.getOutputStream();
PrintWriter pout = new PrintWriter(clientOutput, true);
pout.println(sendData);
System.out.println("Waiting for reply...");
clientInput = sock.getInputStream();
String replyMessage = "";
int replyCode = clientInput.read();
while(replyCode > 10)
{
replyMessage += (char) replyCode;
replyCode = clientInput.read();
}
System.out.println("Server replied with: " + replyMessage);
sock.close();
}
catch (IOException ioe) {
System.err.println(ioe);
}
finally {
sock.close();
}
}
}
/**
* Time-of-day server listening to port 6013.
*
* Figure 3.26
*
* @author Gagne, Galvin, Silberschatz
* Operating System Concepts with Java - Eighth Edition
* Copyright John Wiley & Sons - 2010.
*/
import java.net.*;
import java.io.*;
import java.util.*;
public class DateServer
{
public static void main(String[] args) {
InputStream serverInputStream = null;
BufferedReader bin = null;
System.out.println("Trying on port " + args[0]);
try
{
ServerSocket sock = new ServerSocket(Integer.parseInt(args[0]));
System.out.println("Server is up ...");
// now listen for connections
System.out.println("Server is listening ...");
Socket client = sock.accept();
System.out.println("Client socket has been established.");
// we have a connection
while(!client.isClosed())
{
System.out.println("Connected to " + client.getInetAddress());
String clientReply = "";
serverInputStream = client.getInputStream();
int streamReturn = serverInputStream.read();
while(streamReturn > 10) // read() should return -1 at EOF but returns 10, I can not explain this behavior.
{
clientReply += (char) streamReturn;
streamReturn = serverInputStream.read();
}
System.out.println(clientReply);
System.out.println("No more data from input");
System.out.println("Trying to send to client...");
PrintWriter pout = new PrintWriter(client.getOutputStream(), true);
pout.println(clientReply);
System.out.println("Replied to sender");
client.close();
// Server closes the connection it makes to the client
}
}
catch (IOException ioe) {
System.err.println(ioe);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment