Java Sockets and Socket Programming with TCP, Lecture notes of Data Communication Systems and Computer Networks

The use of Java sockets and socket programming with TCP in data communication and networks. The document covers topics such as internet transport-layer protocols, multiplexing/demultiplexing, and connection-oriented demux. It also explains how demultiplexing works and provides examples of socket programming with TCP. likely to be useful as study notes or lecture notes for university courses related to computer science, networking, or data communication. It could be associated with the typology of lecture notes and is most likely to belong to a course on computer networks or data communication. The document could be useful for both university and high school students as well as lifelong learners. The document succeeded in providing meaningful information about the title, description, related university topics, correlated university topics, rate, typology, academic course, academic year, user type, and succeeded fields.

Typology: Lecture notes

2021/2022

Uploaded on 05/11/2023

dyanabel
dyanabel 🇺🇸

4.7

(20)

287 documents

1 / 32

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
1
Data Communication & Networks
G22.2262-001
Session 10 - Main Theme
Java Sockets
Dr. Jean-Claude Franchitti
New York University
Computer Science Department
Courant Institute of Mathematical Sciences
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20

Partial preview of the text

Download Java Sockets and Socket Programming with TCP and more Lecture notes Data Communication Systems and Computer Networks in PDF only on Docsity!

Data Communication & Networks

G22.2262-

Session 10 - Main Theme

Java Sockets

Dr. Jean-Claude Franchitti

New York University

Computer Science Department

Courant Institute of Mathematical Sciences

Agenda

Internet Transport-Layer Protocols

Multiplexing / Demultiplexing

Socket Programming

Internet Transport-Layer Protocols

  • Reliable, in-order

delivery TCP

  • congestion control
  • flow control
  • connection setup
  • Unreliable, unordered

delivery: UDP

  • no-frills extension of “best-effort” IP
  • Services not available:
  • delay guarantees
  • bandwidth guarantees

application transport data link^ network physical

application transport data link^ network physical

data link^ network physical

data link^ network physical

data link^ network physical

data link^ network data linknetwork physical physical

Part II

Multiplexing / Demultiplexing

How Demultiplexing Works

  • Host receives IP datagrams
    • each datagram has source IP address, destination IP address
    • each datagram carries 1 transport-layer segment
    • each segment has source, destination port number (recall: well-known port numbers for specific applications)
  • Host uses IP addresses & port numbers to direct segment to appropriate socket

source port # dest port #

32 bits

application data (message)

other header fields

TCP/UDP segment format

Connectionless Demultiplexing

  • Create sockets with

port numbers:

DatagramSocket mySocket1 = new DatagramSocket(99111) ; DatagramSocket mySocket2 = new DatagramSocket(99222) ;

  • UDP socket identified by two-tuple:

(dest IP address, dest port number)

  • When host receives UDP

segment:

  • checks destination port number in segment
  • directs UDP segment to socket with that port number
  • IP datagrams with

different source IP

addresses and/or source

port numbers directed to

same socket

Connection-Oriented Demux

  • TCP socket identified

by 4-tuple:

  • source IP address
  • source port number
  • dest IP address
  • dest port number
  • recv host uses all four

values to direct

segment to appropriate

socket

  • Server host may support

many simultaneous TCP

sockets:

  • each socket identified by its own 4-tuple
  • Web servers have different

sockets for each

connecting client

  • non-persistent HTTP will have different socket for each request

Connection-Oriented Demux (cont.)

Client IP:B

P

client IP: A

P4 P2^ P

server IP: C

SP: 9157 DP: 80

SP: 9157 DP: 80

P5 P6^ P

D-IP:C

S-IP: A D-IP:C

S-IP: B

SP: 5775 DP: 80

D-IP:C

S-IP: B

13

Socket Programming

Socket API

  • introduced in BSD4. UNIX, 1981
  • explicitly created, used, released by apps
  • client/server paradigm
  • two types of transport service via socket API: - unreliable datagram - reliable, byte stream- oriented

Goal: learn how to build client/server application that communicate using sockets

a host-local, application-created, OS-controlled interface (a “door”) into which application process can both send and receive messages to/from another application process

socket

Socket Programming Using TCP

Socket: a door between application process and end-end-

transport protocol (UCP or TCP)

TCP service: reliable transfer of bytes from one process

to another

process

TCP with buffers, variables

socket

controlled by application developer controlled by operating system

host or server

process

TCP with buffers, variables

socket

controlled by application developer controlled by operating system

host or server

internet

Stream Jargon

  • A stream is a sequence of

characters that flow into or

out of a process

  • An input stream is attached

to some input source for

the process (e.g., keyboard

or socket)

  • An output stream is

attached to an output

source (e.g., monitor or

socket)

Socket Programming With TCP

Example client-server app:

  1. client reads line from standard input ( inFromUser stream) , sends to server via socket ( outToServer stream)

  2. server reads line from socket

  3. server converts line to uppercase, sends back to client

  4. client reads, prints modified line from socket ( inFromServer stream)

outToServer

to network from network

inFromServer

inFromUser

keyboard monitor

Process

clientSocket

streaminput

streaminput

streamoutput

socketTCP

Client process

client TCP socket

Example: Java Client (TCP)

import java.io.; import java.net.; class TCPClient {

public static void main(String argv[]) throws Exception { String sentence; String modifiedSentence;

BufferedReader inFromUser = new BufferedReader(new InputStreamReader(System.in)); Socket clientSocket = new Socket("hostname", 6789);

DataOutputStream outToServer = new DataOutputStream(clientSocket.getOutputStream());

Create input stream Create client socket, connect to server Create output stream attached to socket

Example: Java Client (TCP), cont.

BufferedReader inFromServer = new BufferedReader(new InputStreamReader(clientSocket.getInputStream())); sentence = inFromUser.readLine(); outToServer.writeBytes(sentence + '\n'); modifiedSentence = inFromServer.readLine(); System.out.println("FROM SERVER: " + modifiedSentence);

clientSocket.close(); } }

Create input stream attached to socket

Send line to server Read line from server