Socket Programming for TCP and UDP

Similar documents
sottotitolo Socket Programming Milano, XX mese 20XX A.A. 2016/17 Federico Reghenzani

Unix Network Programming

Chapter 6. The Transport Layer. Transport Layer 3-1

Oral. Total. Dated Sign (2) (5) (3) (2)

Tutorial on Socket Programming

Socket Programming. CSIS0234A Computer and Communication Networks. Socket Programming in C

Outline. Distributed Computing Systems. Socket Basics (1 of 2) Socket Basics (2 of 2) 3/28/2014

Outline. Distributed Computer Systems. Socket Basics An end-point for a IP network connection. Ports. Sockets and the OS. Transport Layer.

Processes communicating. Network Communication. Sockets. Addressing processes 4/15/2013

Group-A Assignment No. 6

STUDY OF SOCKET PROGRAMMING

Socket Programming TCP UDP

UNIX Sockets. Developed for the Azera Group By: Joseph D. Fournier B.Sc.E.E., M.Sc.E.E.

Socket Programming. #In the name of Allah. Computer Engineering Department Sharif University of Technology CE443- Computer Networks

Lecture 2. Outline. Layering and Protocols. Network Architecture. Layering and Protocols. Layering and Protocols. Chapter 1 - Foundation

UNIX Sockets. COS 461 Precept 1

CS 640: Computer Networking

What s an API? Do we need standardization?

Programming Internet with Socket API. Hui Chen, Ph.D. Dept. of Engineering & Computer Science Virginia State University Petersburg, VA 23806

Outline. Operating Systems. Socket Basics An end-point for a IP network connection. Ports. Network Communication. Sockets and the OS

Hybrid of client-server and P2P. Pure P2P Architecture. App-layer Protocols. Communicating Processes. Transport Service Requirements

Network Communication

Network Software Implementations

Programming with TCP/IP. Ram Dantu

CSE 124 Discussion Section Sockets Programming 10/10/17

TCP: Three-way handshake

Elementary TCP Sockets

Ports under 1024 are often considered special, and usually require special OS privileges to use.

Lecture 7. Followup. Review. Communication Interface. Socket Communication. Client-Server Model. Socket Programming January 28, 2005

Socket Programming(2/2)

CLIENT-SIDE PROGRAMMING

A Socket Example. Haris Andrianakis & Angelos Stavrou George Mason University

NETWORK PROGRAMMING. Instructor: Junaid Tariq, Lecturer, Department of Computer Science

System Programming. Sockets

Review. Preview. Closing a TCP Connection. Closing a TCP Connection. Port Numbers 11/27/2017. Packet Exchange for TCP Connection

CSE/EE 461 Lecture 14. Connections. Last Time. This Time. We began on the Transport layer. Focus How do we send information reliably?

Socket Programming 2007/03/28

CS 43: Computer Networks. 05: Socket Programming September 12-14, 2018

Introduction to Socket Programming

Department of Computer Science

Socket Programming. Dr. -Ing. Abdalkarim Awad. Informatik 7 Rechnernetze und Kommunikationssysteme

Types (Protocols) Associated functions Styles We will look at using sockets in C Java sockets are conceptually quite similar

Networked Applications: Sockets. End System: Computer on the Net

EEC-484/584 Computer Networks

Sockets 15H2. Inshik Song

The User Datagram Protocol

EECS122 Communications Networks Socket Programming. Jörn Altmann

Socket Programming. Sungkyunkwan University. Hyunseung Choo Copyright Networking Laboratory

CSC209H Lecture 9. Dan Zingaro. March 11, 2015

The BSD UNIX Socket Interface (CS 640 Lecture) Assignment 1. Interprocess Communication (IPC) Work Individually (no groups)

CSE 333 Section 8 - Client-Side Networking

Context. Distributed Systems: Sockets Programming. Alberto Bosio, Associate Professor UM Microelectronic Departement

Hyo-bong Son Computer Systems Laboratory Sungkyunkwan University

UDP CONNECT TO A SERVER

Programming Ethernet with Socket API. Hui Chen, Ph.D. Dept. of Engineering & Computer Science Virginia State University Petersburg, VA 23806

Session NM056. Programming TCP/IP with Sockets. Geoff Bryant Process software

CSMC 412. Computer Networks Prof. Ashok K Agrawala Ashok Agrawala Set 2. September 15 CMSC417 Set 2 1

Networked Applications: Sockets. Goals of Todayʼs Lecture. End System: Computer on the ʻNet. Client-server paradigm End systems Clients and servers

Network programming(i) Lenuta Alboaie

UNIT IV- SOCKETS Part A

Introduction to Socket Programming

Systems software design NETWORK COMMUNICATIONS & RPC SYSTEMS

Announcements. CS 5565 Network Architecture and Protocols. Queuing. Demultiplexing. Demultiplexing Issues (1) Demultiplexing Issues (2)

SOCKET PROGRAMMING. What is a socket? Using sockets Types (Protocols) Associated functions Styles

Programming Ethernet with Socket API. Hui Chen, Ph.D. Dept. of Engineering & Computer Science Virginia State University Petersburg, VA 23806

ELEC / COMP 177 Fall Some slides from Kurose and Ross, Computer Networking, 5 th Edition

Christian Tschudin (basierend auf einem Foliensatz von C. Jelger und T. Meyer) Departement Mathematik und Informatik, Universität Basel

A. Basic Function Calls for Network Communications

MSc Integrated Electronics Networks Assignment. Investigation of TCP/IP Sockets and Ports. Gavin Cameron

Network Socket Programming - 3 BUPT/QMUL

Project 3. Reliable Data Transfer over UDP. NTU CSIE Computer Networks 2011 Spring

Network Socket Programming - 3 BUPT/QMUL

ICT 6544 Distributed Systems Lecture 5

How do we Communicate? Introduction to Unix Network Programming. What does Alice do? What does Bob do? Two simplest networking programs

Computer Network Lab, SS Fachgebiet Technische Informatik, Joachim Zumbrägel. Overview. Sockets. Sockets in C.

CS118 Discussion 1B, Week 1. Taqi Raza BUNCHE 1209B, Fridays 12:00pm to 1:50pm

Sockets. Dong-kun Shin Embedded Software Laboratory Sungkyunkwan University Embedded Software Lab.

Computer Networks Prof. Ashok K. Agrawala

Topics for this Week

A Client-Server Exchange

CS4514 (C04) HELP Session 1 Introduction to Network Programming (v1.3)

Overview. Administrative. * HW# 5 Due next week. * HW# 5 : Any Questions. Topics. * Client Server Communication. * 12.

CS321: Computer Networks Socket Programming

Sockets. UNIX-style IPC. Silberschatz, Galvin and Gagne 2005 Msc. Ivan A. Escobar Broitman 2007

CS4700/CS5700 Fundamentals of Computer Networking

Lecture 24. Thursday, November 19 CS 375 UNIX System Programming - Lecture 24 1

CS 3516: Computer Networks

Network Programming in C: The Berkeley Sockets API. Networked Systems 3 Laboratory Sessions

The Berkeley Sockets API. Networked Systems Architecture 3 Lecture 4

Outline. Option Types. Socket Options SWE 545. Socket Options. Out-of-Band Data. Advanced Socket. Many socket options are Boolean flags

The TCP Protocol Stack

CS321: Computer Networks Introduction to Application Layer

Sockets. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

Ken French HELP Session 1 CS4514

Lecture 5 Overview! Last Lecture! This Lecture! Next Lecture! I/O multiplexing! Source: Chapter 6 of Stevens book!

Overview. Exercise 0: Implementing a Client. Setup and Preparation

Network Programming in C. Networked Systems 3 Laboratory Sessions and Problem Sets

Lecture 3 Overview! Last Lecture! TCP/UDP and Sockets introduction!

Simple network applications using sockets (BSD and WinSock) Revision 1 Copyright Clifford Slocombe

CPSC 213. Introduction to Computer Systems. Inter-Process Communication. Unit 2f

Transcription:

CSCI4430 Data Communication and Computer Networks Socket Programming for TCP and UDP ZHANG, Mi Jan. 19, 2017

Outline Socket Programming for TCP Introduction What is TCP What is socket TCP socket programming Network byte ordering Socket Programming for UDP Introduction UDP Socket Programming 2

What is TCP? A connection-oriented transport layer protocol How to transfer data via TCP connection Create a connection (port to port connection) Transfer data Reliable delivery of data 3

What is Socket? Just something which will make sending/receiving data through network feel like reading/writing files. How to use? Create a socket Bind the socket to a connection Send/receive data Close 4

What is Socket? Network programming for TCP 5

TCP Socket Programming Create a socket(server & Client) Syntax: int socket(int family, int type, int protocol); It is like open() function for file operations, the return value is a socket descriptor Example: 6

TCP Socket Programming Create a socket(server & Client) Family: protocol family AF_INET: ipv4 protocols AF_INET6: ipv6 protocols Type: SOCK_STREAM: stream socket (for TCP) SOCK_DGRAM: datagram socket (for UDP) SOCK_PACKET: raw socket for Linux Protocol: set to 0 except raw socket. Return value: non-negative integer for success and -1 for failures. 7

TCP Socket Programming bind() (Server) Bind the socket to a port in the local machine Syntax int bind(int socket, struct sockaddr* addr, int addr_len); Example: 8

TCP Socket Programming bind() (Server) struct sockaddr defines a socket address INADDR_ANY the socket accept connections to ANY IP addresses of this host. 9

TCP Socket Programming listen() (Server) Defines the maximum number of connections can be pending to this socket Syntax: int listen (int socket, int backlog); Example: 10

TCP Socket Programming accept() (Server) Returns socket descriptors for new connection Syntax int accept(int socket, struct sockaddr *addr, int addr_len); Example 11

TCP Socket Programming accept() (Server) The received value is a socket descriptor You may find that there are 2 socket descriptors, sd and client_sd sd is for listening to a port and accepting connections from clients The descriptor returned by accept() is used for transferring data 12

TCP Socket Programming connect() (Meanwhile on the client side ) Connect a TCP client to a TCP server Syntax: int connect(int socket, struct sockaddr *addr, int *addr_len); Example: 13

TCP Socket Programming gethostbyname() convert the hostname into an IP address 14

TCP Socket Programming send()/recv() (Server & Client) Send and receive data Syntax: int send (int socket, const void* msg, size_t msg_len, int flags); int recv (int socket, void* buff, size_t msg_len, int flags); Example: 15

TCP Socket Programming send()/recv() (Server & Client) Setting flags to 0 will be OK for most cases Incomplete recv()/send(), due to the limit of the buffer size, the recv()/send() functions may input/output fewer bytes than requested. Always check the return value, it indicate the bytes sent/received. Use a loop to send/receive 16

Blocking Operations If the socket is blocking, the recv() is a blocking function. It will block the program and wait for data from the other side. Another blocking function is accept() How to deal? Multi-thread programming, we will cover this in the next tutorial. 17

Network-byte Order We know that an integer is stored in 4 bytes. Suppose an integer 0x12345678 is stored in memory. We know the 4-bytes occupied are storing the following data: 0x12, 0x34, 0x56, 0x78 But how are they placed in memory? 18

Network-byte Order How do machines interpret unsigned integers? If we simply transfer the 0-1 sequence, we deliver the WRONG message!! How to PREVENT this? Low memory address 0x12 0x34 0x56 0x78 What SPARC Machine (Big Endian) sees is: 0x12345678 What X86 Machine (Little Endian) sees is: 0x78563412 High memory address 19

Why Network Byte Ordering? Different machines may have different orderings (same bit-stream means different things for different machines) Big-endian: Sparc Little-endian: x86 SPARC1 Big-endian 0x12345678 Directly transfer through network Same 0-1 sequence but different meaning for different machines LINUX1 Little-endian 0x78563412 Low memory address 0x12 0x34 0x56 0x78 High memory address We regulate that: Integers TRANSFERRED IN NETWORK should use Big Endian!! 20

Why Network Byte Ordering? Different machines may have different orderings (same bit-stream means different things for different machines) Big-endian: Sparc little-endian: x86 SPARC1 Big-endian 0x12345678 htonl() Big-endian 0x12345678 0x12 0x34 0x56 0x78 Transfer through network LINUX1 Little-endian 0x12345678 ntohl() Big-endian 0x12345678 0x78 0x56 0x34 0x12 21

Network-byte Order We use a consistent format when transferring data through the network. Network-byte order (big-endian) How to transfer to network-byte order? uint32_t htonl(uint32_t int_type_variable); uint16_t htons(uint16_t short_type_variable); 22

Data Transmission Use network byte ordering for transferring data of type int and unsigned short Values to note: IP address Port number Argument length htonl(), htons(), ntohl(), ntohl() 23

Network-byte Order Another way to change an IP address into network-byte order Syntax: in_addr_t inet_addr (const char *cp); Example: 24

Compile Commands Compilation on Linux Different on Solaris 25

Outline Socket Programming for TCP Introduction What is TCP What is socket TCP socket programming Network byte ordering Socket Programming for UDP Introduction UDP Socket Programming 26

UDP (User Datagram Protocol) Another transport layer protocol Difference from TCP: TCP guarantees reliable data transmission by establishing connection between client and server. UDP does not establish connection between server and client. It is a connection-less protocol. UDP, thus, does not guarantee the arrival, arrival time and content of the message. 27

UDP Socket Programming Overview UDP Server UDP Client socket() socket() Blocking function bind() recvfrom() sendto() sendto() recvfrom() close() close() 28

UDP Socket Programming socket() Syntax: int socket(int family, int type, int protocol); To create a UDP socket: int sd = socket(af_inet, SOCK_DGRAM, 0); 29

UDP Socket Programming sendto() and recvfrom() Syntax: sendto(int sd, void* buf, int buflen, int flags, struct sockaddr* saddr, int addrlen); recvfrom(int sd, void* buf, int buflen, int flags, struct sockaddr* saddr, int* addrlen); Example: 30

UDP Socket Programming sendto() and recvfrom() Return value: how many bytes are successfully sent. The return value may not be equal to the argument buflen. Use a while loop to send and receive data. recvfrom() Similar to recv(), recvfrom() is blocking and waits for data from the other side. The struct sockaddr* saddr argument After executing recvfrom(), the address of the sender is thus stored in *saddr. We can then send the data using the address *saddr. 31

Thank you! 32