Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Network Programming Introduction zBased on Classes in the java.net package zLecture focuses on: yTCP and UDP yNetwork programming basics yIdentifying.

Similar presentations


Presentation on theme: "1 Network Programming Introduction zBased on Classes in the java.net package zLecture focuses on: yTCP and UDP yNetwork programming basics yIdentifying."— Presentation transcript:

1 1 Network Programming Introduction zBased on Classes in the java.net package zLecture focuses on: yTCP and UDP yNetwork programming basics yIdentifying a machine yServers and Clients yPorts and sockets yData transfer using sockets

2 zLots of java programs use Transmission Control Protocol (TCP) zConnection-based (where java stream sockets provides continuous data stream), reliable, data streams will always get there. Also high overhead. We will focus on TCP/IP sockets. zUser Datagram Protocol (UDP) is not connection-based (connectionless service with datagram sockets allowing one message, an unreliable protocol which is much faster, but the message won’t always get there TCP and UDP

3 Network Programming Basics The classes in java.net : Java programs can use TCP or UDP to communicate over the Internet. The URL, URLConnection (for Web applications), Socket, and ServerSocket (client-server applications) classes all use TCP to communicate over the network. The DatagramPacket, DatagramSocket, and MulticastSocket classes are for use with UDP.

4 Network Programming Basics zHistorically error-prone, difficult, complex zJAVA has complete network package, java.net zI/O stream library works quite well for TCP/IP zThreading is also very useful and relatively easy here

5 Identifying a Machine zUniquely identify a machine from all the others in the world zIP (Internet Protocol) address that can exist in two forms:

6 Identify a Machine (Continue) ÀDNS(Domain Name Service) form xjava.sun.com Á“Dotted quad” form: x123.255.28.120 y static InetAddress.getByName() produces java object containing address

7 Servers and Clients zTwo machines must connect zServer waits for connection zClient initiates connection (create socket) zOnce the connection is made, server & client look identical

8 Testing w/o a network zFor testing your program, you can do it w/o network. (server & client on same machine) zLocalhost: the IP address for testing without a network zThree ways to identify:

9 Test w/o networking (Continue) zInetAddress y addr=InetAddress.getByName(null); zEquivalently: yInetAddress.getByName(“localhost”); zOr using the reserved IP number for the loopback: yInetAddress.getByName(“127.0.0.1”);

10 Port number zIP address isn’t enough to identify a unique server zmany servers can exist on one machine zport number: Unique in a Machine zNot a physical location, but a software abstraction to represent a service

11 Port number (Continue) zWhen set up client and server, you must specify IP address and port. zPort number range: 0 to 65,535 z1-1024 are reserved, others may be used.

12 Sockets z Software abstraction used to represent the connection between two machines z Socket is the actual 2-way connector.

13 Sockets z The following sequence establishes a socket from the client class InetAddress addr = InetAddress.getByName(null); Socket socket = new Socket(addr, 8080);

14 Sockets z At the server class, the following establishes the server port, and waits for a client request to establish a socket ServerSocket s = new ServerSocket(8080); Socket socket = s.accept();

15 Data transfer using sockets zOnce you have a Socket, you call getInputStream() and getOutputStream() to produce the corresponding InputStream and OutputStream objects zYou can convert these to readers and writers, wrap them in a BufferedReader or BufferedWriter and PrintWriter

16 Data transfer using sockets (continue) z At the server and client classes BufferedReader in = new BufferedReader( new InputStreamReader( socket.getInputStream())); PrintWriter out = new PrintWriter( new BufferedWriter( new OutputStreamWriter( socket.getOutputStream())),true);

17 Data transfer using sockets (continue) zFrom then on, it’s like reading and writing any other I/O stream! while (true) { String str = in.readLine(); if (str.equals("END")) break; System.out.println("Echoing: " + str); out.println(str); } Example: JabberClient JabberServerJabberClientJabberServer


Download ppt "1 Network Programming Introduction zBased on Classes in the java.net package zLecture focuses on: yTCP and UDP yNetwork programming basics yIdentifying."

Similar presentations


Ads by Google