Register   Login   About   Study   Enterprise   Share
AI / Internet Technology University (AITU)
Fast Login - available after registration







|

Top Links: >> 80. Technology >> Internet Technology Summit Program >> 3. Threads and Network >> 3.2. Network
Current Topic: 3.2.1. Sockets
You have a privilege to create a quiz (QnA) related to this subject and obtain creativity score...
Repetition: What is a socket?

Yes, it is an endpoint network connector. A more precise definition from Oracle: a socket is one endpoint of a two-way communication link between two programs running on the network.

There could be many programs running on the network. To identify a specific endpoint, socket bound to a port number. Think about the port number as part of socket address.

We will focus on TCP/IP sockets that connect Internet and Intranet programs. In the Client ? Server Architecture there are two types of systems:
- Server, which usually runs a set of applications and services and serves multiple clients
- Client, which usually request server for a service

Many clients can send to a server their requests for services. Server must be listening to the requests. Clients should know the address for a connection to a server. First of all it is IP address of the server. Additional part of the address is the port number. Port number can be from 0 to 65536. Usually low numbers are already taken by system programs that runs on the server, such as FTP (21), Secure Shell ? SSH (22), Telnet (23), SMTP ? Email (25), HTTP (80). To introduce new services developers choose high level numbers.

In Java a server socket is represented in the java.net package by the ServerSocket class and client socket by the Socket class.
Communication scenario goes on like this:
a) Usually our application starts with instantiation of a ServerSocket (server) listening on a specific port number.
b) Socket (client): send request to a server providing IP address and a port number
Was it clear so far?

c) ServerSocket hopefully is listening and intercepts this request.
d) In response to this request the ServerSocket creates its own internal Socket that will collaborate with the client request
e) Now the Socket object on the server side and the Socket object on the client side can provide two-directional communications by opening InputStream and OutputStream, which will transfer data in exactly the same manner as it was done for files in the Input-Output section.
f) At the end of communications a program should call the close() method of the Socket class to close the socket.
This scenario is provided as the plan in the header of a source.
/**
* Here is an example of a simple client connecting to the server via sockets.
* The connection can be used to send one command and receive one response.
* All the operation should be inside try - catch statements as any Input/Output operation.
* We create a socket object with the specified host (server name or IP address) and the specified port number.
* We create Input and Output streams connected to the socket.
* We send array of bytes to the server and receive array of bytes in response from the server.
* Then we close the streams and close the socket connection.
* Done !
**/

sockets
| Check Your Progress | Propose QnA | Have a question or comments for open discussion?

Have a suggestion? - shoot an email
Looking for something special? - Talk to me
Read: IT of the future: AI and Semantic Cloud Architecture | Fixing Education
Do you want to move from theory to practice and become a magician? Learn and work with us at Internet Technology University (ITU) - JavaSchool.com.

Technology that we offer and How this works: English | Spanish | Russian | French

Internet Technology University | JavaSchool.com | Copyrights © Since 1997 | All Rights Reserved
Patents: US10956676, US7032006, US7774751, US7966093, US8051026, US8863234
Including conversational semantic decision support systems (CSDS) and bringing us closer to The message from 2040
Privacy Policy