Introduction to Sockets “A socket is one endpoint of a two-way communication link between two...
-
Upload
primrose-powers -
Category
Documents
-
view
214 -
download
0
Transcript of Introduction to Sockets “A socket is one endpoint of a two-way communication link between two...
![Page 1: Introduction to Sockets “A socket is one endpoint of a two-way communication link between two programs running on the network. A socket is bound to a port.](https://reader036.fdocuments.in/reader036/viewer/2022082711/56649f115503460f94c24cc0/html5/thumbnails/1.jpg)
Introduction to Sockets
• “A socket is one endpoint of a two-way communication link between two programs running on the network. A socket is bound to a port number so that the TCP layer can identify the application to which data is destined to be sent.”
![Page 2: Introduction to Sockets “A socket is one endpoint of a two-way communication link between two programs running on the network. A socket is bound to a port.](https://reader036.fdocuments.in/reader036/viewer/2022082711/56649f115503460f94c24cc0/html5/thumbnails/2.jpg)
![Page 3: Introduction to Sockets “A socket is one endpoint of a two-way communication link between two programs running on the network. A socket is bound to a port.](https://reader036.fdocuments.in/reader036/viewer/2022082711/56649f115503460f94c24cc0/html5/thumbnails/3.jpg)
Networking Review
• TCP/IP Protocol Suite• Application
programming interface to TCP/IP (API)
• BSD Unix• Java.net.Socket• Platform
Independence
![Page 4: Introduction to Sockets “A socket is one endpoint of a two-way communication link between two programs running on the network. A socket is bound to a port.](https://reader036.fdocuments.in/reader036/viewer/2022082711/56649f115503460f94c24cc0/html5/thumbnails/4.jpg)
![Page 5: Introduction to Sockets “A socket is one endpoint of a two-way communication link between two programs running on the network. A socket is bound to a port.](https://reader036.fdocuments.in/reader036/viewer/2022082711/56649f115503460f94c24cc0/html5/thumbnails/5.jpg)
Ports
• TCP and UDP use ports to map incoming data to a particular process running on the computer
• Ports are 16 bit numbers
• Need IP Address (32-bits) and port.
• 0-1023 Restricted out of 65,535.
![Page 6: Introduction to Sockets “A socket is one endpoint of a two-way communication link between two programs running on the network. A socket is bound to a port.](https://reader036.fdocuments.in/reader036/viewer/2022082711/56649f115503460f94c24cc0/html5/thumbnails/6.jpg)
Networking Classes in Java
• TCP: URL, URLConnection, Socket, ServerSocket
• UDP: DatagramPacket, DatagramSocket, MulticastSocket
• Socket and ServerSocket form two ends of communicaiton link.
![Page 7: Introduction to Sockets “A socket is one endpoint of a two-way communication link between two programs running on the network. A socket is bound to a port.](https://reader036.fdocuments.in/reader036/viewer/2022082711/56649f115503460f94c24cc0/html5/thumbnails/7.jpg)
import java.io.*;import java.net.*;
public class EchoClient { public static void main(String[] args) throws IOException {
Socket echoSocket = null; PrintWriter out = null; BufferedReader in = null;
try { echoSocket = new Socket("campione", 7); out = new PrintWriter(echoSocket.getOutputStream(), true); in = new BufferedReader(newInputStreamReader(echoSocket.getInputStream())); } catch (UnknownHostException e) { System.err.println("Don't know about host: taranis."); System.exit(1); } catch (IOException e) { System.err.println("Couldn't get I/O for the connection to:taranis."); System.exit(1); }
BufferedReader stdIn = new BufferedReader(newInputStreamReader(System.in));
String userInput;
while ((userInput = stdIn.readLine()) != null) { out.println(userInput); System.out.println("echo: " + in.readLine());}
out.close();in.close();stdIn.close();echoSocket.close();
}}
![Page 8: Introduction to Sockets “A socket is one endpoint of a two-way communication link between two programs running on the network. A socket is bound to a port.](https://reader036.fdocuments.in/reader036/viewer/2022082711/56649f115503460f94c24cc0/html5/thumbnails/8.jpg)
Procedure
• Open a socket
• Open an input and output stream to the socket
• Read from and write to the stream according to the server’s protocol
• close streams
• close socket