Skip to content

Instantly share code, notes, and snippets.

@williamhaley
Last active December 16, 2020 20:01
Show Gist options
  • Save williamhaley/3bf9cddcb76302e30ffd58e640d4a5a9 to your computer and use it in GitHub Desktop.
Save williamhaley/3bf9cddcb76302e30ffd58e640d4a5a9 to your computer and use it in GitHub Desktop.
NCP Java Demo (Client and Server)

NCP Java Install Instructions

Install Java on your computer. Your computer must be a Windows, macOS, or Linux computer. Not a Chromebook. You may also need to reboot.

Open the terminal program on your computer. On macOS it is called "Terminal". On Windows it is called "Command Prompt". Run the java command. Then run the javac command. If you see a message like "command not found", then Java is not properly installed.

Click the Download Zip button in the upper right hand corner of this page. Once the download is complete copy all the Java files to your Desktop.

Client / Server Example

Open your computer's terminal program and use the cd command to change directory to your Desktop.

Windows

cd \Users\%username%\OneDrive\Desktop

or

cd \Users\%username%\Desktop

macOS and Linux

cd $HOME/Desktop

Compile the programs

javac Server.java
javac Client.java

Run the program

Run these programs in separate terminal windows.

java Server

(In another, separate, terminal window)

java Client
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
public class Client {
private Socket clientSocket;
private PrintWriter out;
private BufferedReader in;
public void startConnection(String ip, int port) throws Exception {
clientSocket = new Socket(ip, port);
out = new PrintWriter(clientSocket.getOutputStream(), true);
in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
}
public String sendMessage(String msg) throws Exception {
out.println(msg);
String resp = in.readLine();
return resp;
}
public void stopConnection() throws Exception {
in.close();
out.close();
clientSocket.close();
}
public static void main(String[] args) {
Client client = new Client();
try {
client.startConnection("127.0.0.1", 8000);
String response = client.sendMessage("hello server");
System.out.println(response);
} catch (Exception exception) {
System.err.println("error: " + exception.getMessage());
}
}
}
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.time.LocalDateTime;
public class Server {
private ServerSocket serverSocket;
private Socket clientSocket;
private PrintWriter out;
private BufferedReader in;
public void start(int port) throws Exception {
serverSocket = new ServerSocket(port);
new Thread(() -> {
while (true) {
try {
clientSocket = serverSocket.accept();
out = new PrintWriter(clientSocket.getOutputStream(), true);
in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
LocalDateTime now = java.time.LocalDateTime.now();
System.out.printf("message received at '%s'\n", now);
String greeting = in.readLine();
out.printf("This is a message from the server! I got your message '%s' at '%s'\n", greeting, now);
clientSocket.close();
} catch (Exception exception) {
System.err.println("error reading message from client: " + exception.getMessage());
}
}
}).start();
while (!Thread.interrupted()) {
try {
System.out.println("server is listening for messages...");
Thread.sleep(1000);
} catch (InterruptedException exception) {
System.err.println("server interrupted");
}
}
}
public void stop() throws Exception {
in.close();
out.close();
clientSocket.close();
serverSocket.close();
}
public static void main(String[] args) {
Server server = new Server();
try {
server.start(8000);
System.out.println("server is shutting down");
} catch (Exception exception) {
System.err.println("error: " + exception.getMessage());
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment