Introduction to Socket Programming Advisor: Quincy Wu Speaker: Kuan-Ta Lu Date: Nov. 25, 2010.

Slides:



Advertisements
Similar presentations
Socket Programming 101 Vivek Ramachandran.
Advertisements

Sockets: Network IPC Internet Socket UNIX Domain Socket.
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.
Elementary TCP Sockets Computer Networks Computer Networks Term B10 UNIX Network Programming Vol. 1, Second Ed. Stevens Chapter 4.
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.
Socket Programming: a Primer Socket to me!. Feb. 23, 2001EE122, UCB2 Why does one need sockets? application network protocol sockets network.
CSCE 515: Computer Network Programming Socket Wenyuan Xu Department of Computer Science and Engineering.
1 Socket Interfaces Professor Jinhua Guo CIS527 Fall 2003.
Sockets Programming Introduction © Dr. Ayman Abdel-Hamid, CS4254 Spring CS4254 Computer Network Architecture and Programming Dr. Ayman A. Abdel-Hamid.
Tutorial 8 Socket Programming
Programming with Berkeley Sockets Presented by Chris GauthierDickey Written by Daniel Stutzbach (I think!) for CIS 432/532 Useful References: ● man pages.
Introduction to Socket Programming April What is a socket? An interface between application and network –The application creates a socket –The socket.
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.
TCP Socket Programming. r An abstract interface provided to the application programmer  File descriptor, allows apps to read/write to the network r Allows.
ECE453 – Introduction to Computer Networks Lecture 15 – Transport Layer (II)
ECE 4110 – Internetwork Programming Client-Server Model.
Socket Programming. Introduction Sockets are a protocol independent method of creating a connection between processes. Sockets can be either – Connection.
Client-Side Network Programming
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.
Sirak Kaewjamnong Computer Network Systems
 Wind River Systems, Inc Chapter - 13 Network Programming.
Ports Port - A 16-bit number that identifies the application process that receives an incoming message. Reserved ports or well-known ports (0 to 1023)
Outline Socket programming with Windows OSSocket programming with Windows OS C++ UDPSocket classC++ UDPSocket class Socket programming with Windows OSSocket.
Networking Tutorial Special Interest Group for Software Engineering Luke Rajlich.
CPSC 441 TUTORIAL – FEB 13, 2012 TA: RUITNG ZHOU UDP REVIEW.
TELE202 Lecture 15 Socket programming 1 Lecturer Dr Z. Huang Overview ¥Last Lecture »TCP/UDP (2) »Source: chapter 17 ¥This Lecture »Socket programming.
Lecture 15 Overview.
Elementary TCP Sockets UNIX Network Programming Vol. 1, Second Ed. Stevens Chapter 4.
Introduction to Socket
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.
S OCKET P ROGRAMMING IN C Professor: Dr. Shu-Ching Chen TA: HsinYu Ha.
Sockets Introduction Socket address structures Value-result arguments
Introduction to Sockets
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.
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.
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.
Sockets Intro to Network Programming. Before the internet... Early computers were entirely isolated No Internet No network No model No external communications.
1 Socket Interface. 2 Basic Sockets API Review Socket Library TCPUDP IP EthernetPPP ARP DHCP, Mail, WWW, TELNET, FTP... Network cardCom Layer 4 / Transport.
CS 3214 Computer Systems.
CS 3214 Computer Systems Lecture 22 Godmar Back.
Socket Programming (Cont.)
Network Programming CSC- 341
Socket Programming in C
Network Programming with Sockets
Transport layer API: Socket Programming
Chapter 3 Sockets Introduction
תקשורת ומחשוב תרגול 3-5 סוקטים ב-C.
Introduction to Socket Programming
Socket Programming in C
TCP/IP Socket Programming in C
Lecture 2 Socket Programming
Socket Programming(1/2)
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:

Introduction to Socket Programming Advisor: Quincy Wu Speaker: Kuan-Ta Lu Date: Nov. 25, 2010

Socket Interface  是一種應用程式介面 (API) ,介於 application 層與 transport 層之間,並且提供標準的函式以符合不同的網 路傳輸規格。  最早的 Socket Interface 於 1982 年由柏克萊大學為支援 UNIX 作業系統上的 TCP/IP 應用所開發的 Socket 介面, 稱為 Berkeley Socket Interface ,而其軟體則稱為 Berkeley Software Distribution(BSD) 。  Windows Sockets ,簡稱 Winsock ,是以 UNIX 系統上 Berkeley Sockets 的函式為基礎,並加上了一些符合視 窗環境特性的函式。 2

Socket Interface (con.) 3

Connection-oriented socket (TCP) 4

socket()  Create an endpoint for communication  int socket( int domain, int type, int protocol );  domain PF_INET for IPv4 PF_INET6 for IPv6  type SOCK_STREAM for reliable TCP sockets SOCK_DGRAM for unreliable fast UDP sockets  protocol default: 0 5

bind()  Bind a name to a socket  int bind( int s, const struct sockaddr * name, socklen_t namelen );  s The file descriptor to be bound.  name A pointer to the sockaddr structure that holds the address to be bound to the socket.  namelen The length of the sockaddr structure pointed to by name. 6

bind() (con.)  struct sockaddr { sa_family_t sa_family; // address family, AF_xxx char sa_data[14]; // 14 bytes of protocol address };  sa_family represents address family  sa_data contains data about address 7

bind() (con.)  struct in_addr { uint32_t s_addr; // 32bit IPv4 address (4 bytes) // network byte ordered }; struct sockaddr_in { sa_family_t sin_family; // Address family (2 bytes) in_port_t sin_port; // Port number (2 bytes) struct in_addr sin_addr; // Internet address (4 bytes) char sin_zero[8]; // Empty (for padding) (8 bytes) } 8

bind() (con.)  Example: struct sockaddr_in servaddr; servaddr.sin_family = AF_INET; servaddr.sin_addr.s_addr = htonl(INADDR_ANY);htonl servaddr.sin_port = htons(13); Bind (sockfd, (struct sockaddr *) & servaddr, sizeof(servaddr) ); 9

listen()  Listen for connections on a socket  int listen( int s, int backlog );  s The descriptor for the socket that you want to listen on.  backlog The maximum length that the queue of pending connections may grow to. 10

connect()  Initiate a connection on a socket  int connect( int s, const struct sockaddr * name, socklen_t namelen );  s The descriptor of the socket on which to initiate the connection.  name The name of the socket to connect to for a SOCK_STREAM connection.  namelen The length of the name, in bytes. 11

accept()  Accept a connection on a socket  int accept( int s, struct sockaddr * addr, socklen_t * addrlen );  s The listen()ing socket descriptor.  addr This is filled in with the address of the site that's connecting to you.  addrlen This is filled in with the sizeof() the structure returned in the addr parameter. You can safely ignore it if you assume you're getting a struct sockaddr_in back, which you know you are, because that's the type you passed in for addr. 12

send()  Send a message to a connected socket  ssize_t send( int s, const void * msg, size_t len, int flags );  msg A pointer to the message that you want to send.  len The length of the message.  flags Set flags to zero if you want it to be "normal" data. 13

recv()  Receive a message from a socket  ssize_t recv( int s, void * buf, size_t len, int flags );  buf A pointer to a buffer where the function can store the message.  len The size of the buffer.  flags Set flags to 0 if you want it to be a regular vanilla recv(). 14

close()  Close a socket descriptor  int close(int s);  Windows users: the function you need to use is called closesocket(), not close(). 15

Connectionless socket (UDP) 16

sendto()  Send a message to a socket at a specific address  ssize_t sendto( int s, const void * msg, size_t len, int flags, const struct sockaddr * to, socklen_t tolen );  to A pointer to a sockaddr object that specifies the address of the target.  tolen A socklen_t object that specifies the size of the to address. 17

recvfrom()  Receive a message from the socket at a specified address  ssize_t recvfrom( int s, void * buff, size_t len, int flags, struct sockaddr * from, socklen_t * fromlen );  from NULL, or a pointer to a sockaddr object where the function can store the source address of the message.  fromlen A pointer to a socklen_t object that specifies the size of the from buffer. The function stores the actual size of the address in this object. 18

inet_ntop() inet_pton()  Convert IP addresses to human-readable form and back  const char *inet_ntop(int af, const void *src, char *dst, socklen_t size); int inet_pton(int af, const char *src, void *dst);  The "n" stands for "network", and "p" for "presentation".  These functions are for dealing with human-readable IP addresses and converting them to their binary representation for use with various functions and system calls. 19

IPv4 v.s IPv6 Socket 20

Demo  Environment  OS: Fedora 14  Client(homer): :e10:6840:21:4a5b:39ff:fed6:1b73  Server(sinbad): :e10:6840:21:21a:92ff:fe02:3495  Socket TCP (IPv4): tcp4_client.c, tcp4_server.ctcp4_client.ctcp4_server.c  Socket UDP (IPv4): udp4_client.c, udp4_server.cudp4_client.cudp4_server.c  Socket TCP (IPv6): tcp6_client.c, tcp6_server.ctcp6_client.ctcp6_server.c 21

Reference h/tcpip.htmlhttp:// h/tcpip.html 3. socket_programming.ppthttp://ms11.voip.edu.tw/~jryan/ref/ chenglin- socket_programming.ppt mming.pdfhttp://ms11.voip.edu.tw/~jryan/ref/Introduction_to_IPv6_progra mming.pdf 6. Programming_&_IPv6_Translation Middleware.pdfhttp://ms11.voip.edu.tw/~jryan/ref/Windows_Socket Programming_&_IPv6_Translation Middleware.pdf 7. tw/%E4%BC%AF%E5%85%8B%E5%88%A9%E5%A5%97%E6% 8E%A5%E5%AD%97http://zh.wikipedia.org/zh- tw/%E4%BC%AF%E5%85%8B%E5%88%A9%E5%A5%97%E6% 8E%A5%E5%AD% thttp://ms11.voip.edu.tw/~jryan/ref/ipv6_socket_programming.pp t 22

Thanks for listening~ 23

htons() htonl() ntohs() ntohl()  Convert multi-byte integer types from host byte order to network byte order  uint32_t htonl(uint32_t hostlong); uint16_t htons(uint16_t hostshort); uint32_t ntohl(uint32_t netlong); uint16_t ntohs(uint16_t netshort);  htons()host to network short  htonl()host to network long  ntohs()network to host short  ntohl()network to host long 24