1 cs4550 computer networks ii socket interface ref: feit chap 21 tanenbaum chap 6

16
1 CS4550 Computer Networks II Socket Interface Ref: Feit Chap 21 Tanenbaum Chap 6

Upload: jody-willis

Post on 23-Dec-2015

212 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: 1 CS4550 Computer Networks II Socket Interface Ref: Feit Chap 21 Tanenbaum Chap 6

1

CS4550 Computer Networks II

Socket Interface

Ref: Feit Chap 21Tanenbaum Chap 6

Page 2: 1 CS4550 Computer Networks II Socket Interface Ref: Feit Chap 21 Tanenbaum Chap 6

Socket interface

Application

TCP UDP

raw

IP

Data link

physical

Application

TCP UDP

raw

IP

Data link

physical

Page 3: 1 CS4550 Computer Networks II Socket Interface Ref: Feit Chap 21 Tanenbaum Chap 6

Sockets API - based on Unix Berkeley Software Distribution BSD (release 4.3)

A socket is a communication endpoint — an object through which a Windows Sockets application sends or receives packets of data across a network.

Sockets Abstractions supports many network protocols including Transmission Control Protocol/Internet Protocol (TCP/IP), Xerox® Network System (XNS), Digital Equipment Corporation's DECNet™ protocol, Novell® Corporation's Internet Packet Exchange/Sequenced Packed Exchange (IPX/SPX), and others.

Stream sockets -guaranteed to be delivered and to be correctly sequenced and unduplicated.

Datagram sockets - Not guaranteed to be delivered and to be correctly sequenced and unduplicated.

GENERAL

Page 4: 1 CS4550 Computer Networks II Socket Interface Ref: Feit Chap 21 Tanenbaum Chap 6

TCP/IP - Sidnei Feit Chapter 21

Unix Network - W. Richard Stevens Programming

Win 32 Network - Ralph DavisProgramming

References

Page 5: 1 CS4550 Computer Networks II Socket Interface Ref: Feit Chap 21 Tanenbaum Chap 6

Uses for Sockets

•Client/Server models

•Peer-to-peer scenarios, such as chat applications

•Making remote procedure calls (RPC) by having the receiving application interpret a message as a function call

Page 6: 1 CS4550 Computer Networks II Socket Interface Ref: Feit Chap 21 Tanenbaum Chap 6

Client / Server Model

Client Application

Server Application

- Always running

- Listen for client requests

- Respond to request

- handles multiple clients

-start anytime

-connect to server

-requests service

-disconnect

request

response

Page 7: 1 CS4550 Computer Networks II Socket Interface Ref: Feit Chap 21 Tanenbaum Chap 6

TCP SOCKET CALL SEQUENCE

Socket()

Socket descriptor

connect()

OK

send()

recv()

close()

Socket()

Socket descriptor

bind()

ok

listen()

ok

Accept()

New socket descriptor, Client ID

recv()

send()

close()

Dialog

Page 8: 1 CS4550 Computer Networks II Socket Interface Ref: Feit Chap 21 Tanenbaum Chap 6

Main socket functions

Socket() - create a socket and return a socket handle

bind() - bind socket to a local address

listen() - set up client queue

accept() - wait for connection requests

connect() - attempt to establish connection

send() - send data

recv() - receive data

close() - close the connections

Page 9: 1 CS4550 Computer Networks II Socket Interface Ref: Feit Chap 21 Tanenbaum Chap 6

Switch to a client socket on which to talk

Accept() server listens on socket with server host Ip address and port#

Client socket

assigned by

OS at runtime used to talk

Clients must know

server IP and port#

Switch to a client socket on which to talk

Multiple Connection Architecture

Send()

recv()

Connect()

Page 10: 1 CS4550 Computer Networks II Socket Interface Ref: Feit Chap 21 Tanenbaum Chap 6

*Client must know Server host ID:

- IP address

- server name and translate to IP address using gethostbyname()

*Client must know Server Port

- well known ports

- server advertisement

- auxiliary communication

*Both client and server must translate local data representations to network representations

Helpful tips

Page 11: 1 CS4550 Computer Networks II Socket Interface Ref: Feit Chap 21 Tanenbaum Chap 6

Windows Sockets Byte-Order Conversion Functions(MFC)

Function Purpose

ntohs Convert a 16-bit quantity from network byte order to host byte order (Big-Endian to Little-Endian).

Ntohl Convert a 32-bit quantity from network byte order to host byte order (Big-Endian to Little-Endian).

Htons Convert a 16-bit quantity from host byte order to network byte order (Little-Endian to Big-Endian).

Htonl Convert a 32-bit quantity from host byte order to network byte order (Little-Endian to Big-Endian).

Page 12: 1 CS4550 Computer Networks II Socket Interface Ref: Feit Chap 21 Tanenbaum Chap 6

Sample Programs

Tcpserv.c Feit p777

tcpclient.c Feit p 782

wb_client.cpp on filemaster

wb_server.cpp on filemaster

Page 13: 1 CS4550 Computer Networks II Socket Interface Ref: Feit Chap 21 Tanenbaum Chap 6

Compiling wb_client.cpp in VC++1)start VC++ , and create a projectFile -> new -> Win32 Console Application -> enter your directory and project name “wb_client” 2) put the source file “wb_client.cpp” in the directory “wb_client” that

was created by VC++ for your project.3)Add the file to the projectProject -> Add to Project ->file -> select “wb_client.cpp”4)look at the file select FileV… tab , shows a tree of files, click on wb_client ,5)Compile :Build -> Build wb_client.exe , this should give no errors

This creates an executable file debug/wb_client.exe6)Run from VC++

Build -> Execute wb_client.exe

Page 14: 1 CS4550 Computer Networks II Socket Interface Ref: Feit Chap 21 Tanenbaum Chap 6

Setting Up Socket Communication With MFC

Server Client

// construct a socketCSocket sockSrvr; CSocket sockClient;

// create the SOCKETsockSrvr.Create(nPort);1,2 sockClient.Create( );2

// start listeningsockSrvr.Listen( );

// construct a new, empty socketCSocket sockRecv;// accept connection // seek a connectionsockSrvr.Accept( sockRecv ); sockClient.Connect(strAddr, nPort);

Page 15: 1 CS4550 Computer Networks II Socket Interface Ref: Feit Chap 21 Tanenbaum Chap 6

Important MFC Socket Member Functions

Accept Accepts a connection on the socket.AsyncSelect Requests event notification for the socket.Bind Associates a local address with the socket.Close Closes the socket.Connect Establishes a connection to a peer socket.Create Creates a socketIOCtl Controls the mode of the socket.Listen Establishes a socket to listen for incoming

connection requests.Receive Receives data from the socket.ReceiveFrom Receives a datagram and stores the source

address.Send Sends data to a connected socket.SendTo Sends data to a specific destination.ShutDown Disables Send and/or Receive calls on the

socket.

Page 16: 1 CS4550 Computer Networks II Socket Interface Ref: Feit Chap 21 Tanenbaum Chap 6

Basic ToolsPing [localhost] test availability of network stack

Ping [IP address] test availability of network

Ping [remotehostname] test availability of DNS

netstat get statistics on network activity

Traceroute (Tracert) get topology of network

ipconfig(NT, Unix) get the ethernet interface settings