CS4550 Computer Networks II Socket Interface Ref: Feit Chap 21

Download Report

Transcript CS4550 Computer Networks II Socket Interface Ref: Feit Chap 21

CS4550
Computer Networks II
Socket Interface
Ref: Feit Chap 21
Tanenbaum Chap 6
1
Socket interface
Application
TCP UDP
Application
TCP UDP
raw
IP
raw
IP
Data link
Data link
physical
physical
GENERAL
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.
References
TCP/IP
Chapter 21
- Sidnei Feit
Unix Network
Programming
- W. Richard Stevens
Win 32 Network
DavisProgramming
- Ralph
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
Client / Server Model
Client
Application
request
Server
Application
response
-start anytime
- Always running
-connect to server
- Listen for client requests
-requests service
- Respond to request
-disconnect
- handles multiple clients
TCP SOCKET
CALL SEQUENCE
Socket()
Socket descriptor
bind()
ok
listen()
ok
Accept()
Socket()
Socket descriptor
connect()
New socket descriptor, Client ID
OK
send()
recv()
close()
Dialog
recv()
send()
close()
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
Multiple Connection Architecture
Accept()
server listens
on socket with
server host Ip
address and
port#
Connect()
Switch to a
Switch
a
clienttosocket
on
client
socket
on
which
to talk
which to talk
Client socket
assigned by
Send()
OS at runtime
used to talk
recv()
Clients
must know
server IP
and port#
Helpful tips
*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
Windows Sockets Byte-Order
Conversion Functions(MFC)
Function
ntohs
Ntohl
Htons
Htonl
Purpose
Convert a 16-bit quantity from network
byte order to host byte order (Big-Endian to
Little-Endian).
Convert a 32-bit quantity from network
byte order to host byte order (Big-Endian to
Little-Endian).
Convert a 16-bit quantity from host byte
order to network byte order (Little-Endian
to Big-Endian).
Convert a 32-bit quantity from host byte
order to network byte order (Little-Endian
to Big-Endian).
Sample Programs
Tcpserv.c
Feit p777
tcpclient.c
Feit p 782
wb_client.cpp
on filemaster
wb_server.cpp
on filemaster
Compiling wb_client.cpp in VC++
1)start VC++ , and create a project
File -> 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 project
Project -> 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.exe
6)Run from VC++
Build -> Execute wb_client.exe
Setting Up Socket Communication With MFC
Server
CSocket sockSrvr;
Client
// construct a socket
CSocket sockClient;
// create the SOCKET
sockSrvr.Create(nPort);1,2
sockClient.Create( );2
// start listening
sockSrvr.Listen( );
// construct a new, empty socket
CSocket sockRecv;
// accept connection
sockSrvr.Accept( sockRecv );
// seek a connection
sockClient.Connect(strAddr, nPort);
Important MFC Socket Member Functions
Accept
AsyncSelect
Bind
Close
Connect
Create
IOCtl
Listen
Receive
ReceiveFrom
Send
SendTo
ShutDown
Accepts a connection on the socket.
Requests event notification for the socket.
Associates a local address with the socket.
Closes the socket.
Establishes a connection to a peer socket.
Creates a socket
Controls the mode of the socket.
Establishes a socket to listen for incoming
connection requests.
Receives data from the socket.
Receives a datagram and stores the source
address.
Sends data to a connected socket.
Sends data to a specific destination.
Disables Send and/or Receive calls on the
socket.
Basic Tools
Ping [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