Download presentation
Presentation is loading. Please wait.
Published byBethany Beatrix Holt Modified over 9 years ago
1
Networking with Java
2
Basic Concepts A Network exists when two or more computers are connected such that they can communicate data back and forth. There are two basic models for programs that use networking – “Peer-to-Peer” or “Client- Server”. We will focus upon the Client Server Model.
3
Client - Server Many networking programs require that the user start up a program (known as the Client) and connect to some computer on the internet (known as the Server). A classic example of this model would be when you start up your favorite web browser (a client) and connect to some website’s web Server.
4
Client-Server The basic idea behind this model is that the Server has access to some resource that is unavailable to the client directly. The client requests some service from the Server, the server carries out the request, and the server then returns the results to the client. In our web-example, the client/browser wishes to access a web-page that is not on the client computer. It contacts the web server and requests the web page. The web server locates the page and returns its contents to the client/browser.
5
Exceptions Whenever we deal with things like networks in our Java programs we need to realize that strange things can occur that have nothing to do with our program. That is, errors can pop up that are not the result of our having written bad code. For example, our internet connection may temporarily be down or the server we are trying to access may be down etc. When such an event occurs we call it an “exception”. Often, Java requires us to “handle” such exceptions.
6
Try Catch Statement Java requires us to protect our program against certain types of exceptions. In particular, when we deal with Networking we will need to use a Try-Catch statement which watches for certain exceptions and catches them so that our program can handle them in a clean way.
7
Try Catch Syntax try { body-code } catch (exception-classname variable-name) { handler-code } Body-code consists of the operations we are trying to execute that might cause an exception to occur. Handler-code consists of what we wish to do if such an exception occurs.
8
Internet Addresses In order to write a program that works on the Internet, we need to understand Internet addressing. Every machine on the Internet has an Internet address. Such addresses consist of 4 integers separated by ‘.’s as in 198.206.243.14 Each of the 4 integers will be between 0 and 255.
9
Internet Addresses The problem with these addresses is that humans don’t do real well trying to remember lots of numbers. Because of this, important, computers on the internets (usually servers) are given Domain Names. For example, www.coke.com, or www.nku.edu, or espn.go.com and so on. These are easier for humans to remember.www.coke.com www.nku.edu
10
Ports Ever wonder how your computer can keep track of so many internet applications at once? I mean you might be running a browser, listening to some streaming music, IMing a friend all at the same time! The answer is PORTS. Each computer on the Internet has a variety of different ports that specific programs listen to for communication. For example, your browser listens to port 80. Other applications may listen to say port 23 or port 55 etc.
11
Network Programs It is important to remember that Networking applications will actually consist of TWO programs. The Client program and the Server program. These programs will need to communicate back and forth in order for the entire application to function properly.
12
Sockets The most common way of implementing client- server communication is via SOCKETS. The easiest way to understand a Socket is to think of it as a sort of phone. That is, if you wished to carry on a conversation with a friend at a distance each of you would require a phone which you would use to send information (speak into the phone) and receive information (listen to your friend’s speech).
13
Sockets The vast majority of programming languages supply programmers with a library of code to implement Sockets. This includes Java. Our programs will allow the Client program to open a socket and connect to the Server program. Once connected, the client and server will be able to “speak” to each other simply by writing to the socket (to speak) or reading from the socket (to listen).
14
Sockets – The Server The Server portion of a network application needs to “bind” itself to a specific Port #. This simply means that it is “listening” at that port for someone to try to connect to it. Once a connection request arrives, the Server will have a Socket with which to communicate with the Client on the other end of the line.
15
Sockets – The Client The Client portion of your program will initiate contact with the Server. It will need to know either the IP-Address or the Server or its Domain Name as well as the correct Port # in order to connect. Once connected, it too will have a socket with which to communicate back and forth with the Server.
16
ServerSocket - Java In Java the way the Server “binds” itself to a particular Port # is by creating an object of class ServerSocket. It passes the port number it wishes to bind to to the constructor for ServerSocket. In the example below we create a ServerSocket variable named myserversocket and bind it to Port #4567. ServerSocket myserverSocket = new ServerSocket(4567);
17
ServerSockets Please note that a ServerSocket is NOT a Socket!!!! It is simply a mechanism that allows the Server to listen at a particular port for a connection from a client. Clients always initiate contact. Servers always wait for a connection request.
18
Servers – Obtaining a Socket Once a Server has created a ServerSocket and is bound to a specific port # it must wait for some client to come “knocking”. In Java, the server does this through the accept( ) method of the ServerSocket object as shown below. Socket mysocket = myserverSocket.accept();
19
Sockets Socket mysocket = myserverSocket.accept(); When accept() is called the server program will wait until a client tries to connect. Once a client connects, the accept routine will return an actual Socket objet that can be used by the server to communicate back and forth to the client. In our example above, all communication will occur through the variable mysocket and NOT through the variable myserverSocket which was only needed to bind the Server to the correct Port.
20
Closing up shop - Server Once the Server has finished processing all of the clients requests it should close the socket it was using for communication… mysocket.close( ); When the server is finished completely and is no longer interested in connecting with anymore clients it should close the ServerSocket object as well with…. myserverSocket.close( );
21
Sockets – the Client At this point we have seen that the Server is pretty simple. Create a ServerSocket object and bind it to a specific port #, execute the accept( ) method on the ServerSocket object and wait for a client, use the Server object returned by the accept( ) method to communicate with the client, close everything up when it is finished. Turns out the Client is actually easier….
22
Sockets- Clients Clients do not bind themselves to ports and therefore they do not need to worry about the ServerSocket class. To open a Socket connection with the server all the client needs to do is create a new Socket object and pass it the internet address of the Server and the port number the server is listening on as in…. Socket csocket = new Socket(“127.0.0.1", 4567);
23
java.net.* All of the Socket and ServerSocket classes and method are found in the java.net.* library so ALWAYS make sure you import it into any client-server program you write!!! import java.net.* ;
24
Sample Server – Binding Server to Port ServerSocket MyService; try { MyService = new ServerSocket(4567); } catch (IOException e) { System.out.println(“UNABLE TO BIND SERVER ” + e ); }
25
Server Accepts a Connection and creates a Socket Socket serviceSocket = null; try { serviceSocket = MyService.accept(); } catch (IOException e) { System.out.println(“No SOCKET! “ + e); }
26
Client opens a Socket to Server Socket MyClient; try { MyClient = new Socket(”127.0.0.1", 4567); } catch (IOException e) { System.out.println(e); } NOTE – I keep using IP adress 127.0.0.1 this ALWAYS means “the machine I am running on!!!” So I am assuming in our examples that we are running the client and server on the same machine!
27
Sending/Receiving It sucks so I’ll just show it to you and we will go from there…..
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.