Socket Programming Tutorial Department of Computer Science Southern Illinois University Edwardsville Fall, 2015 Dr. Hiroshi Fujinoki

Slides:



Advertisements
Similar presentations
Socket Programming 101 Vivek Ramachandran.
Advertisements

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.
Quick Overview. 2 ISO/OSI Reference Model Application Application Presentation Presentation Session Session Transport Transport Network Network Data Link.
1 Netcomm Recitation 1: Sockets Communication Networks Recitation 1.
Windows Sockets Purpose Windows Sockets 2 (Winsock) enables programmers to create advanced internet, intranet, and other network-capable applications to.
תקשורת באינטרנט 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.
Tutorial 8 Socket Programming
UDP: User Datagram Protocol. UDP: User Datagram Protocol [RFC 768] r “bare bones”, “best effort” transport protocol r connectionless: m no handshaking.
CS 311 – Lecture 18 Outline IPC via Sockets – Server side socket() bind() accept() listen() – Client side connect() Lecture 181CS Operating Systems.
Socket Addresses. Domains Internet domains –familiar with these Unix domains –for processes communicating on the same hosts –not sure of widespread use.
ISP – 9 th Recitation Socket programming – Client side.
TCP Socket Programming. r An abstract interface provided to the application programmer  File descriptor, allows apps to read/write to the network r Allows.
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.
CPSC 441 TUTORIAL – JANUARY 18, 2012 TA: MARYAM ELAHI INTRODUCTION TO SOCKET PROGRAMMING WITH C.
TCP Socket Programming. r An abstract interface provided to the application programmer  File descriptor, allows apps to read/write to the network r Allows.
Socket Programming References: redKlyde ’ s tutorial set Winsock2 for games (gamedev.net)
Operating Systems Chapter 9 Distributed Communication.
Network Programming Tutorial #9 CPSC 261. A socket is one end of a virtual communication channel Provides network connectivity to any other socket anywhere.
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.
 Wind River Systems, Inc Chapter - 13 Network Programming.
1 Writing Network Applications using the TCP/IP Protocol Stack: Socket Programming.
Technical Details for sockaddr_in Structure Department of Computer Science Southern Illinois University Edwardsville Fall, 2015 Dr. Hiroshi Fujinoki
Lab 5 Sockets. Useful Sockets Links (courtesy of Stanford University) Programming UNIX Sockets in C - Frequently Asked Questions
The Pocket Guide to TCP/IP Sockets: C Version Michael J. Donahoo Kenneth L. Calvert.
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.
1 Computer Networks An Introduction to Computer Networks University of Tehran Dept. of EE and Computer Engineering By: Dr. Nasser Yazdani Lecture 3: Sockets.
TELE202 Lecture 15 Socket programming 1 Lecturer Dr Z. Huang Overview ¥Last Lecture »TCP/UDP (2) »Source: chapter 17 ¥This Lecture »Socket programming.
CSCE 515: Computer Network Programming UDP Socket Wenyuan Xu Department of Computer Science and Engineering.
University of Calgary – CPSC 441.  A socket is an interface between the application and the network (the lower levels of the protocol stack)  The application.
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.
2: Application Layer1 Chapter 2: Application layer r 2.1 Principles of network applications r 2.2 Web and HTTP r 2.3 FTP r 2.4 Electronic Mail  SMTP,
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.
Department of Computer Science Southern Illinois University Edwardsville Spring, 2010 Dr. Hiroshi Fujinoki CS 547/490 Network.
CS 447 Networks and Data Communication Server-Process Organization IP address and SockAddr_In Data Structure Department of Computer Science Southern Illinois.
Lecture 15 Socket Programming CPE 401 / 601 Computer Network Systems slides are modified from Dave Hollinger.
Lecture 3 TCP and UDP Sockets CPE 401 / 601 Computer Network Systems slides are modified from Dave Hollinger.
Socket Programming in C CS587x Lecture 3 Department of Computer Science Iowa State University.
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.
Jim Fawcett CSE 681 – Software Modeling & Analysis Fall 2002
Socket Programming (Cont.)
Jim Fawcett CSE 687-OnLine – Object Oriented Design Summer 2017
The Pocket Guide to TCP/IP Sockets: C Version
Jim Fawcett CSE 775 – Distributed Objects Spring 2007
Socket Programming in C
Network Programming with Sockets
Socket Interface 1 Introduction 11 Socket address formats 2 API 12 13
Tutorial on Socket Programming
CSI 4118 – UNIVERSITY OF OTTAWA
Transport layer API: Socket Programming
Socket Programming in C
TCP Sockets Programming
28.
Chapter 3 Socket API © Bobby Hoggard, Department of Computer Science, East Carolina University These slides may not be used or duplicated without permission.
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
CSI 4118 – UNIVERSITY OF OTTAWA
Jim Fawcett CSE 681 – Software Modeling & Analysis Summer 2003
Presentation transcript:

Socket Programming Tutorial Department of Computer Science Southern Illinois University Edwardsville Fall, 2015 Dr. Hiroshi Fujinoki CS447 - Computer and Data Communication Socket/001

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

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

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

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 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 Client Host Y Connect to process B

CS447 - Computer and Data Communication Socket/005 Concept of “ports” (2) IP Address 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 Client Host Y Connect to process B 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

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 Concept of “ports” (3) Physical Layer Datalink Layer

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

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

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

“*” 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/ END CS447 - Computer and Data Communication

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

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

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

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

Step 4 - Part 2: accept( ) call The accept(_) call returns another port number and establish another connection Client 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

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

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

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

Socket/019 Winsock Programming Technical Details CS447 - Computer and Data Communication

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

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

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

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

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

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

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

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

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

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

“*” 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

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

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

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)

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

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(“ ”); 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

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!

CS447 - Computer and Data Communication Socket/037 #define SERVER_IP " “ /* 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