Introduction to Socket

Slides:



Advertisements
Similar presentations
Sockets: Network IPC Internet Socket UNIX Domain Socket.
Advertisements

Programming with UDP – I Covered Subjects: IPv4 Socket Address Structure Byte Ordering Functions Address Access/Conversion Functions Functions: 1.socket()
Today’s topic: Basic TCP API –Socket –Bind –Listen –Connect –Accept –Read –Write –Close.
Sockets Programming CS144 Review Session 1 April 4, 2008 Ben Nham.
Networks: TCP/IP Socket Calls1 Elementary TCP Sockets Chapter 4 UNIX Network Programming Vol. 1, Second Ed. Stevens.
Quick Overview. 2 ISO/OSI Reference Model Application Application Presentation Presentation Session Session Transport Transport Network Network Data Link.
Socket Programming: a Primer Socket to me!. Feb. 23, 2001EE122, UCB2 Why does one need sockets? application network protocol sockets network.
Network Programming UNIX Internet Socket API. Everything in Unix is a File –When Unix programs do any sort of I/O, they do it by reading or writing to.
תקשורת באינטרנט Tutorial 8. 2 n Socket programming u What is socket ? u Sockets architecture u Types of Sockets u The Socket system calls u Data Transfer.
Tutorial 8 Socket Programming
Socket Addresses. Domains Internet domains –familiar with these Unix domains –for processes communicating on the same hosts –not sure of widespread use.
Winsock programming.  TCP/IP UDP TCP  Winsock #include wsock32.lib.
Lecture 10 Overview. Network API Application Programming Interface – Services that provide the interface between application and protocol software often.
1 Tutorial on Socket Programming Computer Networks - CSC 458 Department of Computer Science Yukun Zhu (Slides are mainly from Monia Ghobadi, and Amin Tootoonchian,
Basic Socket Programming TCP/IP overview. TCP interface Reference: –UNIX Network Programming, by Richard Stevens. –UNIX man page.
CPSC 441 TUTORIAL – JANUARY 18, 2012 TA: MARYAM ELAHI INTRODUCTION TO SOCKET PROGRAMMING WITH C.
1 Programming with TCP/IP Ram Dantu. 2 Client Server Computing r Although the Internet provides a basic communication service, the protocol software cannot.
UNIX Socket Programming CS 6378
TCP Socket Programming. r An abstract interface provided to the application programmer  File descriptor, allows apps to read/write to the network r Allows.
ECE 4110 – Internetwork Programming Client-Server Model.
Sockets and intro to IO multiplexing. Goals We are going to study sockets programming as means to introduce IO multiplexing problem. We will revisit socket.
Operating Systems Chapter 9 Distributed Communication.
Socket Programming. Introduction Sockets are a protocol independent method of creating a connection between processes. Sockets can be either – Connection.
Zhu Reference: Daniel Spangenberger Computer Networks, Fall 2007 PPT-4 Socket Programming.
CS345 Operating Systems Φροντιστήριο Άσκησης 2. Inter-process communication Exchange data among processes Methods –Signal –Pipe –Sockets.
Sockets CIS 370 Lab 10 UMass Dartmouth. Introduction 4 Sockets provide a simple programming interface which is consistent for processes on the same machine.
Sirak Kaewjamnong Computer Network Systems
Server Sockets: A server socket listens on a given port Many different clients may be connecting to that port Ideally, you would like a separate file descriptor.
McGraw-Hill©The McGraw-Hill Companies, Inc., 2000 Chapter 16 Socket Interface
 Wind River Systems, Inc Chapter - 13 Network Programming.
McGraw-Hill©The McGraw-Hill Companies, Inc., 2000 Chapter 16 Socket Interface.
Remote Shell CS230 Project #4 Assigned : Due date :
Technical Details for sockaddr_in Structure Department of Computer Science Southern Illinois University Edwardsville Fall, 2015 Dr. Hiroshi Fujinoki
Networking Tutorial Special Interest Group for Software Engineering Luke Rajlich.
The Sockets Library and Concepts Rudra Dutta CSC Spring 2007, Section 001.
TELE202 Lecture 15 Socket programming 1 Lecturer Dr Z. Huang Overview ¥Last Lecture »TCP/UDP (2) »Source: chapter 17 ¥This Lecture »Socket programming.
University of Calgary – CPSC 441.  A socket is an interface between the application and the network (the lower levels of the protocol stack)  The application.
Socket Programming Tutorial Department of Computer Science Southern Illinois University Edwardsville Fall, 2015 Dr. Hiroshi Fujinoki
Socket Programming Lab 1 1CS Computer Networks.
1 Sockets Programming Socket to me!. 2 Network Application Programming Interface (API) The services provided by the operating system that provide the.
2: Application Layer1 Chapter 2: Application layer r 2.1 Principles of network applications r 2.2 Web and HTTP r 2.3 FTP r 2.4 Electronic Mail  SMTP,
1 CS716 Advanced Computer Networks By A. Wahid Shaikh.
S OCKET P ROGRAMMING IN C Professor: Dr. Shu-Ching Chen TA: HsinYu Ha.
Socket address structures Byte ordering IP address conversion
Introduction to Sockets
UNIX Internet Socket API
S OCKET P ROGRAMMING IN C Professor: Dr. Shu-Ching Chen TA: Hsin-Yu Ha.
Read() recv() connection establishment Server (connection-oriented protocol) blocks until connection from client Client socket() bind() listen() accept()
2: Application Layer 1 Socket Programming UNIX Network Programming, Socket Programming Tutorial:
CSCI 330 UNIX and Network Programming Unit XIV: User Datagram Protocol.
Socket Programming. Computer Science, FSU2 Interprocess Communication Within a single system – Pipes, FIFOs – Message Queues – Semaphores, Shared Memory.
©The McGraw-Hill Companies, Inc., 2000© Adapted for use at JMU by Mohamed Aboutabl, 2003Mohamed Aboutabl1 1 Chapter 16 Socket Interface.
1 Spring Semester 2008, Dept. of Computer Science, Technion Internet Networking recitation #7 Socket Programming.
Lecture 15 Socket Programming CPE 401 / 601 Computer Network Systems slides are modified from Dave Hollinger.
Lecture 3 TCP and UDP Sockets CPE 401 / 601 Computer Network Systems slides are modified from Dave Hollinger.
1 Socket Interface. 2 Client-Server Architecture The client is the one who speaks first Typical client-server situations  Client and server on the same.
Socket Programming(1/2). Outline  1. Introduction to Network Programming  2. Network Architecture – Client/Server Model  3. TCP Socket Programming.
1 Socket Interface. 2 Basic Sockets API Review Socket Library TCPUDP IP EthernetPPP ARP DHCP, Mail, WWW, TELNET, FTP... Network cardCom Layer 4 / Transport.
Network Programming CSC- 341
Socket Programming in C
Network Programming with Sockets
Socket Interface 1 Introduction 11 Socket address formats 2 API 12 13
Transport layer API: Socket Programming
Socket Programming in C
Chapter 16 Socket Interface.
Socket Programming.
Sockets Programming Socket to me!.
Sockets Programming Socket to me!.
Internet Networking recitation #8
Sockets.
Presentation transcript:

Introduction to Socket Programming

CONTENTS SOCKETS SOCKET ADDRESS SOCKET SYSTEM CALLS CONNECTIONLESS ITERATIVE SERVER UDP CLIENT-SERVER PROGRAMS CONNECTION-ORIENTED CONCURRENT SERVER TCP CLIENT-SERVER PROGRAMS

CONTENTS (continued) BYTE ORDERING ADDRESS TRANSFORMATION BYTE MANIPULATION FUNCTIONS INFORMATION ABOUT REMOTE HOST

SOCKETS

What is socket Unix programs do any sort of I/O by reading or writing to a file descriptor. This logical file can be a network connection, a FIFO, a pipe, a terminal, a real on-the-disk file, or just about anything else. When a program wants to communicate with another program over the Internet, also do it through a file descriptor-- socket descriptor.

Declaration for socket function

Socket types

SOCKETS ADDRESS

socket address structure // General socket address structure struct sockaddr { unsigned short sa_family; // address family, AF_xxx char sa_data[14]; // 14 bytes of protocol address }; OR // Internet address: AF_INET struct sockaddr_in { short sin_family; // 2 bytes u_short sin_port; // 2 bytes struct in_addr sin_addr; // 4 bytes char sin_zero; // 8 bytes, unused, set to 0 }

Internet address structure struct in_addr { unsigned long s_addr; // that's a 32-bit long, or 4 bytes }; OR struct in_addr { union { struct { u_char s_b1, s_b2, s_b3,s_b4; }; struct { u_short s_w1, s_w2; }; u_long S_addr; } S_un; } #define s_addr S_un.S_addr

Internet address structure struct sockaddr_in my_addr; my_addr.sin_addr.s_addr = INADDR_ANY; // use my IP address By setting my_addr.sin_addr.s_addr to INADDR_ANY, you are telling it to automatically fill in the IP address of the machine the process is running on.

SOCKET SYSTEM CALLS

Declaration for bind function sockfd is the socket file descriptor returned by socket(). localaddr is a pointer to a struct sockaddr that contains information about your address, namely, port and IP address. localaddrlen can be set to sizeof(struct sockaddr). By setting localaddr.sin_port to zero, you are telling bind() to choose the port for you.

IANA Port Numbers Ranges The Internet Assigned Numbers Authority(IANA) http://www.iana.org/assignments/port-numbers

Declaration for listen function sockfd is the usual socket file descriptor from the socket() system call. backlog is the number of connections allowed on the incoming queue.

Declaration for connect function sockfd is the socket file descriptor, as returned by the socket() call. But can be used to communicate with server after successfully called connect(). serveraddr is a struct sockaddr containing the destination port and IP address. serveraddrlen can be set to sizeof(struct sockaddr).

Declaration for accept function sockfd is the listen()ing socket descriptor. Easy enough. clientaddr will usually be a pointer to a local struct sockaddr_in. This is where the information about the incoming connection will go (and with it you can determine which host is calling you from which port). clientaddrlen is a local integer variable that should be set to sizeof(struct sockaddr_in) before its address is passed to accept().

Declaration for close function

Declaration for write function Used in connection-oriented (TCP) program. sockfd is the socket descriptor you want to send data to (whether it's the one returned by socket() or the one you got with accept().) buf is a pointer to the data you want to send, and buflen is the length of that data in bytes. If the value returned by write() doesn't match the value in buflen, it's up to you to send the rest of the string. Ssize_t send(int sockfd, const void *buf, int buflen, int flags); Just set flags to 0. Set flag=MSG_OOB to send out-of-band data.

Declaration for read function Used in connection-oriented (TCP) program. A similar system call ssize_t recv(int sockfd, void *buf, int len, unsigned int flags); sockfd is the socket descriptor to read from, buf is the buffer to read the information into, buflen is the maximum length of the buffer, flags can again be set to 0. Set flag=MSG_OOB to read out-of-band data.

Declaration for sendto function Used in connection-less (UDP) program. toaddr is a pointer to a struct sockaddr (or struct sockaddr_in which contains the destination IP address and port). toaddrlen can simply be set to sizeof(struct sockaddr).

Declaration for recvfrom function Used in connection-less (UDP) program. fromaddr is a pointer to a struct sockaddr (or struct sockaddr_in which contains the IP address and port of the sender). fromaddrlen can simply be set to sizeof(struct sockaddr).

CONNECTIONLESS ITERATIVE SERVER & UDP CLIENT-SERVER PROGRAMS

Socket interface for connectionless iterative server

CONNECTION-ORIENTED CONCURRENT SERVER

for connection-oriented Socket interface for connection-oriented concurrent server

Part I

Part II

Part I Client and Server

Part II

BYTE ORDERING

Big-endian byte order

Little-endian byte order

The byte order for the TCP/IP protocol suite is big endian.

Bite-order transformation

Declarations for byte-order transformation Examples: my_addr.sin_port = htons(0); // choose an unused port at random my_addr.sin_addr.s_addr = htonl(INADDR_ANY); // use my IP address

ADDRESS TRANSFORMATION

Address transformation Internal data in socket_address structure Predefined in program or user input

Declarations for address transformation functions Example: struct sockaddr_in dest_socket_add; dest_socket_add.sin_addr.s_addr = inet_addr("10.12.110.57");

INFORMATION ABOUT REMOTE HOST

Declaration for gethostbyname When you know the remote site’s domain name, but you don’t know its IP address.

Hostent structure