Technical University of Cluj-Napoca
Faculty of Machine Building
Departament: Engineering Design and Robotics
Client – Server Project
Students:
Cuc Andreea, Lacatus Iustina
Specialization: RI English Class
3rd License Year
Group: 1533e/2
Academic Year Project Coordinator,
2019 – 2020 Prof. Dr. Ing. T. ANTAL
1. What is a socket?
To connect to other machine we need a socket connection. A socket connection
means the two machines have information about each other’s network location
(IP Address) and TCP port.The java.net.Socket class represents a Socket.
If everything goes well, the server accepts the connection. After that, the server
gets a new socket bound to the same local port and also has its remote endpoint
set to the address and port of the client. It needs a new socket so that it can
continue to listen to the original socket for connection requests while tending to
the needs of the connected client.
On the client side, if the connection is accepted, a socket is successfully created
and the client can use the socket to communicate with the server.
The client and server can now communicate by writing to or reading from their
sockets.
2. How does the program work?
This program transforms minuscule letters into majuscules :
Client: Hi!
Server: HI!
The example consists of two independently running Java programs: the client
program and the server program.
The server program begins by creating a new socket object to listen on a specific
port.
try {
port = Integer.parseInt(args[0]); }
catch (NumberFormatException e) {
System.err.println("Error1"); }
}
ServerSocket serverSocket = null;
try {
serverSocket = new ServerSocket(port);
Socket newSock = null;
If the server successfully binds to its port, then the socket object is successfully
created and the server continues to the next step—accepting a connection from a
client :
newSock = serverSocket.accept();
System.out.println("accept");
When a connection is requested and successfully established, the accept() method
returns a new Socket object. It's bound to the same local port, and its remote
address and remote port are set to match the client's. The server can communicate
with the client over this new object and listen for client connection requests.
3. Run the program
The server program must be started first.
Next, run the client program. The client can run on any computer on the same
network.
When a connection is successfully made between the client and server, the
following text will be displayed on the client screen:
“Enter a string”
A text must be entered and then the answer will have the transformed letters
(lower to upper case).