Upload
isabel-bruce
View
235
Download
1
Embed Size (px)
Citation preview
Building an ftp client and server using Building an ftp client and server using socketssockets
we now know enough to build a sophisticated we now know enough to build a sophisticated client/server application!client/server application! ftpftp telnettelnet smtpsmtp httphttp
Implementation: the socket data Implementation: the socket data structurestructure
within the OS, one such data structure per socket:within the OS, one such data structure per socket:
Socket Structure Fieldssocket typesocket options (from socket())time to “linger”socket state (ISCONNECTED, ISCONNECTING,…)pointer to lower level protocol infopointer to accept() socketqueue, number of partially formed connectionsmax number of queued connectionsqueue, number of pending incoming connectionsevent to sleep on if no incoming connectionserror flagssignaling informationout-of-band pointer
socket data structure (cont.)socket data structure (cont.)
sendbuffer
fields/characteristics
number of bytes in bufferhigh water marklow water markpointer to chain of buffers (mbufs)ptr to proc doing send/receiveflags
receivebuffer
same fields as send buffer
Implementation: OS actions on sendto()Implementation: OS actions on sendto()
Implementations: OS actions on Implementations: OS actions on rcvfrom()rcvfrom()
UNIX SVR4: TLI InterfaceUNIX SVR4: TLI Interface
Transport Layer Interface for Unix Transport Layer Interface for Unix System V, Release 4 (svr4)System V, Release 4 (svr4)
like sockets:like sockets: transport independenttransport independent two modes: connection-oriented, two modes: connection-oriented,
datagramdatagram ``transport endpoint'' is like a socket ``transport endpoint'' is like a socket
TLI implemented as user-level C TLI implemented as user-level C
library routines rather than library routines rather than
system callssystem calls
Some TLI versus socket Some TLI versus socket comparisonscomparisons
TLI Function closest BSDcall
comments
t_open() socket()t_bind() bind(),
listen()does work of BSD bind and listen
t_listen() accept() waits for request but does not accept it.Returns info on client
t_snddis() reject client without acceptt_accept() accept() TLI will not create new endpoint. BSD
accept()=t_list+t_open+t_acceptt_connect() connect()t_send() send()t_recv() recv()t_sendudata() sendto()t_recvudata() rcvfrom()t_look() get current event from transport connectiont_getstate() get current state of transport connection
Windows SocketsWindows Sockets
Based on BSD sockets:Based on BSD sockets:
BSD: ``the de facto standard for TCP/IP Networking'' (quote from Winsock1.1 BSD: ``the de facto standard for TCP/IP Networking'' (quote from Winsock1.1 documentation)documentation)
supports stream(TCP)/datagram(UDP) modelsupports stream(TCP)/datagram(UDP) model
API the same as what we have seenAPI the same as what we have seen
A few differences/incompatibilities:A few differences/incompatibilities:
extensions for asynchronous programmingextensions for asynchronous programming different error return codes: -1 not the error return code!different error return codes: -1 not the error return code! socket identifier different from file identifiersocket identifier different from file identifier read(), write(), close() should not be used read(), write(), close() should not be used
use socket-specific equivalents insteaduse socket-specific equivalents instead
Windows Sockets: System CallsWindows Sockets: System Calls
* = The routine can block if acting on a blocking socket.
accept() * An incoming connection is acknowledged and associatedwith an immediately created socket. The original socket isreturned to the listening state.
bind() Assign a local name to an unnamed socket.closesocket() * Remove a socket from the per-process object reference
table. Only blocks if SO_LINGER is set.connect() * Initiate a connection on the specified socket.getpeername() Retrieve the name of the peer connected to the specified
socket.getsockname() Retrieve the current name for the specified socketgetsockopt() Retrieve options associated with the specified socket.htonl() Convert a 32-bit quantity from host byte order to network
byte order.htons() Convert a 16-bit quantity from host byte order to network
byte order.inet_addr() Converts a character string representing a number in the
Internet standard ".'' notation to an Internet address value.inet_ntoa() Converts an Internet address value to an ASCII string in ".''
notation i.e. "a.b.c.d''.
Windows Sockets: System Calls Windows Sockets: System Calls (cont)(cont)
* = The routine can block if acting on a blocking socket.
ioctlsocket() Provide control for sockets.listen() Listen for incoming connections on a specified socket.ntohl() Convert a 32-bit quantity from network byte order to host
byte order.ntohs() Convert a 16-bit quantity from network byte order to host
byte order.recv() * Receive data from a connected socket.recvfrom() * Receive data from either a connected or unconnected
socket.select() * Perform synchronous I/O multiplexing.send() * Send data to a connected socket.sendto() * Send data to either a connected or unconnected socket.setsockopt() Store options associated with the specified socket.shutdown() Shut down part of a full-duplex connection.socket() Create an endpoint for communication and return a
socket.
Windows Sockets: database functionsWindows Sockets: database functions
* = * = the routine can block under some circumstancesthe routine can block under some circumstances.
gethostbyaddr() * Retrieve the name(s) and addresscorresponding to a networkaddress.
gethostbyname() * Retrieve the name(s) and addresscorresponding to a host name.
gethostname() Retrieve the name of the localhost.
getprotobyname() * Retrieve the protocol name andnumber corresponding to aprotocol name.
getprotobynumber() * Retrieve the protocol name andnumber corresponding to aprotocol number.
getservbyname() * Retrieve the service name andport corresponding to a servicename.
getservbyport() * Retrieve the service name andport corresponding to a port.
Windows Sockets: asynchronyWindows Sockets: asynchrony asynchronous support: allows programmer to specify routine asynchronous support: allows programmer to specify routine
to be called when socket-related event occurs: to be called when socket-related event occurs:
socket ready for reading/writing socket ready for reading/writing out-of-band data ready for reading out-of-band data ready for reading socket ready to accept connection socket ready to accept connection connection closed connection closed
these these extensions extensions help with concurrent, multithreaded help with concurrent, multithreaded Windows programmingWindows programming
WSAAsyncGetHostByAddr() A set of functions which provide asynchronousversions of the standard Berkeley getXbyY()functions. For example, theWSAAsyncGetHostByName() function provides anasynchronous message based implementation of thestandard Berkeley gethostbyname() function.
WSAAsyncGetHostByName()
Windows Sockets: asynchrony(cont)Windows Sockets: asynchrony(cont)
WSAAsyncGetProtoByName()WSAAsyncGetProtoByNumber()WSAAsyncGetServByName()WSAAsyncGetServByPort()WSAAsyncSelect() Perform asynchronous version of select()WSACancelAsyncRequest() Cancel an outstanding instance of a
WSAAsyncGetXByY() function.WSACancelBlockingCall() Cancel an outstanding "blocking" API callWSACleanup() Sign off from the underlying Windows Sockets DLL.WSAGetLastError() Obtain details of last Windows Sockets API errorWSAIsBlocking() Determine if the underlying Windows Sockets DLL is
already blocking an existing call for this threadWSASetBlockingHook() "Hook" the blocking method used by the underlying
Windows Sockets implementationWSASetLastError() Set the error to be returned by a subsequent
WSAGetLastError()WSAStartup() Initialize the underlying Windows Sockets DLL.WSAUnhookBlockingHook() Restore the original blocking function
Network Programming in JavaNetwork Programming in Java network API abstraction: socket network API abstraction: socket
transport service classes same as with sockets: transport service classes same as with sockets: datagram: UDP datagram: UDP connection-oriented: TCP connection-oriented: TCP
a slightly (only) higher-level interface than with UNIX sockets a slightly (only) higher-level interface than with UNIX sockets low level system calls (those we've seen for UNIX) also available low level system calls (those we've seen for UNIX) also available
some JAVA-specific considerations: some JAVA-specific considerations: applets can connect only to servers from whence they came (security) applets can connect only to servers from whence they came (security) JAVA garbage collects unused objects JAVA garbage collects unused objects
excellent reference: excellent reference: Java in a Nutshell, 2nd ed. D. Flanagan, O'Reily and Associates, 1997
Sending a Datagram in JavaSending a Datagram in Java
create a DatagramPacket, specifying data, length, dest. IP create a DatagramPacket, specifying data, length, dest. IP address/port address/port
invoke send() method (procedure) of DatagramSocketinvoke send() method (procedure) of DatagramSocket // This example is from the book _Java in a Nutshell_ by
David Flanagan.// Written by David Flanagan. Copyright (c) 1996 O'Reilly &
Associates.// You may study, use, modify, and distribute this example
for any purpose.// This example is provided WITHOUT WARRANTY either expressed
or implied. import java.io.*;import java.net.*;
// This class sends the specified text as a datagram to port 6010 of the specified host.
public class UDPSend { static final int port = 6010; public static void main(String args[]) throws Exception { if (args.length != 2) { System.out.println("Usage: java UDPSend <HOSTNAME><MESSAGE>"); System.exit(0); }
// Get the internet address of the specified host InetAddress address = InetAddress.getByName(args[0]); // Convert the message to an array of bytes int msglen = args[1].length(); byte[] message = new byte[msglen]; args[1].getBytes(0, msglen, message, 0);
// Initilize the packet with data and address DatagramPacket packet = new DatagramPacket(message, msglen, address, port); // Create a socket, and send the packet through it. DatagramSocket socket = new DatagramSocket(); socket.send(packet); }
Receiving a Datagram in JavaReceiving a Datagram in Java
create a DatagramPacket with a buffer to receive packet create a DatagramPacket with a buffer to receive packet create a DatagramSocket that will "listen" for packet create a DatagramSocket that will "listen" for packet invoke receive() method (procedure) of DatagramSocketinvoke receive() method (procedure) of DatagramSocket
Receiving a Datagram in Java (cont)Receiving a Datagram in Java (cont)
// This example is from the book _Java in a Nutshell_ by David Flanagan.
// Written by David Flanagan. Copyright (c) 1996 O'Reilly & Associates.
// You may study, use, modify, and distribute this example for any purpose.
// This example is provided WITHOUT WARRANTY either expressed or implied.
import java.io.*;import java.net.*;
// This program waits to receive datagrams sent to port 6010.
// When it receives one, it displays the sending host and port,
// and prints the contents of the datagram as a string.public class UDPReceive { static final int port = 6010; public static void main(String args[]) throws
Exception { byte[] buffer = new byte[1024]; String s; // Create a packet with an empty buffer to receive data DatagramPacket packet = new DatagramPacket(buffer, buffer.length); // Create a socket to listen on the port. DatagramSocket socket = new DatagramSocket(port);
Receiving a Datagram in Java (cont)Receiving a Datagram in Java (cont)
for(;;) { // Wait to receive a datagram socket.receive(packet); // Convert the contents to a string s = new String(buffer, 0, 0, packet.getLength()); // And display them System.out.println("UDPReceive: received from " + packet.getAddress().getHostName() + ":" + packet.getPort() + ": " + s); } }
}
Critical Assessment of Socket Critical Assessment of Socket APIAPI
““good” things about sockets/TLI/winsock/Java network API:good” things about sockets/TLI/winsock/Java network API:
““bad” things about socket API:bad” things about socket API:
Novell Networks: Netware APINovell Networks: Netware API
Novell Networks: Netware APINovell Networks: Netware API adopts client-server paradigmadopts client-server paradigm
data transport: unreliable datagram (IPX) or connection-oriented reliable data transport: unreliable datagram (IPX) or connection-oriented reliable (SPX)](SPX)]
SAP: Service Advertising ProtocolSAP: Service Advertising Protocol used to advertise/request services used to advertise/request services
NCP: Netware Core ProtocolNCP: Netware Core Protocol used for Netware-defined client-server used for Netware-defined client-server interactionsinteractions
higher-level Netware-defined services:higher-level Netware-defined services: directory servicesdirectory services transaction supporttransaction support resource controlresource control lock serviceslock services
Novell SAP protocolNovell SAP protocol
allows clients/servers to locate advertise servicesallows clients/servers to locate advertise services
routers and gateways maintain table of known servicesrouters and gateways maintain table of known services
broadcast service info periodicallybroadcast service info periodically
different than Internet model: inside of Novell network (routers) different than Internet model: inside of Novell network (routers) have application-level infohave application-level info
SAP broadcasts IPX packets. Sample format:SAP broadcasts IPX packets. Sample format:
service types defined by Novell. service types defined by Novell.
Novell SAP protocol (cont)Novell SAP protocol (cont)
Possible operations:Possible operations:
servicetype
meaning
1 request for name/addr of all servers of specified type
2 response to type-1 request, periodic broadcast by server of its service,period broadcast by router of known services
3 same as type 1, but for nearest service
4 response to type-3 request
OSI Application Service EntitiesOSI Application Service Entities
applications can call (communicate with) predefined existing entities applications can call (communicate with) predefined existing entities that provide a servicethat provide a service
reliable transfer service element:reliable transfer service element: checkpointing and recoverycheckpointing and recovery: reliable data transfer in face of network connection : reliable data transfer in face of network connection
failuresfailures two-way alternating communication:two-way alternating communication: confirm transfer and receipt before confirm transfer and receipt before
sending next messagesending next message
commitment, concurrency and recoverycommitment, concurrency and recovery provides an atomic action: provides an atomic action: set of message exchanges and processing all guaranteed to execute to set of message exchanges and processing all guaranteed to execute to
completion or else as if no action had occurredcompletion or else as if no action had occurred nothing left dangling should network failnothing left dangling should network fail two phase commit: one master, many slavestwo phase commit: one master, many slaves
API: SummaryAPI: Summary
some API’s provide only low-level interface to some API’s provide only low-level interface to transport services: socket, winsock, TLItransport services: socket, winsock, TLI
other API’s provide higher-level services (e.g., other API’s provide higher-level services (e.g., transaction support, service advertising or request)transaction support, service advertising or request)
makes building applications easiermakes building applications easier
sockets the de facto standardsockets the de facto standard
FYI reading:FYI reading: winsock: http://www.sockets.com JAVA: http://java.sun.com