21
Lecture 2 - TCP Sockets Lecturer: Dr Recep Ulusoy Email: [email protected] HET715 Network Computing

Lecture 3- TCP Sockets

Embed Size (px)

Citation preview

Page 1: Lecture 3- TCP Sockets

Lecture 2 - TCP Sockets

Lecturer: Dr Recep Ulusoy Email: [email protected]

HET715 Network Computing

Page 2: Lecture 3- TCP Sockets

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

Page 3: Lecture 3- 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

Page 4: Lecture 3- TCP Sockets

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

Page 5: Lecture 3- TCP Sockets

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

Page 6: Lecture 3- TCP Sockets

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)

Page 7: Lecture 3- TCP Sockets

TCP's Job

• Attempt to deliver the data

• Try again if there are failures

• Notify the sender whether or not the attempt was successful

Page 8: Lecture 3- TCP Sockets

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

Page 9: Lecture 3- TCP Sockets

Port Numbers

• Some well known port numbers include: HTTP 80 HTTPS 443 FTP 20-21 Telnet 23 SMTP 25 POP3 110 IMAP14

Page 10: Lecture 3- TCP Sockets

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

Page 11: Lecture 3- TCP Sockets

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

Page 12: Lecture 3- TCP Sockets

Server and Client Sockets

Figure-2:Client and Server Sockets

Page 13: Lecture 3- TCP 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

Page 14: Lecture 3- TCP Sockets

Setting up a server process

• Step 2: Put the server into a waiting state

• Syntax in Java

Continued

Socket link = servSock.accept();

Page 15: Lecture 3- TCP Sockets

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);

Page 16: Lecture 3- TCP Sockets

Setting up a server process

• Step 4: Receive and send data

• Syntax in Java

Continued

//receive

String message = input.nextLine();

//sendoutput.println(message);

Page 17: Lecture 3- TCP Sockets

Setting up a server process

• Step 5: Close connection after completing dialogue

• Syntax in Java

Continued

link.close();

Page 18: Lecture 3- TCP Sockets

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);

Page 19: Lecture 3- TCP Sockets

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);

Page 20: Lecture 3- TCP Sockets

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();

Page 21: Lecture 3- TCP Sockets

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();