Project23 - SparePartsManagement

Download as pdf or txt
Download as pdf or txt
You are on page 1of 9

Lab – Socket Programming

Follow-Up from Previous Lab


In the previous lab, we have covered the following topics:
1. Client/Server Communication
2. Hosts Identification and Service Ports
3. Sockets and Socket-based Communication
4. Socket Programming and Java.net Classes
5. TCP/IP Socket Programming
a. Simple Server and Client implementation in Java
6. UDP Socket Programming
At the end of the previous lab, you were asked to perform some lab activities which were based
on the code we had covered. It is expected from you that you remember the activities performed
by you. If you remember, it will help you a lot in today’s lab. In the previous lab, we have covered
very basic socket programming. In today’s lab, we are going to implement few more Socket
Programming applications for better understanding.

Math Server
It is time to implement a more comprehensive network application by using the socket
programming APIs you have learned in the pervious lab. A sample math client-server interaction
demonstrating online math server that can perform basic math operations.
The basic math interface is shown as follows:
The implementation of this interface is not related to any network operations. The following code
shows a very simple implementation of this interface:
The implementation of the MathServer (Main) is quite straightforward, which looks pretty similar
to the echo server mentioned previously. The difference is that the MathServer have to reconsider
the specific protocol defined by the math server and client communication. The program uses a
very simple protocol operator:first_value:second_value. It is the math server’s responsibility to
understand this protocol and delegate to the proper methods such as add, sub, mul or div.
A test client program that can access the math server is shown below:
URL Encoding
It is very important that the Web can be accessed via heterogeneous platforms such as Windows,
Linux, or Mac. The characters in the URL must come from a fixed subset of ASCII in order to
maintain the interoperability between various platforms. Specifically, the capital letters A-Z, the
lowercase letter a-z, the digits 0-9 and the punctuation characters. Encoding is very simple, any
characters that are not ASCII numerals, letters, or the punctuation marks allowed are converted
into bytes and each byte is written as a percentage sign followed by two hexadecimal digits. For
example, the following program helps encode a query string if non-ASCII characters are present.

The output of this program will be:


http://www.google.com.pk/search?newwindow=1&q=Zara+Hamid+%26+COMSATS&
It can be seen that the whitespace has been encoded as “+” and the “&” has been encoded as “%26”
the percentage sign following by its byte value. Other characters remain the same. These
conversions have been performed by the URLEncoder class, which is part of the Java base class
library and provides facilities for encoding strings (URLs) in different formats. There is also an
URLDecoder class that performs the reverse process. These two classes are useful not only for
URLs but also for managing HTML form data.
Writing and Reading Data via URLConnection
Besides the socket and datagram introduced in previous section, java.net package provides another
useful class that can be used to write and read data between the server and the client:
URLConnection. It is not possible to instantiate the URLConnection class directly. Instead you
have to create the URLConnection via the URL object.
URLConnection connection = new URL(“www.yahoo.com”).openConnection();
Then the URLConnection class provides the getInputStream and getOutputStream methods that
are very similar to the getInputStream and getOutputStream provided by the Socket class. The
following example shows how to use the URLConnection and the URLEncoder class to send
queries to the Yahoo search engine.
The program first creates an encoded query string that can be used by the web application, then it
utilizes the URLConnection class to send/receive data from the Yahoo Search Engine. The output
will be the entire HTML page.

Lab Activities
1. Write a URL-based program that pulls content from https://www.comsats.edu.pk/
2. Rewrite the math server application, instead of using TCP socket, use the UDP datagram
socket.
3. Try to run a server program on one machine and the client program on another machine
and see if it works. It works actually, all you have to do is to find out how.
4. Currently the server is closed as soon as it responds to the client. Can you make it like the
real word servers? The server should never be down, and the clients can be many.

You might also like