Socket Programming CS3320 Fall 2010.

Slides:



Advertisements
Similar presentations
Network Programming Week #1 J.P. Yoo
Advertisements

Socket Programming Computer Networks, Spring 2008 Your TAs.
Socket Programming 101 Vivek Ramachandran.
Introduction to Sockets Jan Why do we need sockets? Provides an abstraction for interprocess communication.
Socket programming Socket programming.
Pål Halvorsen (adapted from lectures by Carsten Griwodz & Olav Lysne) Data Communication: Introduction to Berkeley Sockets INF1060: Introduction to Operating.
Pål Halvorsen (adapted from lectures by Carsten Griwodz & Olav Lysne)
Socket Programming. Basics Socket is an interface between application and network – Application creates a socket – Socket type dictates the style of communication.
CS 4700 / CS 5700 Network Fundamentals
Computer Net Lab/Praktikum Datenverarbeitung 2 1 Overview Sockets Sockets in C Sockets in Delphi.
Ipv4 Socket Address Structure struct in_addr { in_addr_t s_addr; /* 32-bit IPv4 address */ /* network byte ordered */ }; struct sockaddr_in { uint8_t sin_len;
CSE 333 – SECTION 8 Networking and sockets. Overview Network Sockets IP addresses and IP address structures in C/C++ DNS – Resolving DNS names Demos.
Taekyung Kim 0x410 ~ 0x International Standards Organization (ISO) is a multinational body dedicated to worldwide agreement on international.
Programming with UDP – I Covered Subjects: IPv4 Socket Address Structure Byte Ordering Functions Address Access/Conversion Functions Functions: 1.socket()
CS252: Systems Programming Ninghui Li Based on slides by Prof. Gustavo Rodriguez-Rivera Topic 16: Socket Programming & Project 5.
1 Socket Programming r What is a socket? r Using sockets m Types (Protocols) m Associated functions m Styles m We will look at using sockets in C.
Networks: TCP/IP Socket Calls1 Elementary TCP Sockets Chapter 4 UNIX Network Programming Vol. 1, Second Ed. Stevens.
1 Netcomm Recitation 1: Sockets Communication Networks Recitation 1.
1 Netcomm 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.
1 Socket Interfaces Professor Jinhua Guo CIS527 Fall 2003.
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.
Client Server Model The client machine (or the client process) makes the request for some resource or service, and the server machine (the server process)
Introduction to Project 1 Web Client and Server Jan 2006.
1 Tutorial on Socket Programming Computer Networks - CSC 458 Department of Computer Science Yukun Zhu (Slides are mainly from Monia Ghobadi, and Amin Tootoonchian,
Client Software Design Objectives: Understand principles of C/S design, with focus on clients Review Windows implementations of Socket functions.
Socket Programming Based on tutorial prepared by EUISOK CHUNG CS3320 Spring2008.
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.
TCP/IP Protocol Stack IP Device Drivers TCPUDP Application Sockets (Gate to network) TCP: –Establish connection –Maintain connection during the communication.
Operating Systems Chapter 9 Distributed Communication.
Socket Programming. Introduction Sockets are a protocol independent method of creating a connection between processes. Sockets can be either – Connection.
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.
Chapter 2 Applications and Layered Architectures Sockets.
The Socket Interface Chapter 21. Application Program Interface (API) Interface used between application programs and TCP/IP protocols Interface used between.
The Socket Interface Chapter 22. Introduction This chapter reviews one example of an Application Program Interface (API) which is the interface between.
1 Socket Programming r What is a socket? r Using sockets m Types (Protocols) m Associated functions m Styles m We will look at using sockets in C.
Remote Shell CS230 Project #4 Assigned : Due date :
Computer Networks : TP1 Prof. Dr. Amine Berqia and Prof. Dr. Fernando Lobo {bamine,
Networking Tutorial Special Interest Group for Software Engineering Luke Rajlich.
CPSC 441 TUTORIAL – FEB 13, 2012 TA: RUITNG ZHOU UDP REVIEW.
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.
UNIX Sockets COS 461 Precept 1. Socket and Process Communication The interface that the OS provides to its networking subsystem application layer transport.
Introduction to Socket
CSE/EE 461 Getting Started with Networking. 2 Basic Concepts A PROCESS is an executing program somewhere. –Eg, “./a.out” A MESSAGE contains information.
Socket Programming Lab 1 1CS Computer Networks.
S OCKET P ROGRAMMING IN C Professor: Dr. Shu-Ching Chen TA: HsinYu Ha.
S OCKET P ROGRAMMING IN C Professor: Dr. Shu-Ching Chen TA: Hsin-Yu Ha.
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.
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.
1 Socket Interface. 2 Basic Sockets API Review Socket Library TCPUDP IP EthernetPPP ARP DHCP, Mail, WWW, TELNET, FTP... Network cardCom Layer 4 / Transport.
CSCE 313 Network Socket MP8 DUE: FRI MAY 5, 2017
Jim Fawcett CSE 681 – Software Modeling & Analysis Fall 2002
Socket Programming in C
Transport layer API: Socket Programming
Socket Programming What is a socket? Using sockets Types (Protocols)
UNIX Sockets Outline Homework #1 posted by end of day
Socket Programming in C
Socket Programming What is a socket? Using sockets Types (Protocols)
Socket Programming(1/2)
Socket Programming What is a socket? Using sockets Types (Protocols)
Internet Networking recitation #8
Socket Programming What is a socket? Using sockets Types (Protocols)
Jim Fawcett CSE 681 – Software Modeling & Analysis Summer 2003
Presentation transcript:

Socket Programming CS3320 Fall 2010

Contents What is Socket? Socket Programming API(C language) Example Bind, Listen Accept Connect Send, Recv Close Example

What is a socket? An interface between application and network The application creates a socket The socket type dictates the style of communication reliable vs. best effort connection-oriented vs. connectionless Once configured, the application can pass data to the socket for network transmission receive data from the socket (transmitted through the network by some other host)

Two essential types of sockets SOCK_STREAM TCP sockets reliable delivery in-order guaranteed connection-oriented SOCK_DGRAM UDP sockets unreliable delivery no order guarantees no notion of “connection” We’ll look at this one App socket 3 2 1 Dest. App socket 3 2 1 D1 D3 D2

Socket Creation

Socket Creation in C: socket int s = socket(domain, type, protocol); where s: socket descriptor, an integer (like a file-handle) domain: integer, communication domain e.g., AF_INET (IPv4 protocol) Note. We’ll use AF_INET type: communication type SOCK_STREAM: reliable, 2-way, connection-based service SOCK_DGRAM: unreliable, connectionless Note. We’ll use SOCK_STREAM protocol: e.g., TCP or UDP use IPPROTO_TCP or IPPROTO_UDP to send/receive TCP or UDP packets Note. We’ll use IPPROTO_TCP

Socket Creation in C: socket /* sample code to create a socket */ hSocket=socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);

Binds a socket to an address

Addresses, Ports and Sockets Like apartments and mailboxes You are the application Street address of your apartment building is the IP address Your mailbox is the port The post-office is the network The socket is the key that gives you access to the right mailbox Q: How do you choose which port a socket connects to?

Addresses, Ports and Sockets Choose a port number that is registered for general use, from 1024 to 49151 Do not use ports 1 to 1023. These ports are reserved for use by the Internet Assigned Numbers Authority (IANA) Avoid using ports 49152 through 65535. These are dynamic ports that operating systems use randomly. If you choose one of these ports, you risk a potential port conflict

The bind function associates a port for use by the socket int status = bind(sock, &addrport, size) where status: return status, 0 if successful, -1 otherwise sock: socket being used addrport: address structure This C structure has four parts to it (next slide) size: the size (in bytes) of the addrport structure Bind is non-blocking: returns immediately

The struct sockaddr The sockaddr_in structure has four parts: sin_family: address family (e.g., AF_INET IP addresses) sin_port: port number sin_addr: IP-address sin_zero: // un-used

(struct sockaddr *) &Address ? Example(Server) Some computers put the most significant byte within a word first (this is called “big-endian” order), and others put it last (“little-endian” order). So that machines with different byte order conventions can communicate, the Internet protocols specify a byte order convention for data transmitted over the network. This is known as network byte order (it is big-endian). // first, create and fill in values for the sockaddr_in structure Address struct sockaddr_in Address; /* create Address stucture */ Address.sin_family = AF_INET; /* AF_INET represents the address family INET for Internet sockets. */ Address.sin_port = htons(nHostPort); /* The function htons() converts from host byte order to network byte order*/ Address.sin_addr.s_addr = INADDR_ANY;  /* INADDR_ANY allows us to work without knowing the IP address of the machine the program is running  on (very convenient) */ // next, bind the socket to the port if( bind(hServerSocket, (struct sockaddr *) &Address, sizeof(Address)) == -1) {         printf("\nCould not connect to host\n");         return -1; } (struct sockaddr *) &Address ?

Connection setup

Connection Setup A connection occurs between two kinds of participants Passive waits for an active participant to request connection Active initiates connection request to passive side Once connection is established, passive and active participants can: both can send & receive data either can terminate the connection

Connection setup cont’d Passive participant (e.g., our server) step 1: listen (for incoming requests) step 3: accept (a request) step 4: data transfer The accepted connection is on a new socket connection need to create another socket The old socket continues to listen for other active participants Active participant (e.g., our client) step 2: request & establish connection step 4: data transfer Passive Participant a-sock-1 l-sock a-sock-2 Active 1 Active 2 socket socket

Connection setup: listen & accept The listen function prepares a bound socket to accept incoming connections int status = listen(sock, queuelen) where status: return value, 0 if listening, -1 if error sock: socket being used queuelen: number of active participants that can “wait” for a connection listen is non-blocking: returns immediately Example code: if (listen(hServerSocket, 1) == -1) {         printf("\nCould not listen\n");         return -1; }

Connection setup: listen & accept Use the accept function to accept a connection request from a remote host The function returns a socket corresponding to the accepted connection int s = accept(sock, &cliaddr, &addrlen) where s: new socket used for data-transfer sock: original socket being listened on (e.g., server) cliaddr: address structure of the active participant (e.g., client) The accept function updates/returns the sockaddr structure with the client's address information addrlen: size (in bytes) of the client sockaddr structure The accept function updates/returns this value accept is blocking: waits for connection before returning Example code: hSocket = accept(hServerSocket, (struct sockaddr *) &Address, (socklen_t *) &nAddressSize); /* socklen_t is socket address length type, defined in sys/socket.h; in our example code it is being cast from a pointer to an integer */

Client create socket and connect to remote host First, the client must create a socket (socket call as before) and fills in its address structure Then, the client connects to the remote host The connect function is used by a client program to establish communication with a remote entity int status = connect(sock, &servaddr, addrlen); where status: return value, 0 if successful connect, -1 otherwise sock: client’s socket to be used in connection servaddr: server’s address structure addrlen: size (in bytes) of the servaddr structure connect is blocking Example code: if(connect(hSocket, (struct sockaddr*) &Address, sizeof(Address)) == -1) {         printf("\nCould not connect to host\n"); }

Sending/Receiving Data

Sending / Receiving Data Send data int count = send(int sock, const void * msg, int len, unsigned int falgs); Where: count: number of bytes transmitted (-1 if error) sock: socket being used buf: buffer to be transmitted len: length of buffer (in bytes) to transmit flags: special options, usually just 0 Receive data int count = recv(int sock, void *buf, int len, unsigned int flags); count: number of bytes received (-1 if error) buf: stores received bytes len: length of buf

Example (Client/Server) … // write a message to the server n = send(sock,buffer,strlen(buffer),0); // do some processing // read a message from the server n = recv(sock,buffer,255,0); // after the client executed a write(), it will read n = recv(newsock,buffer,255,0); // do some processing … // send the result to the client n = send(newsock,resp_msg,strlen(resp_msg),0); CLIENT SERVER

close When finished using a socket, the socket should be closed: status = close(s); status: return value, 0 if successful, -1 if error s: the file descriptor (socket being closed)

Other Library Routines in_addr_t inet_addr(const char* string) returns the 32-bit IP address that corresponds to the A.B.C.D format string. The IP address is in network-byte order. If no error occurs, inet_addr returns an unsigned long value containing a suitable binary representation of the Internet address given. If the string in the cp parameter does not contain a legitimate Internet address, for example if a portion of an "a.b.c.d" address exceeds 255, then inet_addr returns the value INADDR_NONE.

Other Library Routines int gethostname(char* name, int nameLen) sets the character array pointed to by name of length nameLen to a null-terminated string equal to the local host’s name.

Other Library Routines in_addr_t htonl(in_addr_t hostLong) in_addr_t htons(in_port_t hostShort) in_addr_t ntohl(in_addr_t networkLong) in_addr_t ntohs(in_port_t networkShort) each of these functions performs a conversion between a host-format number and a network-format number.

Other Library Routines void bezore(void* buffer, size_t length) fills the array buffer of size length with zeroes void memset(void* buffer, int value, size_t length) fills the array buffer of size length with the value of value.

Compile and run? Need some additional options to compile the client and the server gcc -o server server.c -lsocket -lnsl gcc -o client client.c -lsocket -lnsl Run the server first, then the client after For example: server 4444 // run this on cs1 client cs1.utdallas.edu 4444 // run this on cs2

Summary