Escolar Documentos
Profissional Documentos
Cultura Documentos
What is a socket?
Original idea came from UNIX The network is just like a file system Read and write a stream of data to the network through socket A socket is bound to a port number so that the TCP layer can identify the correct application for the data
5 fadzlihasan@unisza.edu.my
What is a socket?
A little background
Socket provides the TCP/IP communication protocol
Introduced in 1981- UNIX BSD 4.2 Later Sun build RPC and NFS over the socket
Is supported by all OS
WinSocks for Windows 3.1 Built into Windows 95, and NT For other OS it is not new
6 fadzlihasan@unisza.edu.my
message client other ports Internet address = 138.37.94.248 Internet address = 138.37.88.249 server
Port: The destination of a message Socket: The final point for processes communication
7 fadzlihasan@unisza.edu.my
ports in TCP are also represented by a number in the range 1 65 535 Ports below 1024 are restricted to use by well-known services. For example,
Telnet (port 23) SMTP (port 25) HTTP (port 80) POP3 (port 110)
8 fadzlihasan@unisza.edu.my
Sockets
Figure A
Figure B
Socket Operations
TCP sockets can perform a variety of operations:
Establish a connection to a remote host Send data to a remote host Receive data from a remote host Close a connection
10 fadzlihasan@unisza.edu.my
Socket Operations
There is a special type of socket that provides a service that will bind to a specific port number. Normally used only in servers, this socket can perform the following operations:
Bind to a local port Accept incoming connections from remote hosts Unbind from a local port
11 fadzlihasan@unisza.edu.my
12 fadzlihasan@unisza.edu.my
Network Clients
Network clients initiate connections and control network transactions. The server fulfills the requests of the client but not the other way round. The network client speaks to the server using a network protocol. E.g an HTTP client communicates with an HTTP server using HTTP. Port numbers are used to enable clients to locate server applications. E.g. a web server uses port 80.
13 fadzlihasan@unisza.edu.my
Network Servers
The role of the network server is to bind to a specific port and to listen for new connections. Unlike the client, the server must run continually in the hope that some client will want its services. The server runs indefinitely. Normally, it is automatically started when the host computer of the server is started.
14 fadzlihasan@unisza.edu.my
Network Servers
Some servers can handle only one connection at a time, while others can handle many connections concurrently, through the use of threads. Some protocols (e.g. HTTP/1.0) normally allow only one request per connection. Others, like POP3, support a sequence of requests. Servers answer the client request by sending either a response or an error message.
15 fadzlihasan@unisza.edu.my
Socket Types
In Java, there are 4 main socket types:
ServerSocket Socket DatagramSocket MulticastSocket
16 fadzlihasan@unisza.edu.my
The Socket class should be used when writing client software. The ServerSocket class should be used when writing server software.
17 fadzlihasan@unisza.edu.my
Socket (client)
Textbook page 273
fadzlihasan@unisza.edu.my
18
Socket Class
Socket objects represent client sockets, and is a communication channel between two TCP communications ports belonging to one or two machines.
19 fadzlihasan@unisza.edu.my
Socket Class
There are several constructors for the Socket class. The easiest way to create a socket is shown below:
Socket mySocket; try { mySocket = new Socket("www.aol.com", 80); } catch (Exception e) { }
20 fadzlihasan@unisza.edu.my
21 fadzlihasan@unisza.edu.my
Socket socket; InputStreamReader isr; BufferedReader br; PrintStream ps; try { socket = new Socket("www.aol.com",80);
} catch (Exception e) { }
22 fadzlihasan@unisza.edu.my
ServerSockets (server)
Textbook page 311
fadzlihasan@unisza.edu.my 23
Server Socket
Server Socket runs on the server and listens for incoming TCP connections. Server socket is bound to a certain port on the server machine. When it is successfully bound to a port, it immediately listens to any attempt of incoming connection When a server detects an attempt of incoming connection, it negotiates the connection between client and server. This creates/opens a regular Socket which handles communication between the client and server
24 fadzlihasan@unisza.edu.my
ServerSocket Class
java.net.ServerSocket represents the server socket ServerSocket object is created on a local port and calls method accept() to listen to incoming connection accept() will block until a connection is detected. Then it returns a Socket object that handles communication with a client
25 fadzlihasan@unisza.edu.my
The easiest way to create a socket to listen at a certain port is shown below:
ServerSocket mySocket; try { mySocket = new ServerSocket(80); } catch (Exception e) { }
26 fadzlihasan@unisza.edu.my
27 fadzlihasan@unisza.edu.my
ServerSocket server; BufferedReader reader; PrintWriter writer; server = new ServerSocket(13); while (true) { Socket client = server.accept(); reader = new BufferedReader( new InputStreamReader( client.getInputStream())); writer = new PrintWriter( new OutputStreamWriter( client.getOutputStream())); }
28 fadzlihasan@unisza.edu.my
Take a break!
What are threads?
fadzlihasan@unisza.edu.my
29