Tutorial 8 Socket Programming

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 CS 3516 – Computer Networks. Outline Socket basics Socket details (TCP and UDP) Socket options Final notes.
Distributed Computing Systems Sockets. Outline Socket basics Socket details (TCP and UDP) Socket options Final notes.
Elementary TCP Sockets Computer Networks Computer Networks Term B10 UNIX Network Programming Vol. 1, Second Ed. Stevens Chapter 4.
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.
Elementary TCP Sockets Chapter 4 UNIX Network Programming Vol. 1, Second Ed. Stevens.
Multimedia Networking Sockets. Outline Socket basics Socket details (TCP and UDP) Socket options Final notes.
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.
1 Socket Interfaces Professor Jinhua Guo CIS527 Fall 2003.
תקשורת באינטרנט 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.
Programming with Berkeley Sockets Presented by Chris GauthierDickey Written by Daniel Stutzbach (I think!) for CIS 432/532 Useful References: ● man pages.
Winsock programming.  TCP/IP UDP TCP  Winsock #include wsock32.lib.
EECC694 - Shaaban #1 lec #14 Spring The Application Layer Client/Server Computing, Basic Approaches: –Passing Messages. Example: Communication.
CS 360 – Spring 2007 Pacific University TCP section 6.5 (Read this section!) 27 Feb 2007.
1 Tutorial on Socket Programming Computer Networks - CSC 458 Department of Computer Science Yukun Zhu (Slides are mainly from Monia Ghobadi, and Amin Tootoonchian,
UNIX Sockets COS 461 Precept 1.
Sockets CIS 370 Fall 2009, UMassD. Introduction  Sockets provide a simple programming interface which is consistent for processes on the same machine.
Network Programming Sockets and Winsock. Please Be Responsible We all know that the Internet is full of security holes –most of them do not require any.
Basic Socket Programming TCP/IP overview. TCP interface Reference: –UNIX Network Programming, by Richard Stevens. –UNIX man page.
Ashutosh Shukla Lecture 3 Network Programming CS 640: Computer Networking.
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.
---- IT Acumens. COM IT Acumens. COMIT Acumens. COM.
Remote Shell CS230 Project #4 Assigned : Due date :
Networking Tutorial Special Interest Group for Software Engineering Luke Rajlich.
TELE202 Lecture 15 Socket programming 1 Lecturer Dr Z. Huang Overview ¥Last Lecture »TCP/UDP (2) »Source: chapter 17 ¥This Lecture »Socket programming.
UNIX Sockets COS 461 Precept 1. Socket and Process Communication The interface that the OS provides to its networking subsystem application layer transport.
Introduction to Socket
Socket Programming Lab 1 1CS Computer Networks.
S OCKET P ROGRAMMING IN C Professor: Dr. Shu-Ching Chen TA: HsinYu Ha.
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.
回到第一頁 Client/sever model n Client asks (request) – server provides (response) n Typically: single server - multiple clients n The server does not need.
Socket Programming. Computer Science, FSU2 Interprocess Communication Within a single system – Pipes, FIFOs – Message Queues – Semaphores, Shared Memory.
UNIX Sockets Outline UNIX sockets CS 640.
Inter-Process Communication 9.1 Unix Sockets You may regard a socket as being a communication endpoint. –For two processes to communicate, both must create.
1 Spring Semester 2008, Dept. of Computer Science, Technion Internet Networking recitation #7 Socket Programming.
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.
Socket programming Péter Verhás August 2002
Socket Programming in C
Tutorial on Socket Programming
Transport layer API: Socket Programming
תקשורת ומחשוב תרגול 3-5 סוקטים ב-C.
Socket Programming in C
Sockets Programming Socket to me!.
Sockets Programming Socket to me!.
Internet Networking recitation #8
Outline Communications in Distributed Systems Socket Programming
Sockets.
Today’s topic: Basic TCP API
Presentation transcript:

Tutorial 8 Socket Programming Internet Networking Tutorial 8 Socket Programming

What is socket? First appeared in 4.1 BSD UNIX 1982. An integer associated with a network connection. Implemented as file descriptor. Types of Internet Sockets: Stream Sockets for TCP connection. Datagram Socket for UDP communication. Raw Sockets.

Socket Interface User Space App1 App2 Socket I/F IP Stack Kernel ND1

Structures ‘Struct sockaddr’ holds socket address information: unsigned short sa_family; // AF_INET for internet char sa_data[14]; // 14 bytes of protocol address. contains a destination address and port number for the socket. };

Structures ‘struct sockaddr_in’ is used to deal with ‘struct sockaddr’ for internet family (AF_INET): struct sockaddr_in { short int sin_family; // AF_INET for internet unsigned short int sin_port; // Port number struct in_addr sin_addr; // Internet address unsigned char sin_zero[8]; // padding (must be zero) }; A pointer to a ‘struct sockaddr_in’ can be cast to a pointer to a ‘struct sockaddr’ and vice-versa, since they both have the same size.

Structures In order to set the IP address one should use ‘struct in_addr’: struct in_addr { unsigned long s_addr; // IPv4 address };

Bytes Order The CPU and the NIC may store and read data from the memory in a different order. Memory CPU R/W R/W NIC

Bytes Order Two ways to store data in the memory: Big-Endian Little-Endian One should convert the order of the bytes (if necessary) in order to transmit them in network in the same way. 1 2 3 4 4 3 2 1

Bytes Order Accessories Function: htons(x) – Convert short from host to network order. htonl(x) ntohs(x) ntohl(x) In order to be portable one should use this function even if not necessary (i.e. in a little-endian machine).

System Calls - Socket int socket(int domain, int type, int protocol); Return a file descriptor (or -1 in case of error). domain: AF_INET for internet. type: SOCK_STREAM for TCP, SOCK_DGRAM for UDP and SOCK_RAW for raw socket.

System Call - Bind int bind(int sockfd, struct sockaddr *my_addr, int addrlen); ‘sockfd’ is the socket file descriptor. Associate a socket with a port on the local machine (usually used by servers). ‘my_addr’ is a pointer to a struct sockaddr that contains information about the port. ‘addrlen’ is set to sizeof(struct sockaddr). return 0 on success or -1 on failure (when?).

System Call - Connect int connect(int sockfd, struct sockaddr *serv_addr, int addrlen); Used by clients to connect a socket to a server. ‘sockfd’ is the socket file descriptor. ‘struct sockaddr’ contains the destination port and IP address ‘addrlen’ can be set to sizeof(struct sockaddr).

System Call - Listen int listen(int sockfd, int backlog); Define the maximum of pending TCP connections. ‘sockfd’ is the socket file descriptor. ‘backlog is the number of pending connections.

System Call - Accept int accept(int sockfd, void *addr, int *addrlen); The accept system call is used by a TCP server to accept pending connection. Blocked until a connection is arrived. Return a new socket file descriptor that is used to receive and send data on this connection. ‘sockfd’ is the socket file descriptor. ‘addr’ contains information (port and IP address) of the incoming connection. ‘addrlen’ is a local integer variable that should be set to sizeof(struct sockaddr_in).

Data Transfer int send(int sockfd, const void *msg, int len, int flags); Transmit data to through a socket. Can be used only when the socket is connected. Return the number of bytes that has been sent (or -1 in case of error). ‘buf’ is a pointer to the buffer to be sent, while ‘len’ is its length.

Data Transfer int recv(int sockfd, void *buf, int len, unsigned int flags); Obtain data from the socket. Used only when the socket is in a connected state. Return the number of bytes that has been obtailed input (or -1 in case of error). ‘buf’ is a pointer to the buffer for holding the received data and ‘len’ is its length.

Data Transfer int sendto(int sockfd, const void *msg, int len, unsigned int flags, const struct sockaddr *to, int tolen); Transmit data to through a socket. The destination (port and IP address) is specified in the call. Used for UDP communication.

Data Transfer int recvfrom(int sockfd, void *buf, int len, unsigned int flags, struct sockaddr *from, int *fromlen); Obtain data from the socket. ‘sockaddr’ contains information about the originator of the data. Used for UDP communication.

Data Transfer Since ‘send’ and ‘recv’ are used in a connected mode, the usually used in TCP connection while ‘recvfrom’ and ‘sendto’ are used for UDP communication.

Other Services Dealing with IP Addresses DNS ina.sin_addr.s_addr = inet_addr("168.68.37.90"); printf("%s", inet_ntoa(ina.sin_addr)); DNS int gethostname(char* name, int namelen) struct hostent *gethostbyname(char* name)

Client-Server Model UDP Client sd=socket(…) sendto(sd,…) recvfrom(sd,…) close(sd) UDP Server sd=socket(…) bind(sd,…) recvfrom(sd,…) sendto(sd,…) close(sd)

Client-Server Model TCP Client sd=socket(…) connect(sd) send(sd,…) recv(sd,…) close(sd) TCP Server sd=socket(…) bind(sd,…) listen(sd,…) new_sd=accept(sd,…) fork() recv(new_sd,…) send(new_sd,…) close(new_sd)

Port Assignment Ports 0 through 1023 are reserved. In order to let the system to assign the port number, one should set the port number (e.g. in ‘bind’) to zero.