CS1103 Discussion Assignment Unit 6 - The differences between handling Socket and ServerSocket

Questions

Please discuss the differences between handling Socket and ServerSocket.

1. Definitions

Before I go into the differences between handling Socket and ServerSocket, it would be helpful to explain the definitions of client, server, Socket, and ServerSocket.
The definitions of client, server, Socket, and ServerSocket
As shown above, a client is a program that connects to a server by socket, and a server is a program that listens to the client socket and responds to the client (Eck, 2019). In Java, two classes help the communication between client and server: Socket and ServerSocket. Specifically, on the client-side, one can use the constructor Socket(String host, int port) of the Socket class to create a Socket object, and then use this object to call the getInputStream() or getOutputStream() to receive or send data; on the server-side, one can use the ServerSocket(int port) constructor of the ServerSocket class to create a ServerSocket object, and then call the accept() method to obtain the socket from the client. With this socket, the server can call getInputStream() or getOutputStream() to process data from the client or send data to the client.

2. Differences

The difference between handling Socket and ServerSocket is that: Socket is a class usually used on the client-side. One can communicate with the server by creating a Socket object and calling its getInputStream() or getOutputStream(); ServerSocket is a class used on the server-side. A person can use this class to create a ServerSocket object, treat it as a listener to receive socket from clients, and then process the received data and respond to the client. For example, the following code details handling Socket and ServerSocket.

Codes
The client program:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.Scanner;

/**
* A client proogram
*/
public class Client {
public static void main(String[] args) {

String host;
int port;
Socket connection;

/**
* Enter the IP address and port
*/
Scanner in = new Scanner(System.in);
System.out.print("Please enter the IP address: ");
host = in.nextLine();
System.out.print("Please enter the port: ");
port = in.nextInt();

/**
* Send data to the server and print the data from the server
*/
try {
connection = new Socket(host, port);

// Send data to the server
PrintWriter dataToServer = new PrintWriter(connection.getOutputStream());
dataToServer.println("Hi, I am a message from the client.");
dataToServer.flush(); //Make sure the data is sent to the server

// Receive data from the server
BufferedReader dataFromServer = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String lineDataFromServer = dataFromServer.readLine(); //data from the server
if (lineDataFromServer == null) {
throw new IOException("Connection was opened, " + "but the server did not send any data.");
}
System.out.println(lineDataFromServer);

dataToServer.close();
dataFromServer.close();

} catch (Exception e) {
e.printStackTrace();
}
}
}

The server program:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;

/**
* A server program
*/
public class Server {
public static final int LISTENING_PORT = 8189;

public static void main(String[] args) {
ServerSocket listener;
Socket connection;

try {
listener = new ServerSocket(LISTENING_PORT);

while (true) {
connection = listener.accept();

// Receive data from the client and print it
BufferedReader dataFromClient = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String lineDataFromServer = dataFromClient.readLine(); //data from the client
if (lineDataFromServer == null) {
throw new IOException("Connection was opened, " + "but the client did not send any data.");
}
System.out.println(lineDataFromServer);

// Send message to the client
PrintWriter dataToClient = new PrintWriter(connection.getOutputStream());
dataToClient.println("Hi, I am a message from the server.");
dataToClient.flush(); //Make sure the data is sent to the client

dataFromClient.close();
dataToClient.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}

Outputs
The client program’s outputs:

1
2
3
4
5
Please enter the IP address: localhost
Please enter the port: 8189
Hi, I am a message from the server.

Process finished with exit code 0

The server program’s outputs:

1
2
3
Hi, I am a message from the client.

Process finished with exit code 130

In the above code, the Client class creates a Socket object, calls the getOutputStream() method to send data to the server, and calls the getInputStream() method to receive the data from the server and print it out. Similarly, a ServerSocket object is created to receive the client’s socket in the Server class. Then, call the getOutputStream() method to send data to the client, and call the getInputStream() method to receive the data from the client and print it out.

Word count: 705

References

1.Eck, D. J. (2019). Introduction to Programming Using Java.
http://math.hws.edu/javanotes

2.Horstmann, C. S. 2019. Core Java Volume I–Fundamentals (11th ed.). Pearson.

​


This is the ending, thanks for reading.