Download presentation
Presentation is loading. Please wait.
Published byDylan Reynolds Modified over 9 years ago
1
Socket Programming Tutorial Department of Computer Science Southern Illinois University Edwardsville Fall, 2015 Dr. Hiroshi Fujinoki E-mail: hfujino@siue.edu CS447 - Computer and Data Communication Socket/001
2
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 What is “socket”? CS447 - Computer and Data Communication Socket/001
3
Network Socket as a virtual connection between two processes (physical connection) Host B Process 2 Host A Process 1 Network Adapter Card Socket + NW protocol Socket connection (virtual connection) Information Hiding CS447 - Computer and Data Communication Socket/002
4
Server Host A The server should always be waiting for requests from the clients. A client makes a request, then the server responds. Client 2 Client 3 Host C socket Client 1 Host B Socket as a client/server model Request Reply Socket How can we distinguish two connections? “PORT #” CS447 - Computer and Data Communication Socket/003
5
CS447 - Computer and Data Communication Socket/004 Concept of “ports” What is “port”? It’s a logical connecting point at the transport-layer protocol. If we do not have “ports”, what would happen? IP Address 146.163.147.81 Server Host Internet Request Response Request (1) Response with tag (2) Request (3) Response (4) Internet Request Response Internet Client Host X Process A Process B 146.163.147.81 Client Host Y Connect to process B 146.163.147.81
6
CS447 - Computer and Data Communication Socket/005 Concept of “ports” (2) IP Address 146.163.147.81 Server Host Internet Request Response Request (1) Response with tag (2) Request (3) Response (4) Internet Request Response Internet Client Host X Process A Process B 146.163.147.81 Client Host Y Connect to process B 146.163.147.81 If we use only IP address, we can’t select a destination process at the destination host Only one network application program Can exist at a time
7
Host Computer IP Address CS447 - Computer and Data Communication Socket/006 MAC Layer LLC Layer Network Layer (IP Layer) Transport Layer (TCP Layer) Process DProcess C Process B Process A ports 10459133605512133 Concept of “ports” (3) Physical Layer Datalink Layer
8
SERVER bind() listen() accept() read() write() close() CLIENT socket() connect() write() close() socket() 1: Connection Request 2. Send a request 3. Receive the result read() “*” indicates a blocking function call. * * * * Request Acknowledge CS447 - Computer and Data Communication Socket/007 Client/Server Process Organization Application Programming Interface API
9
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/008 CS447 - Computer and Data Communication
10
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/009 CS447 - Computer and Data Communication
11
“*” indicates a blocking function call. SERVER bind() listen() accept() read() CLIENT socket() connect() write() close() socket() * * * 1: Connection Request 2. Send a command 3. Receive the result read() write() We are not doing this... * Socket/010 4. END CS447 - Computer and Data Communication
12
Server Step 1: socket( ) call It declares a socket to be used. After socket(_) call: Socket/011 Prepare data structure to manage socket OS is responsible for this Server Host Computer CS447 - Computer and Data Communication
13
Step 2: bind( ) call It connects a process to a specific port After bind(_) call: Port Numbers: 0~1023: System Reserved Port 21: FTP Port 23: telnet Port 80: HTTP 1024 and above: available to users Socket/012 Port Port = A logical connecting point at a host for two communicating processes using socket 6500 Server CS447 - Computer and Data Communication
14
Step 3: listen( ) call After listen(_) call: 6500 Buffer listen (socket_id, number_of_connection); listen( ) system call: prepare memory buffer for incoming connections Server Socket/013 Client request We need to specify how many connection requests should be held in the buffer when SERVER is busy (can’t accept a request). CS447 - Computer and Data Communication
15
Step 4 - Part 1: accept( ) call After accept(_) call: Server 6500 accept ( ) function is a blocking function Socket/014 The server process accepts a request from a client Client CS447 - Computer and Data Communication
16
Step 4 - Part 2: accept( ) call The accept(_) call returns another port number and establish another connection Client 7100 6500 Socket/015 A new port is assigned by OS OS duplicates the socket connection Server needs to close the first Server CS447 - Computer and Data Communication
17
Step 5: read( ) and write( ) call Client Server 7100 The server and client communicate using the second socket 6500 Socket/016 Data transmission write (or send) read (or recv) CS447 - Computer and Data Communication
18
Step 6: close ( ) call Client Server 7100 Close the second socket and leave the first socket for next client 6500 Socket/017 CS447 - Computer and Data Communication
19
Step 7: Go back to accept( ) call Client Server 6500 Socket/018 The server process goes back to the accept call CS447 - Computer and Data Communication
20
Socket/019 Winsock Programming Technical Details CS447 - Computer and Data Communication
21
Socket/020 Initialize Winsock 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 ( ….. ); Step 1: Define your socket Step 2: Initialize your socket Step 3: Start using it Winsock version 1.1 CS447 - Computer and Data Communication
22
Socket/021 socket ( ) function unsigned int socket_id = socket (AF_INET, SOCK_STREAM, 0); “AF_INET” = Use IP protocol “SOCK_STREAM” = Use TCP Returns socket ID on success Always 0 CS447 - Computer and Data Communication
23
Socket/022 bind ( ) 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 Return code (< 0 if error) Socket ID returned by socket function CS447 - Computer and Data Communication
24
Socket/023 The “sock_addr” structure struct sockaddr_in my_addr; /* My (client) Internet address */ /* 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 1: You instantiate the structure Step 2: Fill up the components CS447 - Computer and Data Communication
25
Socket/024 listen ( ) function int status = listen (socket_id, 3); The size of the connection request buffer Return code (< 0 if error) Socket ID returned by socket function CS447 - Computer and Data Communication
26
Socket/025 accept ( ) function unsingned int child_sock = accept (socket_id, (struct sockaddr_in *) client_addr, The size of the sockaddr_in structure for connecting client duplicated socket ID (< 0 if error) Socket ID returned by socket function sizeof (client_addr); The sockaddr_in structure for a connecting client CS447 - Computer and Data Communication
27
Socket/026 recv ( ) function int status = recv ( child_sock, in_buffer, MAX_BUFFER_SIZE, 0); Return code (< 0 if error) Always 0 The maximum buffer size The input (receive) buffer as a character string Example: char in_buffer [MAX_BUFFER] Socket ID returned by socket function On success, the number of bytes received CS447 - Computer and Data Communication
28
Socket/027 send ( ) function int status = send ( child_sock, out_buffer, MAX_BUFFER_SIZE, 0); Return code (< 0 if error) Socket ID returned by socket function Always 0 The maximum buffer size The output (send) buffer as a character string On success, the number of bytes actually sent CS447 - Computer and Data Communication
29
Socket/028 closesocket ( ) function int status = closesocket ( child_sock ); Return code (< 0 if error) Socket ID returned by socket function CS447 - Computer and Data Communication
30
Socket/029 Clear winsock After you call “closesocket” function but before your program is terminated /* This stuff cleans-up winsock */ WSACleanup( ); CS447 - Computer and Data Communication
31
“*” indicates a blocking function call. SERVER bind() listen() accept() read() CLIENT socket() connect() write() close() socket() * * * 1: Connection Request 2. Send a command 3. Receive the result read() write() * Socket/030 Format of time stamp: “HH:MM:SS” Client ID# 1 digit integer CS447 - Computer and Data Communication
32
Socket/031 How to specify your destination in socket? Server Host Client Host X IP Address Server Process Port # Each destination for a socket connection is determined by
33
CS447 - Computer and Data Communication Socket/032 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 */ }; How to specify your destination in our socket program source code? struct in_addr { u_long s_addr; /* Actual IP address */ }; 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 Pointer
34
CS447 - Computer and Data Communication Socket/033 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) Network-layer protocol Port number Pointer to in_addr structure Extra information An instance of in_addr structure in memory IP address as a binary number (32 bits)
35
CS447 - Computer and Data Communication Socket/034 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 An instance of in_addr structure in memory 146 163 147 59
36
CS447 - Computer and Data Communication Socket/035 struct sockaddr_in server_address; 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: 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”); Case 1: by “32-bit IP address” Case 2: by a host name Case 3: by a system-defined parameter server_address.sin_addr.s_addr = htonl(INADDR_ANY); IP Address Host Name “INADDR_ANY” keyword
37
CS447 - Computer and Data Communication Socket/036 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!
38
CS447 - Computer and Data Communication Socket/037 #define SERVER_IP "146.163.144.99“ /* Server IP address */ #define SERVER_PORT 8050 /* 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 */ Example of setting IP address and port number
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.