CIS 225 Advanced Java Networking Y Daniel Liang

  • Slides: 10
Download presentation
CIS 225 – Advanced Java Networking Y. Daniel Liang Introduction to Java Programming 1

CIS 225 – Advanced Java Networking Y. Daniel Liang Introduction to Java Programming 1

Overview n Introduction n Client / Server Computing Y. Daniel Liang Introduction to Java

Overview n Introduction n Client / Server Computing Y. Daniel Liang Introduction to Java Programming 2

Introduction n Networking is tightly integrated in Java provides socket-based communication n A socket

Introduction n Networking is tightly integrated in Java provides socket-based communication n A socket is an abstraction that facilitates client/server communication n Java treats socket communication using the same semantics as file I/O Y. Daniel Liang Introduction to Java Programming 3

Introduction n Networking is tightly integrated in Java provides socket-based communication n A socket

Introduction n Networking is tightly integrated in Java provides socket-based communication n A socket is an abstraction that facilitates client/server communication n Java treats socket communication using the same semantics as file I/O Y. Daniel Liang Introduction to Java Programming 4

Introduction (cont. ) n Java supports stream and datagram sockets n Stream sockets use

Introduction (cont. ) n Java supports stream and datagram sockets n Stream sockets use TCP (Transmission Control Protocol) for data transmission n Datagrams sockets use UDP (User Datagram Protocol) for data transmission Y. Daniel Liang Introduction to Java Programming 5

Client / Server Computing n Network programming involves a server and one or more

Client / Server Computing n Network programming involves a server and one or more clients n The server is passive, it waits for connection requests from clients n Once a connection is made, they communicate via sockets n The server must be running when the client starts Y. Daniel Liang Introduction to Java Programming 6

Client / Server Computing To establish a server, you need to create a server

Client / Server Computing To establish a server, you need to create a server socket and attach it to a port n The port is where the server listens for connections n The port identifies the TCP service on the socket n Port numbers between 0 and 1023 are reserved n Socket s = new Server. Socket(port); Y. Daniel Liang Introduction to Java Programming 7

Client / Server Computing n After a socket is created, the server listens for

Client / Server Computing n After a socket is created, the server listens for connections Socket client. Con = s. accept(); n The client then can issue a connection request n The client request can use either an IP address or a DNS name Socket server. Con = new Socket(“ 123. 14. 12. 10”, 8000); Y. Daniel Liang Introduction to Java Programming 8

Client / Server Computing After the server accepts the client connection request, I/O streams

Client / Server Computing After the server accepts the client connection request, I/O streams are used for communication n The get. Input. Stream() and get. Output. Stream() methods are used to get streams on a socket n Input. Stream from. Server = server. Con. get. Input. Stream(); Output. Stream to. Server = server. Con. get. Input. Stream(); Y. Daniel Liang Introduction to Java Programming 9

Server Client int port = 8000; String host = “localhost”; Data. Input. Stream in;

Server Client int port = 8000; String host = “localhost”; Data. Input. Stream in; Data. Output. Stream out; Data. Input. Stream in; Server. Socket server; Data. Output. Stream out; Socket socket; server = new Server. Socket (port); socket = server. accept(); Client Connection Request Socket socket; socket = new socket(host, 8000) in = new Data. Input. Stream (socket. get. Input. Stream()); out = new Data. Output. Stream (socket. get. Output. Stream()); I/O Streams System. out. println(in. read. Double()); out. write. Double(a. Number); System. out. println(in. read. Double());