Download presentation
Presentation is loading. Please wait.
1
CSI 4118 – UNIVERSITY OF OTTAWA
Part 3.1(supp) Java Socket Details (supplemental materials) (Client-Server Concept, Use of Protocol Ports, Socket API) FALL 2005 CSI 4118 – UNIVERSITY OF OTTAWA
2
CSI 4118 – UNIVERSITY OF OTTAWA
Creating 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); FALL 2005 CSI 4118 – UNIVERSITY OF OTTAWA
3
CSI 4118 – UNIVERSITY OF OTTAWA
Socket 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 FALL 2005 CSI 4118 – UNIVERSITY OF OTTAWA
4
CSI 4118 – UNIVERSITY OF OTTAWA
Socket Operations Close Terminate use of socket Permanent In Java, use socket.close(); or sSocket.close(); close(socket); FALL 2005 CSI 4118 – UNIVERSITY OF OTTAWA
5
CSI 4118 – UNIVERSITY OF OTTAWA
Socket 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); FALL 2005 CSI 4118 – UNIVERSITY OF OTTAWA
6
Generic Address Format
struct sockaddr { u_char sa_len; /*length of address*/ u_char sa_family; /*family of address*/ char sa_data[14]; /*address itself*/ } FALL 2005 CSI 4118 – UNIVERSITY OF OTTAWA
7
CSI 4118 – UNIVERSITY OF OTTAWA
TCP/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_zero[8]; /*not used(set to zero)*/ } FALL 2005 CSI 4118 – UNIVERSITY OF OTTAWA
8
Socket Operations (continued)
Listen Used by server Prepares socket to accept incoming connections Accept 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); FALL 2005 CSI 4118 – UNIVERSITY OF OTTAWA
9
Socket 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); FALL 2005 CSI 4118 – UNIVERSITY OF OTTAWA
10
Two 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 server’s address in the socket, allowing the client to send many messages to the same server without specifying the destination address with each message. FALL 2005 CSI 4118 – UNIVERSITY OF OTTAWA
11
Socket 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,addrlen); sendmsg(socket,msgstruct,flags); FALL 2005 CSI 4118 – UNIVERSITY OF OTTAWA
12
CSI 4118 – UNIVERSITY OF OTTAWA
Format 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*/ } FALL 2005 CSI 4118 – UNIVERSITY OF OTTAWA
13
Socket 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); FALL 2005 CSI 4118 – UNIVERSITY OF OTTAWA
14
Java Code Equivalent to Example in Chapter 30
FALL 2005 CSI 4118 – UNIVERSITY OF OTTAWA
15
Example Server Java Code
import java.io.*; import java.net.*; public class server{ public static void main(String[] args) { // variable declaration int visits = 0; ServerSocket srv = null; PrintStream os; Socket clientSocket = null; String msg = ""; //check command-line argument ... //(omited) FALL 2005 CSI 4118 – UNIVERSITY OF OTTAWA
16
CSI 4118 – UNIVERSITY OF OTTAWA
/* create socket */ try { srv = new ServerSocket(Integer.parseInt(args[0])); } catch (IOException e) { System.out.println(e); /* Main server loop - accept and handle requests */ while(true){ try {// listen for a connection from client and accept it. clientSocket = srv.accept(); catch(IOException e) { System.out.println("accept failed"); System.exit(1); FALL 2005 CSI 4118 – UNIVERSITY OF OTTAWA
17
CSI 4118 – UNIVERSITY OF OTTAWA
try{ os = new PrintStream(clientSocket.getOutputStream()); visits++; msg = "This server has been contacted " + visits + " times"; os.println(msg); //sends msg os.close(); clientSocket.close(); } catch(IOException e) { System.out.println(e); FALL 2005 CSI 4118 – UNIVERSITY OF OTTAWA
18
Example Client Java Code
import java.io.*; import java.net.*; public class client { public static void main(String[] args) //variable declaration Socket socket = null; BufferedReader in = null; //check command-line argument... //(omited) FALL 2005 CSI 4118 – UNIVERSITY OF OTTAWA
19
CSI 4118 – UNIVERSITY OF OTTAWA
try { /* create a socket and connect to the specified server. */ socket = new Socket(args[0], Integer.parseInt(args[1])); } catch (UnknownHostException e) { System.exit(1); } catch (IOException e) { System.out.println("connect failed"); } /* read data from socket and write to user's screen. */ try{ in = new BufferedReader(new InputStreamReader(socket.getInputStream())); System.out.println(in.readLine()); in.close(); socket.close(); catch(IOException e) { System.out.println(e); }}} FALL 2005 CSI 4118 – UNIVERSITY OF OTTAWA
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.