DOC PREVIEW
UT Dallas CS 6390 - 3. SocketProgramminginC

This preview shows page 1-2-3-26-27-28 out of 28 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 28 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 28 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 28 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 28 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 28 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 28 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 28 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

CS 6390 Advanced Computer NetworksSocket ProgrammingWhat is a socket?Two essential types of socketsSocket Creation in C: socketOur view of the InternetPortsThe bind functionThe struct sockaddrSlide 10Skipping the bindConnection Setup (SOCK_STREAM)Connection setup cont’dConnection setup: listen & acceptconnect callSending / Receiving DataSending / Receiving Data (cont’d)closeAddress and port byte-orderingSolution: Network Byte-OrderingUNIX’s byte-ordering funcsDealing with blocking callsDealing w/ blocking (cont’d)select function callTo be used with select:Other useful functionsRelease of portsFinal Thoughts1CS 6390Advanced Computer NetworksSocket Programming in CSlides by Dan Rubenstein2Socket ProgrammingWhat is a socket?Using socketsTypes (Protocols)Associated functionsIssuesWe will look at using sockets in CSockets in Java have similar logic but much easier to use3What 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)4Two essential types of socketsSOCK_STREAMa.k.a. TCP socketsreliable deliveryin-order guaranteedconnection-orientedbidirectionalSOCK_DGRAMa.k.a. UDP socketsunreliable deliveryno in-order guaranteesno notion of “connection” can send and receiveAppsocket321Dest.Appsocket321D1D3D2Q: why have type SOCK_DGRAM?5Socket Creation in C: socketint s = socket(domain, type, protocol);s: socket descriptor, an integer (like a file-handle)domain: integer, communication domaine.g., PF_INET (IPv4 protocol) – typically usedtype: communication typeSOCK_STREAM: reliable, 2-way, connection-based serviceSOCK_DGRAM: unreliable, connectionless,other values: need root permission, rarely used, or obsoleteprotocol: specifies protocol (see file /etc/protocols for a list of options) - usually set to 0NOTE: socket call does not specify where data will be coming from, nor where it will be going to – it just creates the interface!6Our view of the InternetEach host machine has an IP addressWhat happens when a packet arrives at a host?190.2.4.3167.55.34.13129.55.3.33128.15.44.13128.115.4.32128.11.1.12130.1.1.22134.13.1.2128.110.52.27PortsPort 0Port 1Port 65535Each host has 65,536 portsSome ports are reserved for specific apps20,21: FTP23: Telnet80: HTTPsee RFC 1700 (about 2000 ports are reserved)A socket provides an interface to send data to/from the network through a port8The bind functionassociates and (can exclusively) reserves a port for use by the socketint status = bind(sockid, &addrport, size);status: error status, = -1 if bind failedsockid: integer, socket descriptoraddrport: struct sockaddr, the (IP) address and port of the machine (address usually set to INADDR_ANY – chooses a local address)size: the size (in bytes) of the addrport structurebind can be skipped for both types of sockets. When and why?9The struct sockaddrThe generic:struct sockaddr {u_short sa_family;char sa_data[14];};sa_family specifies which address family is being useddetermines how the remaining 14 bytes are usedThe Internet-specific:struct sockaddr_in {short sin_family;u_short sin_port;struct in_addr sin_addr;char sin_zero[8];};sin_family = AF_INETsin_port: port # (0-65535)sin_addr: IP-addresssin_zero: unusedstruct in_addr { u_long s_addr;};1011Skipping the bindSOCK_DGRAM:if only sending, no need to bind. The OS finds a port each time the socket sends a pktif receiving, need to bind (why?)SOCK_STREAM:destination determined during conn. setupdon’t need to know port sending from (during connection setup, receiving end is informed of port)12Connection Setup (SOCK_STREAM) Recall: no connection setup for SOCK_DGRAM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 are “similar”both can send & receive dataeither can terminate the connection13Connection setup cont’dPassive participantstep 1: listen (for incoming requests)step 3: accept (a request)step 4: data transferThe accepted connection is on a new socketThe old socket continues to listen for other active participantsWhy?Active participantstep 2: request & establish connectionstep 4: data transferPassive Participantl-socka-sock-1 a-sock-2Active 1socketActive 2socket14Connection setup: listen & acceptCalled by passive participantint status = listen(sock, queuelen);status: 0 if listening, -1 if error sock: integer, socket descriptorqueuelen: integer, # of active participants that can “wait” for a connectionlisten is non-blocking: returns immediatelyint s = accept(sock, &name, &namelen);s: integer, the new socket desc. (used for data-transfer)sock: integer, the orig. socket desc. (being listened on)name: struct sockaddr, address of the active participantnamelen: sizeof(name): value/result parametermust be set appropriately before calladjusted by OS upon returnaccept is blocking: waits for connection before returning15connect callint status = connect(sock, &name, namelen);status: 0 if successful connect, -1 otherwisesock: integer, socket to be used in connectionname: struct sockaddr: address of passive participantnamelen: integer, sizeof(name)connect is blocki ng16Sending / Receiving Data With a connection (SOCK_STREAM):int count = send(sock, &buf, len, flags);count: # bytes transmitted (-1 if error)buf: char[], buffer to be transmittedlen: integer, length of buffer (in bytes) to transmitflags: integer, special options, usually just 0int count = recv(sock, &buf, len, flags);count: # bytes received (-1 if error)buf: void[], stores received byteslen: # bytes receivedflags: integer, special options, usually just 0Calls are blocking [returns only after data is sent (to socket buf) / received]17Sending / Receiving Data (cont’d) Without a connection (SOCK_DGRAM):int count = sendto(sock, &buf, len, flags,


View Full Document

UT Dallas CS 6390 - 3. SocketProgramminginC

Documents in this Course
VoIP

VoIP

44 pages

TE-MPLS

TE-MPLS

38 pages

TCP

TCP

28 pages

QoS

QoS

27 pages

P2P

P2P

50 pages

IPv6

IPv6

81 pages

IPv6

IPv6

64 pages

AODV-v2

AODV-v2

19 pages

aodv

aodv

32 pages

19. P2P

19. P2P

50 pages

18. VoIP

18. VoIP

44 pages

17. QoS

17. QoS

27 pages

13. TCP

13. TCP

28 pages

6. IPv6

6. IPv6

81 pages

19. P2P

19. P2P

50 pages

18. VoIP

18. VoIP

44 pages

17. QoS

17. QoS

27 pages

6. IPv6

6. IPv6

81 pages

6. IPv6

6. IPv6

81 pages

19. P2P

19. P2P

50 pages

18. VoIP

18. VoIP

44 pages

17. QoS

17. QoS

27 pages

13. TCP

13. TCP

28 pages

CC

CC

74 pages

19. P2P

19. P2P

50 pages

18. VoIP

18. VoIP

44 pages

17. QoS

17. QoS

27 pages

13. TCP

13. TCP

28 pages

6. IPv6

6. IPv6

81 pages

CC

CC

74 pages

Load more
Download 3. SocketProgramminginC
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view 3. SocketProgramminginC and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view 3. SocketProgramminginC 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?