Overview
In this task, you will learn how to:
Create TCP sockets, and use them to send and receive data Design the client side of client-server communication
Deal with errors that can happen during the communication
The task is a programming assignment to implement a simple TCP client, as a Java class. The class is called TCPClient, and works in a straight-forward manner:
1. Open a TCP connection to a server at a given host address and port number.
2. Send data to the server.
3. Take the data that the server sends back in response, and return that as the result.
The TCPClient Class
To use the TCPClient class, an instance should first be created. The constructor has no parameters:
TCP provides the communication service of bidirectional transfer of streams of bytes. Hence, the TCPClient class has a method to send bytes to a server and receive bytes in return. This method is called askServer, and its specification is as follows:
The hostname parameter is the domain name of the server to which the client should connect. The
port parameter is the TCP port number on the server. The bytesToServer parameter is a byte array with the data to send to the server. So, for instance, to connect to KTH's web server and send the data in the byte array "webdata", a program could do the following:
Task
Your task is straight-forward: implement the TCPClient class.
To help you with development and testing, you will also get an application program that uses the TCPClient class. The application is called TCPAsk, which is where the name of this assignment comes from.
Instructions and Tips
Socket I/O and Wrappers
You should use the Socket class (https://docs.oracle.com/javase/7/docs/api/java/net/Socket.html) to create the client's socket. In Java, InputStream (https://docs.oracle.com/javase/7/docs/api/java/io/InputStream.html) and OutputStream (https://docs.oracle.com/javase/7/docs/api/java/io/OutputStream.html) are the basic classes for I/O,
which read and write "raw" binary data in the form of byte arrays. The slides from Kurose-Ross_5e (https://canvas.kth.se/courses/31590/files/5060007/download?download_frd=1) suggest to use additional "wrappers" around the socket's InputStream and OutputStream. Those wrappers add data processing and buffering to the basic InputStream and OutputStream, and automatically convert between text and binary data. Using wrappers like that is common in Java I/O. However, in this assigned we want to transmit binary data transparently without additional data processing, so we have no use for wrappers. Furthermore, it would not be a general solution; it would only work for text- based applications, and not for binary transfers.
It is requirement in this assignment that you use InputStream and OutputStream classes for "raw" binary I/O with byte arrays. You are not allowed to use wrappers such as InputStreamReader/OutputStreamWriter and BufferedReader/BufferedWriter. See the slides from the Socket Programming Lecture (https://canvas.kth.se/courses/31590/files/5153647/download? download_frd=1) for more information, and for examples of how to use the Socket class (https://docs.oracle.com/javase/7/docs/api/java/net/Socket.html) for byte I/O.
Receiver Buffers
TCPClient's askServer method waits until all data has been received from the server before it returns. In other words, askServer reads data from the TCP connection until the connection is closed. Then the question is, how does askServer store all that data internally? Clearly askServer needs to have a receive buffer where it stores the data it receives from the server. A simple (but
incorrect) solution would be to use a byte array of fixed size for storing the data, and make that byte array so large that it is highly unlikely that it will overflow. This is not a good solution, for several reasons. First, it means there is an assumption built into the program about how much data the server could send. To design a program in that way that is not good programming. Second, the program would occupy a lot of memory resources on the computer, resources that the program does not use, so it is not efficient usage of the computer.
DescriptionIn this final assignment, the students will demonstrate their ability to apply two ma
Path finding involves finding a path from A to B. Typically we want the path to have certain properties,such as being the shortest or to avoid going t
Develop a program to emulate a purchase transaction at a retail store. Thisprogram will have two classes, a LineItem class and a Transaction class. Th
1 Project 1 Introduction - the SeaPort Project series For this set of projects for the course, we wish to simulate some of the aspects of a number of
1 Project 2 Introduction - the SeaPort Project series For this set of projects for the course, we wish to simulate some of the aspects of a number of