0.0(0)

Tutorial

We’re going to implement TCP connection for the server side now

  • serverSocket.accept() - start accepting requests from the client

  • outputstream gets input from client

  • inputstream get input from scanner

  • Loop

  • s = string s defined slide before

  • s defined each line from server side input stream

  • check if it contains a word like bye

  • if it is bye, print out bye, flush then print the whole string then flush again

  • Then close our streams

  • All this code is in a try catch

import java.util.*;
import java.io.*;
import java.net.*;
/**
* A Simple Client
* CPSC 441
*/

public class TCPClient {

public static void main(String[] args) {

String s, tmp;
Scanner inputStream;
PrintWriter outputStream;
Scanner userinput; // Get input from user to send to server

try {
// connects to port server app listesing at port 8888 in the same
// machine
Socket socket = new Socket("localhost", 8888); // Make socket and give port

// Create necessary streams
outputStream = new PrintWriter(new DataOutputStream( //for output stream
socket.getOutputStream()));
inputStream = new Scanner(new InputStreamReader(
socket.getInputStream()));
userinput = new Scanner(System.in); // get user input by system scanner

// send/receive messages to/from server
while (true) {
System.out.println("Enter Text Message for Echo Server: ");
tmp = userinput.nextLine(); // get user input line by line

// Send user input message to server
outputStream.println(tmp); // put in output stream to send line by line to server
// Flush to make sure message is send
outputStream.flush(); // once done sending to socket, we flush so we can send the next. Here we use flush to clear the output stream

s = inputStream.nextLine(); // get next line from input stream
System.out.println(s);

// Exit if message from server is "bye"
// Should do this from both client and server side so server terminates then client terminates
if (s.equalsIgnoreCase("bye")
break;

}
inputStream.close();
outputStream.close();
}
catch (Exception e) {
System.out.println("Error: " + e.getMessage());
}
}
}
import java.io.*;
import java.net.*;
import java.util.*;

/**
* A Simple Echo Server
* CPSC 441
*/

public class TCPServer {

public static void main(String[] args) {

String s;
Scanner inputStream;
PrintWriter outputStream;
ServerSocket serverSocket;

try {
// Listen on port 8888
serverSocket = new ServerSocket(8888);

Socket socket = serverSocket.accept();
// Connected to client
outputStream = new PrintWriter(new DataOutputStream(
socket.getOutputStream()));
inputStream = new Scanner(new InputStreamReader(
socket.getInputStream()));

// Respond to messages from the client
while (true) {
s = inputStream.nextLine();
System.out.println(s);

// exit if message from client is "bye"
if (s.equalsIgnoreCase("bye")) {
outputStream.println("bye");
outputStream.flush();
break;
}

outputStream.println(s);
outputStream.flush();
}

inputStream.close();
outputStream.close();
}
catch (Exception e) {
System.out.println("Error: " + e.getMessage());
}
}
}

  • Multi threading allows us to respond to multiple clients at the same time

  • Important for handling multiple users at the same tiem

  • Make threads inside the loop for each connection

  • One socket, but accept multiple requests

  • Sever sever = new Sever(clientSocket) → Makes separate areas on the server to accept multiple requests

  • Thread thread = new Thread(server) → Handles one client

  • thread.start → starts each thread

  • Iterate through each line of the input stream

  • Defines what happens in each thread

  • Same as other code but its in the run() function

  • For server side

  • Will be called for each thread

Server side Fully implemented:

import java.io.*;
import java.net.*;
import java.util.*;

/**
* A Simple Echo Server
* CPSC 441
*/

class WorkerThread implements Runnable{
private Socket socket;
public WorkerThread(Socket socket){
this.socket = socket;
}

@Override
public void run() {
// Connected to client
System.out.println("Worker Thread started: " + Thread.currentThread().getName());
Scanner inputStream;
PrintWriter outputStream;
try {
outputStream = new PrintWriter(new DataOutputStream(
socket.getOutputStream()));
inputStream = new Scanner(new InputStreamReader(
socket.getInputStream()));
} catch (IOException e) {
throw new RuntimeException(e);
}

// Respond to messages from the client
while (true) {
String s = inputStream.nextLine();
System.out.println(s);

// exit if message from client is "bye"
if (s.equalsIgnoreCase("bye")) {
outputStream.println("bye");
outputStream.flush();
break;
}

outputStream.println(s);
outputStream.flush();
}

inputStream.close();
outputStream.close();
}
}

public class TCPServer {

public static void main(String[] args) {

String s;
ServerSocket serverSocket;

try {
// Listen on port 8888
serverSocket = new ServerSocket(8888);

while(true) {
System.out.println("Waiting for Client");
Socket socket = serverSocket.accept();
System.out.println("Client Connected");
WorkerThread workerThread = new WorkerThread(socket);
Thread thr = new Thread(workerThread);
thr.start();
}
}
catch (Exception e) {
System.out.println("Error: " + e.getMessage());
}
}
}
0.0(0)
robot