package network;
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
public class HelloServer
public static void main(String[] args)
String s;
Scanner inputStream;
PrintWriter outputStream;
ServerSocket serverSocket;
try
//Wait for connection on port 6789
[Link]("Waiting for a connection.");
//bind to a port
serverSocket = new ServerSocket(6789);//bind to a port
Socket socket = [Link]();//listen for incomming data
//Connection made, set up streams
inputStream = new Scanner(new InputStreamReader([Link]()));
outputStream = new PrintWriter(new DataOutputStream([Link]()));
//Read a line from the client
s = [Link]();
//Output text to the client
[Link]("Well, ");
[Link](s + " is a fine programming language!");
[Link]();
[Link]("Closing connection from " + s);
[Link]();
[Link]();
catch(IOException e)
//If any exception occurs, display it
[Link]("Error " + e);
package network;
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
public class HelloClient
public static void main(String[] args)
String s;
Scanner inputStream;
PrintWriter outputStream;
try
// Connect to server on same machine, port 6789
Socket clientSocket = new Socket("localhost", 6789);
// Set up streams to send/receive data
inputStream = new Scanner(new InputStreamReader([Link]()));
outputStream = new PrintWriter(new DataOutputStream([Link]()));
//send "Java" to the server
[Link]("Java");
[Link](); // Sends data to the stream
//Read everything from the server until no
// more lines and output it to the screen
while([Link]())
s = [Link]();
[Link](s);
[Link]();
[Link]();
catch(IOException e)
[Link]("Error " + e);
}
}
package network;
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
public class AdditionServer
public static void main(String[] args)
final int port = 8901;
// declare a "general" socket and a server socket
Socket connection;
ServerSocket listenSocket;
// declare low level and high level objects for input
InputStream inStream;
DataInputStream inDataStream;
// declare low level and high level objects for output
OutputStream outStream;
DataOutputStream outDataStream;
// declare other variables
String client;
int first, second, sum;
boolean connected;
while(true)
try
//create a server socket
listenSocket = new ServerSocket(port);//bind to a port
[Link]("Server started at " + new Date());
[Link]("Listening on port " + port);
// listen for a connection from the client
connection = [Link](); //
connected = true;
[Link]("Connection established");
// create an input stream from the client
inStream = [Link]();
inDataStream = new DataInputStream(inStream);
// create an output stream to the client
outStream = [Link] ();
outDataStream = new DataOutputStream(outStream);
// wait for a string from the client
client = [Link]();//listen for incoming data
[Link]("Address of client: " + client);
while(connected)
//read an integer from the client
first = [Link]();
[Link]("First number received: " + first);
// read an integer from the client
second = [Link]();
[Link]("Second number received: " + second);
sum = first + second;
[Link]("Sum returned: " + sum);
// send the sum to the client
[Link](sum);
catch (IOException e)
connected = false;
package network;
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
public class AdditionClient extends Application
private String remoteHost;
private int port;
//declare low level and high level objects for input
private InputStream inStream;
private DataInputStream inDataStream;
//declare low level and high level objects for output
private OutputStream outStream ;
private DataOutputStream outDataStream;
//declare a socket
private Socket connection;
@Override
public void start(Stage stage)
getInfo(); // call the method that gets the information about the server
//declare visual components
TextField msg = new TextField();
TextField firstNumber = new TextField();
Label plus = new Label("+");
TextField secondNumber = new TextField();
Label equals = new Label("=");
Label sum = new Label();
Button calculateButton = new Button("Press to see the sum of the two numbers");
//configure the scene
[Link](150);
[Link](30);
[Link](30);
HBox hBox = new HBox(10);
[Link]([Link]);
[Link]().addAll(firstNumber, plus, secondNumber, equals, sum);
VBox root = new VBox(10);
[Link]([Link]);
[Link]().addAll(hBox, msg, calculateButton);
Scene scene = new Scene(root, 400, 300);
[Link](scene);
[Link]("Addition Client");
// show the stage
[Link]();
try
{
//attempt to create a connection to the server
connection = new Socket(remoteHost, port);// connect to remote machine
[Link]("Connection established");
// create an input stream from the server
inStream = [Link]();
inDataStream = new DataInputStream(inStream);
// create an output stream to the server
outStream = [Link]();
outDataStream = new DataOutputStream(outStream);
// send the host IP to the server
[Link]([Link]().getHostAddress());//sending data
catch (UnknownHostException e)
[Link]("Unknown host");
catch (IOException ie)
[Link]("Network Exception");
// specifiy the behaviour of the calculate button
[Link](e ->
try
// send the two integers to the server
[Link]([Link]([Link]()));
[Link]([Link]([Link]()));
// read and display the result sent back from the server
int result = [Link]();
[Link]("" + result);
catch(IOException ie)
);
private void getInfo()
Optional<String> response;
//use the TextInputDialog class to allow the user to enter the host address
TextInputDialog addressDialog = new TextInputDialog();
[Link]("Enter remote host");
[Link]("Addition Client");
response = [Link]();
remoteHost = [Link]();
//use the TextInputDialog class to allow the user to enter port number
TextInputDialog portDialog = new TextInputDialog();
[Link]("Enter port number");
[Link]("Addition Client");
response = [Link]();
port = [Link]([Link]());
public static void main(String[] args)
launch(args);
}
package network;
import [Link];
import [Link];
import [Link];
public class AdditionServerMultiple
public static void main(String[] args)
final int port = 8901;
AdditionServerThread thread;
Socket socket;
[Link]("Listening for connections on port: " + port);
try
ServerSocket listenSocket = new ServerSocket(port);
while(true) // continuously listen for connections
socket = [Link]();
thread = new AdditionServerThread(socket);
[Link]();
catch(IOException e)
}
}
package network;
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
public class AdditionServerThread extends Thread
private int id;
private static int totalConnections;
private final int port = 8901;
private final Socket connection;
private InputStream inStream;
private DataInputStream inDataStream;
// declare low level and high level objects for output
private OutputStream outStream;
DataOutputStream outDataStream;
// declare other variables
private String client;
private int first, second, sum;
private boolean connected;
public AdditionServerThread(Socket socketIn)
{
connection = socketIn;
@Override
public void run()
try
connected = true;
[Link]("Connection established");
totalConnections++; // increase the total number of connections
id = totalConnections; // assign an id
//create an input stream from the client
inStream = [Link]();
inDataStream = new DataInputStream(inStream);
//create an output stream to the client
outStream = [Link] ();
outDataStream = new DataOutputStream(outStream);
//wait for a string from the client
client = [Link]();
[Link]("Address of client: " + client);
while(connected)
//read an integer from the client
first = [Link]();
[Link]("First number received from connection " + id + ": " + first);
//read an integer from the client
second = [Link]();
[Link]("Second number received from connection " + id + ": " + second);
sum = first + second;
[Link]("Sum returned to connection " + id + ": " + sum);
// send the sum to the client
[Link](sum);
catch(IOException e)
connected = false;