Presentation is loading. Please wait.

Presentation is loading. Please wait.

Network Programming. The biggest difficult part in networking programming lies in understanding networking not in using java networking package. Since.

Similar presentations


Presentation on theme: "Network Programming. The biggest difficult part in networking programming lies in understanding networking not in using java networking package. Since."— Presentation transcript:

1 Network Programming

2 The biggest difficult part in networking programming lies in understanding networking not in using java networking package. Since this is not a networking class, only the basic concept of networking will be explained here. Networking is all about sending bits from one place to another. To understand networking, we need to understand various network protocols, which are agreements between two hosts(eg. Computer). TCP/IP is a protocol stack(two protocols are layered), which is used in the Internet.

3 How data is transferred over the Internet Let’s suppose a chunk of data is sent from site A to site B. The following is how data is delivered over the Internet. –Data is broken into smaller pieces called packet. –Site A sends one packet at a time. –Site B receives one packet at a time. –Site B sorts all received packets into proper order and restore original data by merging all packets. There are two issues we need to consider: –How each packet finds its way to the destination. (Network) –What if some packets are lost somewhere in the Internet. (Transport)

4 Internet Protocol(IP) A protocol is an agreement between two points (works as a pair). Internet Protocol(IP) is a protocol that specifies the way to transfer data between two hosts on the Internet. We call this data as IP packet. The source and destination of a packet is specified by an address called IP address.(eg. 134.121.34.35) A packet travels through a number of routers(or gateway) before it reaches its destination. A router is a door to a certain local network. The internet is an interconnection among routers. When a router receives a packet, it sends the packet inside the local network or relays it to another rounter according to the destined IP address.

5 Transport Control Protocol(TCP) TCP takes charge of sending/receiving the given data. Its role can be classified as: –Check every packet has successfully arrived. –If notified that some packets are missing, it resends them. –Assemble the original data from a bunch of packets TCP connection is very reliable in a sense that both sender and receiver work together to make sure every data is successfully delivered. Therefore TCP is called connection oriented even though there is no physical one-to-one connection. User Datagram Protocol(UDP) is another transport layer protocol, which is not connection oriented. (discussed later)

6 The core of networking is layering protocols such that protocol at each level only talks to the counterpart protocol at the same level. Consider the following diagram. Application(your java program) only talks to its counter part application, after setting up the connection channel of you choice(TCP or UDP). Big Picture

7 Demultiplexing with Port Number All data destined to a computer arrives through the same connection(ie through the same IP address). However the data may be sent to different applications. TCP(or UDP) forward a received packet to the destined application through port number.

8 Socket Socket is a programming model that encapsulates all networking details. A socket is an end point of network communication link between two application program running on the network. A socket is ready to use only when it is bound to a specific port number. (or assigned) For programmers, a socket is viewed as: IP Address + port number

9 Client/Server Rendezvous Generally, a server runs on a specific computer and has a socket that is bound to specific port. The server is listening(monitoring) to the socket for a client to make a connection. For the client to get connected to the server, client application should know the hostname(or IP address) of the machine where the server runs and the port number bound to it. In short, a client tries to make a connection to a server through IP address and port number. If everything goes well between two, socket connection is established.

10 Server/Client Socket Server side socket is used only to open a connection with a client. Notice that its port number is fixed and can not be used by only one client. When a connection is established(or server accepts the connection request), a new socket is created at server side. This is also when the client side socket is created. Now both client and server have the dedicated sockets, they can enjoy talking to each other with TCP/IP stack being a humble servant.

11 Socket/ServerSocket Class Socket/ServerSocket class in java.net package implement client/server side socket respectively. To create a server socket, ServerSocket ssock = new ServerSocket(int port); Socket nsock = ssock.accept(); // nsock is created by accept() to serve the client. It is at this moment // when the corresponding client socket is created. To create a client side socket (or connect to the server), Socket csock = new Socket(String hostname, int port); // csock is created when connection request is accepted by ssock

12 Reading/Writing from/to a Socket Socket is bidirectional connection, which means both ends can read/write data from/to a socket simultaneouly. Now recall what we have learned from java.io package. Everything coming into or going out from a java application is through stream. Both Socket/ServerSocket provides two methods with which we can obtain both InputStream and OutputStream. –getInputStream() –getOutputStream() As you can easily guess, these streams are connected down to TCP/IP stack.

13


Download ppt "Network Programming. The biggest difficult part in networking programming lies in understanding networking not in using java networking package. Since."

Similar presentations


Ads by Google