Download presentation
Presentation is loading. Please wait.
Published byJody Willis Modified over 9 years ago
1
1 CS4550 Computer Networks II Socket Interface Ref: Feit Chap 21 Tanenbaum Chap 6
2
Socket interface Application TCP UDP raw IP Data link physical Application TCP UDP raw IP Data link physical
3
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
4
TCP/IP - Sidnei Feit Chapter 21 Unix Network- W. Richard Stevens Programming Win 32 Network - Ralph DavisProgramming References
5
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
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
7
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
8
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
9
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()
10
*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
11
Windows Sockets Byte-Order Conversion Functions(MFC) Function Purpose ntohsConvert a 16-bit quantity from network byte order to host byte order (Big-Endian to Little-Endian). NtohlConvert a 32-bit quantity from network byte order to host byte order (Big-Endian to Little-Endian). HtonsConvert a 16-bit quantity from host byte order to network byte order (Little-Endian to Big-Endian). HtonlConvert a 32-bit quantity from host byte order to network byte order (Little-Endian to Big-Endian).
12
Sample Programs Tcpserv.cFeit p777 tcpclient.cFeit p 782 wb_client.cppon filemaster wb_server.cppon filemaster
13
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
14
Setting Up Socket Communication With MFC Server Client // construct a socket CSocket sockSrvr; CSocket sockClient; // create the SOCKET sockSrvr.Create(nPort);1,2sockClient.Create( );2 // start listening sockSrvr.Listen( ); // construct a new, empty socket CSocket sockRecv; // accept connection// seek a connection sockSrvr.Accept( sockRecv ); sockClient.Connect(strAddr, nPort);
15
Important MFC Socket Member Functions AcceptAccepts a connection on the socket. AsyncSelectRequests event notification for the socket. BindAssociates a local address with the socket. CloseCloses the socket. ConnectEstablishes a connection to a peer socket. CreateCreates a socket IOCtlControls the mode of the socket. ListenEstablishes a socket to listen for incoming connection requests. ReceiveReceives data from the socket. ReceiveFromReceives a datagram and stores the source address. SendSends data to a connected socket. SendToSends data to a specific destination. ShutDownDisables Send and/or Receive calls on the socket.
16
Basic Tools Ping [localhost]test availability of network stack Ping [IP address]test availability of network Ping [remotehostname] test availability of DNS netstatget statistics on network activity Traceroute (Tracert)get topology of network ipconfig(NT, Unix)get the ethernet interface settings
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.