1 / 38

Socket Programming Tutorial

Learn about sockets, virtual connections between processes, bi-directional transmission, ports, and socket programming concepts.

Download Presentation

Socket Programming Tutorial

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Socket Programming Tutorial CS447 - Socket Programming Tutorial Department of Computer Science Southern Illinois University Edwardsville Fall, 2019 Dr. Hiroshi Fujinoki E-mail: hfujino@siue.edu Socket.ppt/001

  2. What is “socket”? CS447 - Socket Programming Tutorial • A socket is a virtual connection between two applications • Using a socket, two processes can communicate with each other • The socket is the major communication tool for Internet applications • A socket is bi-directional (full-duplex) transmission • A socket can be created dynamically Socket.ppt/001

  3. Socket + NW protocol Socket connection (virtual connection) Network Adapter Card CS447 - Socket Programming Tutorial Socket as a virtual connection between two processes Host A Host B Process 1 Process 2 Information Hiding Network (physical connection) Socket.ppt/002

  4. How can we distinguish two connections? The server should always be waiting for requests from the clients. A client makes a request, then the server responds. Host C Client 2 socket socket Host A Server Client 3 Host B Request Reply Socket “PORT #” Client 1 CS447 - Socket Programming Tutorial Socket as a client/server model Socket.ppt/003

  5. Concept of “ports” It’s a logical connecting point at the transport-layer protocol. Connect to process B Process A Process B Client Host X Request Request Request (1) Response Response Response with tag (2) Request (3) Server Host Internet Internet Response (4) 146.163.147.81 IP Address 146.163.147.81 Internet Client Host Y CS447 - Socket Programming Tutorial What is “port”? If we do not have “ports”, what would happen? 146.163.147.81 Socket.ppt/004

  6. Concept of “ports” (2) If we use only IP address, we can’t select a destination process at the destination host Connect to process B Only one network application program Can exist at a time Process A Process B Client Host X Request Request Request (1) Response Response Response with tag (2) Request (3) Server Host Internet Internet Response (4) 146.163.147.81 IP Address 146.163.147.81 Internet Client Host Y CS447 - Socket Programming Tutorial 146.163.147.81 Socket.ppt/005

  7. Host Computer Concept of “ports” (3) 6055 9133 12133 1045 Transport Layer (TCP Layer) ports Process D Process C Process B Process A Network Layer (IP Layer) LLC Layer Physical Layer Datalink Layer MAC Layer IP Address CS447 - Socket Programming Tutorial Socket.ppt/006

  8. API Client/Server Process Organization “*” indicates a blocking function call. 1: Connection Request * * Request Acknowledge * 2. Send a request * 3. Receive the result CS447 - Socket Programming Tutorial Application Programming Interface SERVER socket() CLIENT socket() bind() listen() connect() accept() write() read() read() write() close() close() Socket.ppt/007

  9. CS447 - Socket Programming Tutorial Functions and parameter format (for server side) (1) create socket: socket_id = socket (AF_INET, SOCK_STREM, DEFAULT_PROTOCOL); (2) bind socket: bind (socket_id, server_addr, server_len); (3) listen to socket: listen (socket_id, number_of_connection); (4) accept a connection: accept (socket_id, &client_addr, &client_len); (5) read (receive) data: read (socket_id, buffer, buffer_len); (6) write (send) data: write (socket_id, buffer, buffer_len); (7) close socket: close(socket_id); Socket.ppt/008

  10. CS447 - Socket Programming Tutorial Functions and parameter format (for client side) (1) create socket: same as server socket_id = socket (AF_INET, SOCK_STREM, DEFAULT_PROTOCOL); (2) connect socket: connect (socket_id, serverINETaddress, server_len); (3) write (send) data: write (socket_id, buffer, buffer_len); (4) read (receive) data: read (socket_id, buffer, buffer_len); (5) close socket: same as server close(socket_id); Socket.ppt/009

  11. write() 4. END close() CS447 - Socket Programming Tutorial SERVER “*” indicates a blocking function call. socket() CLIENT We are not doing this... socket() bind() 1: Connection Request * listen() connect() * accept() write() * read() 2. Send a command * read() 3. Receive the result Socket.ppt/010

  12. Step 1: socket( ) call • Prepare data structure to • manage socket • OS is responsible for this Server Server Host Computer CS447 - Socket Programming Tutorial After socket(_) call: It declares a socket to be used. Socket.ppt/011

  13. Step 2: bind( ) call Port Port = A logical connecting point at a host for two communicating processes using socket 6500 Server CS447 - Socket Programming Tutorial After bind(_) call: Port Numbers: 0~1023: System Reserved Port 21: FTP Port 23: telnet Port 80: HTTP 1024 and above: available to users It connects a process to a specific port Socket.ppt/012

  14. Step 3: listen( ) call listen( ) system call: prepare memory buffer for incoming connections Buffer Client request Server We need to specify how many connection requests should be held in the buffer when SERVER is busy (can’t accept a request). CS447 - Socket Programming Tutorial After listen(_) call: 6500 listen (socket_id, number_of_connection); Socket.ppt/013

  15. accept ( ) function is a blocking function Client Server The server process accepts a request from a client CS447 - Socket Programming Tutorial Step 4 - Part 1: accept( ) call After accept(_) call: 6500 Socket.ppt/014

  16. Step 4 - Part 2: accept( ) call  Server needs to close the first Server  OS duplicates the socket connection  A new port is assigned by OS CS447 - Socket Programming Tutorial The accept(_) call returns another port number and establish another connection 6500 Client 7100 Socket.ppt/015

  17. write (or send) read (or recv) Server Data transmission The server and client communicate using the second socket CS447 - Socket Programming Tutorial Step 5: read( ) and write( ) call 6500 Client 7100 Socket.ppt/016

  18. Close the second socket and leave the first socket for next client CS447 - Socket Programming Tutorial Step 6: close( ) call 6500 Client 7100 Server Socket.ppt/017

  19. The server process goes back to the accept call CS447 - Socket Programming Tutorial Step 7: Go back to accept( ) call 6500 Client Server Socket.ppt/018

  20. CS447 - Socket Programming Tutorial Winsock Programming Technical Details Socket.ppt/019

  21. Initialize Winsock Step 1: Define your socket Winsock version 1.1      Step 2: Initialize your socket /* This stuff initializes winsock */ WSAStartup(wVersionRequested, &wsaData); /* Create a socket */ My_SocketID = socket ( ….. ); Step 3: Start using it CS447 - Socket Programming Tutorial void main (void) { /* The following two lines needed for Window's socket */ WORD wVersionRequested = MAKEWORD(1,1); /* Stuff for WSA functions */ WSADATA wsaData; /* Stuff for WSA functions */ Socket.ppt/020

  22. Returns socket ID on success “AF_INET” = Use IP protocol socket ( ) function “SOCK_STREAM” = Use TCP Always 0 CS447 - Socket Programming Tutorial unsigned int socket_id = socket (AF_INET, SOCK_STREAM, 0); Socket.ppt/021

  23. bind ( ) function Return code (< 0 if error) Socket ID returned by socket function The sockaddr_in structure to specify port # and IP address of this machine (server machine) The byte size of the Sockaddr_in structure CS447 - Socket Programming Tutorial int status = bind (socket_id, (struct sockaddr_in *) my_addr, sizeof(my_addr)); Socket.ppt/022

  24. The “sock_addr” structure Step 1: You instantiate the structure    /* Set My(client's) IP Address ---------------------------------------- */ my_addr.sin_family= AF_INET; /* Address Family To Be Used */ my_addr.sin_port = htons (MY_PORT_NUM); /* Port number to use */ my_addr.sin_addr.s_addr = htonl (INADDR_ANY); /* My IP address */ Step 2: Fill up the components CS447 - Socket Programming Tutorial struct sockaddr_in my_addr; /* My (client) Internet address */ Socket.ppt/023

  25. listen ( ) function Return code (< 0 if error) Socket ID returned by socket function The size of the connection request buffer CS447 - Socket Programming Tutorial int status = listen (socket_id, 3); Socket.ppt/024

  26. accept ( ) function duplicated socket ID (< 0 if error) Socket ID returned by socket function The sockaddr_in structure for a connecting client The size of the sockaddr_in structure for connecting client CS447 - Socket Programming Tutorial unsingned int child_sock = accept (socket_id, (struct sockaddr_in *) client_addr, sizeof (client_addr); Socket.ppt/025

  27. recv ( ) function Return code (< 0 if error) Socket ID returned by socket function The input (receive) buffer as a character string The maximum buffer size Always 0 CS447 - Socket Programming Tutorial On success, the number of bytes received int status = recv (child_sock, in_buffer, MAX_BUFFER_SIZE, 0); Example: char in_buffer [MAX_BUFFER] Socket.ppt/026

  28. send ( ) function Return code (< 0 if error) Socket ID returned by socket function The output (send) buffer as a character string The maximum buffer size Always 0 CS447 - Socket Programming Tutorial On success, the number of bytes actually sent int status = send (child_sock, out_buffer, MAX_BUFFER_SIZE, 0); Socket.ppt/027

  29. closesocket ( ) function Return code (< 0 if error) Socket ID returned by socket function CS447 - Socket Programming Tutorial int status = closesocket (child_sock); Socket.ppt/028

  30. Clear winsock CS447 - Socket Programming Tutorial After you call “closesocket” function but before your program is terminated /* This stuff cleans-up winsock */ WSACleanup( ); Socket.ppt/029

  31. 1 digit integer write() close() Format of time stamp: “HH:MM:SS” CS447 - Socket Programming Tutorial SERVER “*” indicates a blocking function call. socket() CLIENT socket() bind() 1: Connection Request * listen() connect() * accept() Client ID# write() * read() 2. Send a command * read() 3. Receive the result Socket.ppt/030

  32. How to specify your destination in socket? Each destination for a socket connection is determined by < IP address + Port# > Port # Client Host X Server Host IP Address Server Process CS447 - Socket Programming Tutorial < IP Address + Port# > Socket.ppt/031

  33. sockaddr_in structure is used to define your destination How to specify your destination in our socket program source code? struct sockaddr_in { u_char sin_len; /* Length of this structure */ u_char sin_family; /* Network protocol used*/ u_short sin_port; /* Port number */ struct in_addr sin_addr; /* Pointer to an IP address */ char sin_zero[8]; /* Extra information */ }; Pointer struct in_addr { u_long s_addr; /* Actual IP address */ }; CS447 - Socket Programming Tutorial • The sockaddr_instructure is defined in C/C++ struct • The sockaddr_in structure is defined in windows.h header file Socket.ppt/032

  34. How to specify your destination in our socket program source code (part 2)? An instance of sockaddr_in structure in memory Structure length (in bytes) An instance of in_addr structure in memory Network-layer protocol IP address as a binary number (32 bits) Port number Pointer to in_addr structure Extra information CS447 - Socket Programming Tutorial Socket.ppt/033

  35. How to specify your destination in our socket program source code (part 3)? CS447 - Socket Programming Tutorial An instance of sockaddr_in structure in memory An instance of in_addr structure in memory 128 IP protocol 146 163 147 59 1050 Extra information Socket.ppt/034

  36. How to specify your destination in our socket program source code (part 4)? IP Address Case 1: by “32-bit IP address” Host Name “INADDR_ANY” keyword Case 2: by a host name Case 3: by a system-defined parameter CS447 - Socket Programming Tutorial How can I set the IP address and the port number of my destination? STEP #1: Instantiate a sockaddr_in structure: structsockaddr_in server_address; STEP #2: Set your destination IP address: server_address.sin_addr.s_addr = inet_addr(“146.163.147.59”); server_address.sin_addr.s_addr = inet_aton(“cougar.siue.edu”); server_address.sin_addr.s_addr = htonl(INADDR_ANY); Socket.ppt/035

  37. How to specify your destination in our socket program source code (part 4)? You are connecting to port #80! CS447 - Socket Programming Tutorial How can I set the IP address and the port number of my destination? STEP #3: Set your destination port number: server_address.sin_port = htons(80); Socket.ppt/036

  38. Example of setting IP address and port number #define SERVER_IP "146.163.144.99“ /* Server IP address */ #define SERVER_PORT 8050 /* Server-side port # */ structsockaddr_in server_addr; /* Server Internet address */ /* Set Server's IP Address --------------------------------------------- */ server_addr.sin_family = AF_INET; /* Address Family to be Used */ server_addr.sin_addr.s_addr = inet_addr(SERVER_IP); /* IP address */ server_addr.sin_port = htons(int(SERVER_PORT));/* Port num to use */ CS447 - Socket Programming Tutorial Socket.ppt/037

More Related