Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 21

Lecture 2 - TCP Sockets

Lecturer: Dr Recep Ulusoy


Email: rulusoy@swin.edu.au


HET715 Network Computing
Objectives
Internet and Internet Protocol (IP)
Introduction to TCP packets
To understand the concept of sockets
To learn how to send and receive data
through sockets
To implement network clients and servers
using TCP sockets
The Internet Protocol
Internet
A worldwide collection of computer networks
Uses a common set of protocols to define how the
parties will interact with each other
IP: Internet Protocol
Developed to enable different networks to
communicate with each other
Has become the basis for connecting computers
around the world together over the Internet

Data Transmission
Consists of sending/receiving streams of
zeros and ones along the network connection
Two Types of Information
Application data
The information one computer wants to send to
another
Protocol data
Describes how to reach the intended computer
Describes how to check for errors in the
transmission
IP Packets
IP breaks large chunks of data up into more
manageable packets
Each packet is delivered separately
Each packet in a larger transmission may be
sent by a different route
Packets are numbered
The recipient reassembles the data
Transmission Control Protocol
(TCP)
Internet Protocol (IP) does not notify the
sender if data is lost or garbled
This is the job of a higher level protocol
Transmission Control Protocol (TCP)
That is why the most commonly used Internet
services use TCP with IP (TCP/IP)

TCP's Job
Attempt to deliver the data
Try again if there are failures
Notify the sender whether or not the attempt
was successful
Port Numbers
One computer can offer multiple services over the
Internet
For example, both a web server program and an email server
program could reside at the same machine
When data are sent to that computer, they need to
indicate which program (service) is to receive the
data
IP uses port numbers for this
A port number is an integer between 0 and 65,535
Some of these are preallocated for certain applications (well-
known ports b/w 1-1023)
The sending program must know the port number of the receiving
program
This number is included in the transmitted data
Continued
Port Numbers
Some well known port numbers include:
HTTP 80
HTTPS 443
FTP 20-21
Telnet 23
SMTP 25
POP3 110
IMAP14
Contents of TCP Packet
The Internet address (IP) of the recipient
The port number of the recipient
Internet address (IP) of the sender
The port number of the sender

Server and Client Sockets
A socket is an object that encapsulates a
TCP/IP connection
There is a socket on both ends of a
connection (refer to Figure-2)
Continued
Server and Client Sockets
Figure-2:
Client and Server Sockets
Setting up a server process
It involves 5 steps
Step 1: Create a ServerSocket object bound
to a specified port
Syntax in Java
Continued
ServerSocket servSock = new ServerSocket(PORT);
An integer b/w 1024-65535 (eg.1234) because port
numbers up to 1023 are reserved for special services
Setting up a server process
Step 2: Put the server into a waiting state
Syntax in Java
Continued
Socket link = servSock.accept();
Setting up a server process
Step 3: Set up input and output streams
Syntax in Java
Continued
Scanner input = new Scanner(link.getInputStream());
PrintWriter output = new PrintWriter
(link.getOutputStream(), true);
Setting up a server process
Step 4: Receive and send data
Syntax in Java
Continued

//receive
String message = input.nextLine();

//send
output.println(message);


Setting up a server process
Step 5: Close connection after completing
dialogue
Syntax in Java
Continued
link.close();
Setting up a corresponding client
It involves 4 steps
Step 1: Establish a connection with the server
Syntax in Java
Continued

Socket link = new Socket(host, PORT);


Setting up a corresponding client
Step 2: Set up input and output streams
Same as in the server set up
Syntax in Java
Continued
//input stream
Scanner input = new Scanner(link.getInputStream());
//output stream
PrintWriter output = new PrintWriter
(link.getOutputStream(), true);
Setting up a corresponding client
Step 3: Send and receive data
Same as in the server set up
Syntax in Java
Continued
//send
output.println(message);
//receive
String response = input.nextLine();

Setting up a corresponding client
Step 4: Close connection after completing the
dialogue
Same as in the server set up
Syntax in Java
link.close();

You might also like