Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

OSI and TCP/IP Layers in Java Networking: Understanding Sockets, UDP, TCP, and Datagrams, Slides of Java Programming

An overview of the osi and tcp/ip layers in java networking, focusing on sockets, udp, tcp, and datagrams. It covers the structure and functions of each layer, as well as practical examples of creating servers and clients using java code.

Typology: Slides

2011/2012

Uploaded on 03/11/2012

holmger
holmger 🇸🇪

1

(1)

12 documents

1 / 2

Related documents


Partial preview of the text

Download OSI and TCP/IP Layers in Java Networking: Understanding Sockets, UDP, TCP, and Datagrams and more Slides Java Programming in PDF only on Docsity! Basic Networking in Java Calin Curescu Basic Networking in Java Calin Curescu 12 pages TDDC32 lecture 2, 2006 Basic Networking in Java Calin Curescu 2 of 12 TDDC32 lecture 2, 2006 OSI and TCP/IP Layers • ISO (International Standards Organization) has created a layered model, called the OSI (Open Systems Interconnect) • TCP/IP (Transmission Control Protocol / Internet Protocol) already developed OSI Layer Description TCP/IP Comments Application Application level protocol Presentation Encoding, Formatting Session Manages end-user comm. HTTP, FTP Transport Transparent user data, reliability, manages packets TCP, UDP Source and destination port numbers Network Packets between source & destination, routing IP Source & destination IP numbers Data Link Data between nodes Physical Bits on the medium (cable) Network frames Basic Networking in Java Calin Curescu 3 of 12 TDDC32 lecture 2, 2006 TCP & UDP • TCP – Connection based protocol • Data flows • i.e. IP Packets are ordered, FIFO – Reliable • IP Packets are retransmitted – Congestion and flow control • UDP – Connectionless protocol • Independent datagrams – Basically adding source and destination port to IP packet • May arrive in any order – Unreliable • Datagrams can be lost Basic Networking in Java Calin Curescu 4 of 12 TDDC32 lecture 2, 2006 java.net package • Sockets – One endpoint of a two-way communication link between two programs running on the network – ServerSocket, Socket classes in java.net • URL (Uniform Resource Locator) – http://ida.liu.se:80/figure.jpg (protocol:resource) – Connecting to resource – Reading & writing (using input and output streams) • Datagrams – Needed for UDP communication – TCP provides reliable flows, which are automatically transformed to InputStream and OutputStream Basic Networking in Java Calin Curescu 5 of 12 TDDC32 lecture 2, 2006 Sockets for TCP 1. On server side, a socket (ServerSocket) is bound to a port and listens for incoming client connections 2. On client side, the server address and port is known; the client creates a new socket (Socket) and tries to connect to server 3. If everything ok • On server side the server accepts the connection and creates a new socket (Socket) bound on a different port • Server needs the initial socket to continue to listen • On client side the connection attempt returns successfully 4. Client and server can communicate by using the InputStream and OutputStream provided by Socket • Think about a pipe between server and client • A “pipe” is uniquely identified by [server address, server port, client address, client port] Basic Networking in Java Calin Curescu 6 of 12 TDDC32 lecture 2, 2006 Server Example import java.net.*; import java.io.*; public class EchoServer { public static void main(String[] args) throws IOException { ServerSocket serverSocket = null; try { serverSocket = new ServerSocket(7); } catch (IOException e) { System.err.println("Could not listen on port: 7"); System.exit(1); } Socket clientSocket = null; try { clientSocket = serverSocket.accept(); } catch (IOException e) { System.err.println("Accept failed."); System.exit(1); } PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true); BufferedReader in = new BufferedReader(new InputStreamReader( clientSocket.getInputStream())); String inputLine; while ((inputLine = in.readLine()) != null) { out.println(inputLine); } out.close(); in.close(); clientSocket.close(); serverSocket.close(); } } //thread blocks until a client connects //ports under 1024 are usually reserved by OS Examples from “Java Tutorials”: http://java.sun.com/docs/books/tutorial/networking/
Docsity logo



Copyright © 2024 Ladybird Srl - Via Leonardo da Vinci 16, 10126, Torino, Italy - VAT 10816460017 - All rights reserved