III. First Communications
To communicate between machines, Computer scientists have identified two network protocols commonly used today: Protocol TCP and protocol UDP. Fairly brief, the protocol TCP has been created for you to send data reliably over the network, including:
- ensuring that data arrives at destination, if failure of transmission, the sending computer must be made aware
- ensuring that data arrives out of order
- establishing a connection between machines
The protocol UDP adds very little to the protocol IP. Compared Protocol TCP, UDP is unreliable, it is unclear whether data arrive and it is not certain than the data arrive is in good order. TCP performs a large number of round trips, which has the disadvantage to lower connection speed. Today TCP is almost used all the time, except for trade with the packet loss is not large. Java offers several classes for handling these protocols so absolutely transparent. Class Socket based on the protocol TCP and can therefore transmit data reliably. the Class DatagramSocket in turn, depends on UDP.
III-A. Socket client side
Most operating systems provide an abstraction to handle data networks known as socket. Point for low-level, a socket behaves the same way a file (which is usually handled through what is called a file descriptor). Once a connection is established, the socket handling is similar to managing files. In java was introduced a class Socket to manipulate with higher-level abstraction of the operating system. There are two methods: getOutputStream () and getInputStream () which will allow you to manipulate data as a file. Java is the difference between socket client side (object allows you to connect to remote computers) and socket server side (object that can wait for connection requests from outside). Class Socket corresponds to a socket client class, ServerSocket corresponds to a socket server.
III-A-1. The Socket class
At first, we'll see how we connect to a server remote, how to send data and how to receive, I think this is fairly simple.
Code:
Socket soc = new Socket("www.cnet.com", 80);
//or
InetAddress add = InetAddress.getByName("www.cnet.com");
Socket s2 = new Socket(add, 80);
The previous code used to connect to a server named by its IP or by its host name on a particular port (the port number 80). The concept of port, which is purely virtual, has been defined at the layer TCP and UDP to afford not to mix the data sent to a person. This is done using two different ports for two treatments. It is also possible to reuse the class InetAddress.
We will now send data using a OutputStream.
Code:
Socket s = new Socket("www.cnet.com", 80);
String g = "GET / HTTP/1.1 \ n" +
"Host: www.cnet.com \ n \ n";
OutputStream str = s.getOutputStream();
str.write(g.getBytes());
For better management one can use BufferedInputStream and BufferedOutputStream
Classes BufferedInputStream and BufferedInputStream allow manipulate flows more efficiently standard networks. Classes buffered work for the user, in much the same way as non-buffered classes, these classes are inherited OutputStream and InputStream. It is possible to create using directly normal flows:
Here is an example
Code:
strm = s.getOutputStream();
istrm = s.getInputStream();
bin BufferedInputStream = new BufferedInputStream(strm);
BufferedInputStream bin2 = new BufferedInputStream(istrm);
bin.write(g.getBytes());
At this level you will realize that the code does not work. Indeed, as the output stream is buffered, it is not immediately sent to the server, to force the sending, it is necessary to use the method: flush Class BufferedInputStream. In all, we have:
Code:
ostrm = s.getOutputStream();
istrm = s.getInputStream();
bin BufferedInputStream = new BufferedInputStream(ostrm);
BufferedInputStream bin2 = new BufferedInputStream(istrm);
bin.write(g.getBytes());
bin.flush();
int bts = 0;
while((bts = bin2.read(b)) >= 0) {
System.out.System.out.println("We has received : " + bts + " bit");
System.out.System.out.println("Received : " + new String(b 0, bts));
}
bin.close();
bin2.close();
Bookmarks