A socket is the one end –point of two-way communication link between two programs running over the network. This means the programs run on different computers, usually referred as the local and the remote computer. However, one can run the two programs on the same computer. Such communicating programs constitute a client/server application normally; a server runs on a specific computer and has a socket that is bound to a specific port number. The server just waits, listening to the socket for a client to make a connection request (Polam, 2014).
On the client- side, the client knows the hostname of the machine on which the server is ruing and the port number on which the server is listening. To make a connection request, the client tries to rendezvous with the server on the on the server’s machines and port. The client also needs to identify itself to the server so that it binds to a local port number that it will use during this connection. *The communication with each client is done via the sockets created for each communication. SERVER CLIENT ,--------------. ,--------------. | ,-| | | | accept | || | read/write | | `-| |-' | `--------------' `--------------' *“A socket” is an abstraction of IP Port. …show more content…
First appeared in early 1970’systems, There are two kinds of sockets:
1, the connection oriented socket based on TCP
2, connectionless sockets based on user Datagram Protocol (UDP).
*The TCP type sockets guarantee data arrives in the correct order, but the user Datagram protocol (UDP) does not.
One of the main differences between (” socket” and “server socket “) is;
* Socket implemented at the client side to send request to the port of the machine where the server socket is listing. But
*server socket implemented at the server side so that it can listen to the client’s and respond to them,
When we look at this two differently, the socket class placed on the client’s side, which is send request to the server side socket and waits for the response from the server. While the server socket placed in server side, which is sends requests to clients’ side socket and waits for response from client.
In censes, we need to make an object of socket class for networking application. While for server side, networking application server socket class is used. A method named server socket_ object. Accepts [] is used by server to listen to clients at a specific port addresses. So in general summary the socket completely based on the client’s side while the server socket based on the server side. *Opening a socket * When programming a client, a socket must be opened like this: When programming a client, a socket must be opened like below: Socket MyClient; MyClient = new Socket("MachineName", PortNumber); This code, however, must be put in a try/catch block to catch the IOException: Socket MyClient; try { MyClient = new Socket("MachineName", PortNumber); } catch (IOException e) { System.out.println(e); } Where • MachineName is the machine name to open a connection to and • PortNumber is the port number on which the server to connect to is listening. When selecting a port number, one has to keep in mind that the port numbers in the range from 0 to 1023 are reserved for standard services, such as email, FTP, HTTP, etc. For our service (the chat server) the port number should be chosen greater than 1023. The server side When programming a server, a server socket must be created first, like below: ServerSocket MyService; try { MyServerice = new ServerSocket(PortNumber); } catch (IOException e) { System.out.println(e); } The server socket is dedicated to listen to and accept connections from clients. After accepting a request from a client the server creates a client socket to communicate (to send/receive data) with the client, like below : Socket clientSocket = null; try { serviceSocket = MyService.accept(); } catch (IOException e) { System.out.println(e); } Now the server can send/receive data to/from the clients. Since the sockets are like the file descriptors the send/receive operations are implemented like read/write file operations on the input/output streams. Creating an input stream On the client side, you can use the DataInputStream class to create an input stream to