Title: Part 3.1(supp)
1Part 3.1(supp)
- Java Socket Details (supplemental materials)
- (Client-Server Concept,
- Use of Protocol Ports, Socket API)
2Creating a Socket
- Application calls socket function
- OS returns descriptor for socket
- Descriptor valid until application closes socket
or exits - Common protofamily PF_INET,
type SOCK_STREAM or SOCK_DGRAM - In Java, to create a client socket
- Socket socket new Socket(string host, int port)
- To create a server socket
- ServerSocket sSocket new ServerSocket(int port)
desc socket(protofamily,type,proto)
3Socket Functionality
- Socket completely general
- Can be used
- By client
- By server
- With a CO transport protocol
- With a CL transport protocol
- To send data, receive data, or both
- Large set of operations
4Socket Operations
- Close
- Terminate use of socket
- Permanent
- In Java, use socket.close()
- or sSocket.close()
close(socket)
5Socket Operations
- Bind
- Specify protocol port for a socket
- Specify local IP address for a socket
- Can use INADDR_ANY for any IP address
- In Java, use socket.bind(SocketAddress endpoint)
to bind the socket to a IP address and port.
bind(socket,localaddr,addrlen)
6Generic Address Format
- struct sockaddr
-
- u_char sa_len /length of address/
- u_char sa_family /family of address/
- char sa_data14 /address itself/
-
7TCP/IP Address Format
- struct sockaddr_in
-
- u_char sin_len /length of address/
- u_char sin_family /family of address/
- u_short sin_port /protocol port number/
- struct in_addr sin_addr /IP address/
- char sin_zero8 /not used(set to zero)/
-
8Socket Operations (continued)
- Listen
- Used by server
- Prepares socket to accept incoming connections
- Accept
- Used by server
- Waits for next connection and returns new socket
- In Java, method accept() in java.net.ServerSocket
class listens for a connection and accepts it.
listen(socket,queuesize)
newsock accept(socket,caddr,caddrlen)
9Socket Operations (continued)
- Connect
- Used by client
- Either
- Performs a TCP connection
- Fully specifies addresses for UDP
- In Java, connect(SocketAddress server) to connect
the socket to a specific server.
connect(socket,saddr,saddrlen)
10Two Purposes of the Connect Function
- The connect function, which is called by clients,
has two uses. With connection-oriented
transport, connect establishes a transport
connection to a specified server. With
connectionless transport, connect records the
servers address in the socket, allowing the
client to send many messages to the same server
without specifying the destination address with
each message.
11Socket Operations (continued)
- Send, sendto, and sndmsg
- Transfer outgoing data from application
- In java, java.io.PrintStream is used to send
data. e.g. method println()
send(socket,data,length,flags)
sendto(socket,data,length,flags, destaddr,addrle
n)
sendmsg(socket,msgstruct,flags)
12Format of msgstruct
struct msgstruct struct sockaddr m_saddr
/dest address/ struct datavec m_dvec
/message (vector)/ int mdvlength
/size of vector/ struct access m_rights
/access rights/ int m_alength /size of
access rights/
13Socket Operations (continued)
- Recv, recvfrom, and recvmsg
- Transfer incoming data to application
- In Java, java.io.BufferReader used to receive
data. E.g. method readline()
recv(socket,buffer,length,flags)
recvfrom(socket,buffer,length,flags, senderaddr,
saddrlen)
recvmsg(socket,msgstruct,flags)