Part 3 1supp Java Socket Details supplemental materials

  • Slides: 19
Download presentation
Part 3. 1(supp) Java Socket Details (supplemental materials) (Client-Server Concept, Use of Protocol Ports,

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 1

Creating a Socket l Application calls socket function desc = socket(protofamily, type, proto); l

Creating a Socket l Application calls socket function desc = socket(protofamily, type, proto); l l l 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) l To create a server socket: Server. Socket s. Socket = new Server. Socket(int port) l FALL 2005 CSI 4118 – UNIVERSITY OF OTTAWA 2

Socket Functionality l l Socket completely general Can be used l l l By

Socket Functionality l l Socket completely general Can be used l l l 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 3

Socket Operations l Close l l Terminate use of socket Permanent close(socket); In Java,

Socket Operations l Close l l Terminate use of socket Permanent close(socket); In Java, use socket. close(); or s. Socket. close(); FALL 2005 CSI 4118 – UNIVERSITY OF OTTAWA 4

Socket Operations l Bind Specify protocol port for a socket Specify local IP address

Socket Operations l Bind Specify protocol port for a socket Specify local IP address for a socket Can use INADDR_ANY for any IP address l l l bind(socket, localaddr, addrlen); l FALL 2005 In Java, use socket. bind(Socket. Address endpoint) to bind the socket to a IP address and port. CSI 4118 – UNIVERSITY OF OTTAWA 5

Generic Address Format struct sockaddr { u_char sa_len; /*length of address*/ u_char sa_family; /*family

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 6

TCP/IP Address Format struct sockaddr_in { u_char sin_len; /*length of address*/ u_char sin_family; /*family

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 7

Socket Operations (continued) l Listen l Used by server l Prepares socket to accept

Socket Operations (continued) l Listen l Used by server l Prepares socket to accept incoming connections listen(socket, queuesize); l Accept l Used by server l Waits for next connection and returns new socket newsock = accept(socket, caddrlen); l FALL 2005 In Java, method accept() in java. net. Server. Socket class listens for a connection and accepts it. CSI 4118 – UNIVERSITY OF OTTAWA 8

Socket Operations (continued) l Connect l l Used by client Either Performs a TCP

Socket Operations (continued) l Connect l l Used by client Either Performs a TCP connection l Fully specifies addresses for UDP l connect(socket, saddrlen); l FALL 2005 In Java, connect(Socket. Address server) to connect the socket to a specific server. CSI 4118 – UNIVERSITY OF OTTAWA 9

Two Purposes of the Connect Function The connect function, which is called by clients,

Two Purposes of the Connect Function The connect function, which is called by clients, has two uses. With connectionoriented 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 10

Socket Operations (continued) l Send, sendto, and sndmsg Transfer outgoing data from application l

Socket Operations (continued) l Send, sendto, and sndmsg Transfer outgoing data from application l send(socket, data, length, flags); sendto(socket, data, length, flags, destaddr, addrlen); sendmsg(socket, msgstruct, flags); l FALL 2005 In java, java. io. Print. Stream is used to send data. e. g. method println() CSI 4118 – UNIVERSITY OF OTTAWA 11

Format of msgstruct { struct sockaddr *m_saddr; /*dest address*/ struct datavec *m_dvec; /*message (vector)*/

Format of 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 12

Socket Operations (continued) l Recv, recvfrom, and recvmsg Transfer incoming data to application l

Socket Operations (continued) l Recv, recvfrom, and recvmsg Transfer incoming data to application l recv(socket, buffer, length, flags); recvfrom(socket, buffer, length, flags, senderaddr, saddrlen); recvmsg(socket, msgstruct, flags); l FALL 2005 In Java, java. io. Buffer. Reader used to receive data. E. g. method readline() CSI 4118 – UNIVERSITY OF OTTAWA 13

Java Code Equivalent to Example in Chapter 30 FALL 2005 CSI 4118 – UNIVERSITY

Java Code Equivalent to Example in Chapter 30 FALL 2005 CSI 4118 – UNIVERSITY OF OTTAWA 14

Example Server Java Code import java. io. *; import java. net. *; public class

Example Server Java Code import java. io. *; import java. net. *; public class server{ public static void main(String[] args) { // variable declaration int visits = 0; Server. Socket srv = null; Print. Stream os; Socket client. Socket = null; String msg = ""; FALL 2005 //check command-line argument. . . //(omited) CSI 4118 – UNIVERSITY OF OTTAWA 15

/* create socket */ try { srv = new Server. Socket(Integer. parse. Int(args[0])); }

/* create socket */ try { srv = new Server. Socket(Integer. parse. Int(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. client. Socket = srv. accept(); } catch(IOException e) { System. out. println("accept failed"); System. exit(1); } FALL 2005 CSI 4118 – UNIVERSITY OF OTTAWA 16

try{ os = new Print. Stream(client. Socket. get. Output. Stream()); visits++; msg = "This

try{ os = new Print. Stream(client. Socket. get. Output. Stream()); visits++; msg = "This server has been contacted " + visits + " times"; os. println(msg); //sends msg os. close(); client. Socket. close(); } catch(IOException e) { System. out. println(e); } } FALL 2005 CSI 4118 – UNIVERSITY OF OTTAWA 17

Example Client Java Code import java. io. *; import java. net. *; public class

Example Client Java Code import java. io. *; import java. net. *; public class client { public static void main(String[] args) { //variable declaration Socket socket = null; Buffered. Reader in = null; //check command-line argument. . . //(omited) FALL 2005 CSI 4118 – UNIVERSITY OF OTTAWA 18

try { /* create a socket and connect to the specified server. */ socket

try { /* create a socket and connect to the specified server. */ socket = new Socket(args[0], Integer. parse. Int(args[1])); } catch (Unknown. Host. Exception e) { System. exit(1); } catch (IOException e) { System. out. println("connect failed"); System. exit(1); } /* read data from socket and write to user's screen. */ try{ in = new Buffered. Reader(new Input. Stream. Reader(socket. get. Input. Stream())); System. out. println(in. read. Line()); in. close(); socket. close(); } catch(IOException e) { System. out. println(e); FALL 2005 CSI 4118 – UNIVERSITY OF OTTAWA }}} 19