スライド タイトルなし - cs.siue.edu

スライド タイトルなし - cs.siue.edu

CS447 - Socket Programming Tutorial Socket Programming Tutorial Department of Computer Science Southern Illinois University Edwardsville Fall, 2019 Dr. Hiroshi Fujinoki E-mail: [email protected] Socket.ppt/001 CS447 - Socket Programming Tutorial What is socket? 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

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

CS447 - Socket Programming Tutorial How can we distinguish two connections? Socket as a client/server model 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 Host B Request Reply

Socket Socket.ppt/003 Client 3 Client 1 PORT # CS447 - Socket Programming Tutorial Concept of ports What is port? Its a logical connecting point at the transport-layer protocol. If we do not have ports, what would happen? Process B Process A Client Host X uest )

est (1 R e qu ) Response ith tag (2 ew s 146.163.147.81 n o p Re s Req Request (3) Internet 146.163.147.81 R es ponse (4 )

Server Host IP Address 146.163.147.81 Socket.ppt/004 Client Host Y Internet Connect to process B CS447 - Socket Programming Tutorial Concept of ports (2) If we use only IP address, we cant select a destination process at the destination host Process B Process A Only one network application program

Can exist at a time Client Host X uest ) est (1 R e qu ) Response ith tag (2 ew s 146.163.147.81 n o p Re s Req Request (3) Internet 146.163.147.81 R es

ponse (4 ) Server Host IP Address 146.163.147.81 Socket.ppt/005 Client Host Y Internet Connect to process B CS447 - Socket Programming Tutorial Concept of ports (3) Process A 6055 Host Computer Process B

Process C 9133 12133 Process D 1045 Transport Layer (TCP Layer) ports Network Layer (IP Layer) Datalink Layer LLC Layer Physical Layer MAC Layer IP Address Socket.ppt/006

CS447 - Socket Programming Tutorial API Client/Server Process Organization Application Programming Interface SERVER socket() * indicates a blocking function call. CLIENT socket() bind() listen() * accept() *

read() write() Socket.ppt/007 close() 1: Connection Request e Requ st connect() dge e l w o A ck n *

write() 2. Send a request 3. Receive the result read() close() * 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 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 CS447 - Socket Programming Tutorial SERVER

socket() * indicates a blocking function call. CLIENT socket() We are not bind() doing this... listen() * accept() * read() 1: Connection Request *

write() write() 2. Send a command 3. Receive the result 4. END Socket.ppt/010 connect() read() close() * CS447 - Socket Programming Tutorial Step 1: socket( ) call Prepare data structure to manage socket After socket(_) call:

OS is responsible for this Server Server Host Computer It declares a socket to be used. Socket.ppt/011 CS447 - Socket Programming Tutorial Step 2: bind( ) call After bind(_) call: 6500 Server Port Port = A logical connecting point at a host for two communicating processes using socket Port Numbers:

0~1023: System Reserved Port 21: FTP Port 23: telnet Port 80: HTTP It connects a process to a specific port Socket.ppt/012 1024 and above: available to users CS447 - Socket Programming Tutorial Step 3: listen( ) call After listen(_) call: Buffer 6500 listen( ) system call: prepare memory buffer for incoming connections Client request listen (socket_id, number_of_connection);

Server We need to specify how many connection requests should be held in the buffer when SERVER is busy (cant accept a request). Socket.ppt/013 CS447 - Socket Programming Tutorial Step 4 - Part 1: accept( ) call After accept(_) call: 6500 accept ( ) function is a blocking function Client Server The server process accepts a request from a client Socket.ppt/014

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

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

7100 Close the second socket and leave the first socket for next client Socket.ppt/017 CS447 - Socket Programming Tutorial Step 7: Go back to accept( ) call 6500 Client Server The server process goes back to the accept call Socket.ppt/018 CS447 - Socket Programming Tutorial Winsock Programming Technical Details

Socket.ppt/019 CS447 - Socket Programming Tutorial Initialize Winsock Step 1: Define your socket Winsock version 1.1 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 */ /* This stuff initializes winsock WSAStartup(wVersionRequested, &wsaData); /* Create a socket */ My_SocketID = socket ( .. ); Socket.ppt/020

Step 2: Initialize your socket */ Step 3: Start using it CS447 - Socket Programming Tutorial socket ( ) function Returns socket ID on success unsigned int socket_id = socket (AF_INET, SOCK_STREAM, 0); AF_INET = Use IP protocol SOCK_STREAM = Use TCP Always 0 Socket.ppt/021 CS447 - Socket Programming Tutorial bind ( ) function Return code (< 0 if error) Socket ID returned by socket function

int status = bind (socket_id, (struct sockaddr_in *) my_addr, sizeof(my_addr)); The sockaddr_in structure to specify port # and IP address of this machine (server machine) The byte size of the Sockaddr_in structure Socket.ppt/022 CS447 - Socket Programming Tutorial The sock_addr structure /* My (client) Internet address */ struct sockaddr_in my_addr; 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 Socket.ppt/023 CS447 - Socket Programming Tutorial listen ( ) function Return code (< 0 if error) Socket ID returned by socket function int status = listen (socket_id, 3); The size of the connection request buffer Socket.ppt/024 CS447 - Socket Programming Tutorial

accept ( ) function duplicated socket ID (< 0 if error) Socket ID returned by socket function unsingned int child_sock = accept (socket_id, (struct sockaddr_in *) client_addr, sizeof (client_addr); The sockaddr_in structure for a connecting client The size of the sockaddr_in structure for connecting client Socket.ppt/025 CS447 - Socket Programming Tutorial recv ( ) function On success, the number of bytes received Return code (< 0 if error) Socket ID returned by socket function int status = recv (child_sock, in_buffer, MAX_BUFFER_SIZE, 0); The input (receive) buffer as a character string Example: char in_buffer [MAX_BUFFER] The maximum buffer size

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

int status = closesocket (child_sock); Socket ID returned by socket function Socket.ppt/028 CS447 - Socket Programming Tutorial Clear winsock After you call closesocket function but before your program is terminated /* This stuff cleans-up winsock */ WSACleanup( ); Socket.ppt/029 CS447 - Socket Programming Tutorial SERVER socket() * indicates a blocking function call.

CLIENT socket() bind() listen() * accept() * read() Client ID# write() Socket.ppt/030 1: Connection Request connect()

1 digit integer * write() 2. Send a command 3. Receive the result Format of time stamp: HH:MM:SS read() close() * CS447 - Socket Programming Tutorial How to specify your destination in socket? Port # Server

Process Each destination for a socket connection is determined by < IP address + Port# > < IP Address + Port# > Server Host IP Address Socket.ppt/031 Client Host X CS447 - Socket Programming Tutorial How to specify your destination in our socket program source code? sockaddr_in structure is used to define your destination The sockaddr_in structure is defined in C/C++ struct The sockaddr_in structure is defined in windows.h header file struct sockaddr_in { u_char sin_len; u_char sin_family;

u_short sin_port; struct in_addr sin_addr; char sin_zero[8]; }; /* Length of this structure */ /* Network protocol used*/ /* Port number */ /* Pointer to an IP address */ /* Extra information */ struct in_addr { u_long s_addr; }; Socket.ppt/032 /* Actual IP address */ Pointer CS447 - Socket Programming Tutorial 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 Socket.ppt/033 CS447 - Socket Programming Tutorial How to specify your destination in our socket program source code (part 3)?

An instance of sockaddr_in structure in memory 128 IP protocol 1050 Extra information Socket.ppt/034 An instance of in_addr structure in memory 146 163 147 59 CS447 - Socket Programming Tutorial How to specify your destination in our socket program source code (part 4)? How can I set the IP address and the port number of my destination? STEP #1: Instantiate a sockaddr_in structure: struct sockaddr_in server_address; STEP #2: Set your destination IP address:

Case 1: by 32-bit IP address IP Address server_address.sin_addr.s_addr = inet_addr(146.163.147.59); Case 2: by a host name Host Name server_address.sin_addr.s_addr = inet_aton(cougar.siue.edu); Case 3: by a system-defined parameter INADDR_ANY keyword server_address.sin_addr.s_addr = htonl(INADDR_ANY); Socket.ppt/035 CS447 - Socket Programming Tutorial How to specify your destination in our socket program source code (part 4)? 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); You are connecting to port #80! Socket.ppt/036 CS447 - Socket Programming Tutorial Example of setting IP address and port number #define SERVER_IP "146.163.144.99 #define SERVER_PORT 8050 /* Server IP address /* Server-side port # struct sockaddr_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 */ Socket.ppt/037

Recently Viewed Presentations

  • Impact Studies - Saasta

    Impact Studies - Saasta

    Mercury's atmosphere contains mostly nitrogen. Earth's atmosphere contains mostly oxygen. Saturn's atmosphere contains mostly helium. QUESTION 9. The three most abundant gases found in Uranus' atmosphere are __ Hydrogen, Helium & Nitrogen .
  • Soutenance de Thèse

    Soutenance de Thèse

    Centre Léon Bérard. INSERM / UCBL U590. Jeudi 10 décembre 2009. Les gènes TWIST : cibles transcriptionnelles des gènes MYC dans le neuroblastome. Soutenance de thèse. Abdelkader Selmi. Directrice de thèse : Dr Sandrine Wittmann
  • 10 Thing You Should Know About Physical Therapy

    10 Thing You Should Know About Physical Therapy

    12 Things You Should Know About Physical Therapy . Application Process. PT School. By Zach Hall PT, DPT. A little about me.. ... Most of the answers to what values schools are looking for can be found on PTCAS, the...
  • The Selection of Manufacturing Engineering Processes By Dr ...

    The Selection of Manufacturing Engineering Processes By Dr ...

    (the measured size of the finished part after machining) 4. Basic size (the theoretical size used as a starting point for the application of tolerances) ... Symbols for Tolerances and Deviation and Symbols for Fits: 3.A fit (A fit is...
  • Lock Out/Tag Out Authorized/Affected Personnel

    Lock Out/Tag Out Authorized/Affected Personnel

    Lock Out/Tag Out (LO/TO) LOCK OUT / TAG OUT (LO/TO) 29 CFR 1910.147 LO/TO Most incidents occur in workplace around machinery or equipment Other incidents occur from electric shock or hazardous materials This can be avoided by using Lock Out/Tag...
  • Patient Centered Transitional Care for Patients Transferred from

    Patient Centered Transitional Care for Patients Transferred from

    Confirmability. All data will safe kept for audit purpose. Transferability. Purposive sampling method helps to ensure well represent the study context . Develop PCTC Pathway. Drafted transitional care protocol has been developed. Transitional theory and available literatures.
  • The Decision Making Process

    The Decision Making Process

    Evaluate the Decision The Decision Making Process 1. Define Your Problem 2. Gather Information and Resources 3. List Options 4. Weigh and Compare Options 5. Make a Decision Make a Plan to Reach Your Goal 7. Evaluate the Decision *...
  • The Social Organization of Deviance

    The Social Organization of Deviance

    Constructions of Deviance, 6e Patricia A. Adler Peter Adler Part VI Power Point Presentation Kenneth Colburn Sociology Department Butler University