Answer / Request layer:
This level of abstraction has dropped all the details of all network layers, providing an interface like a stream of two-way transmission data. Java can accept the HTTP URL and complete the get command by the following command:
URL URL = New URL ("http://to.Post.to.com");
UrlConnection urlConnection url.openConnection ();
InputStream INPUT URLCONNECTION.GETINPUTSTREAM ();
... Read Stuff from Input ...
INPUT.CLOSE ();
C # Complete the same function via System.Net.WebRequest object:
WebRequest Request = WebRequestFactory.create
"http://to.post.to.com");
Stream Input Request.getResponse (). GetResponseSstream ();
... Read Stuff from Input ...
INPUT.CLOSE ();
Both languages hide the underlying socket creation HTTP protocol requirements, but provides the stream that programmers can use to send and receive data. Like the Stream class in the C #, the WebRequest class has a way to write a stream to write or read data from the middle, or a WebResponse object that can be read from the data.
Protocol layer:
For familiar java.net.socket, Java programmers should be very familiar with system.net.sockets.tcpclient, because both are very similar. Since the programmer does not need to process the implementation of the socket, it is only necessary to return a stream available, so the APIs and functions of the two are very similar.
In Java, you can use the following command to implement a very simple Telnet client:
Socket Telnet = New Socket ("Telnet.host.com", 23);
OutputStream output = telnet.getOutputStream ();
InputStream INPUT = Telnet.getinputStream ();
Two streams can be used to connect to Telnet.host.com. The same functionality can be implemented in C # in the same style:
TcpClient Telnet = New TcpClient ("Telnet.host.com", 23);
Stream telnetStream = telnet.getStream ();
StreamWriter Output = New Streamwriter (TelnetStream);
StreamReader Input = New StreamReader (TelnetStream);
The receiving TCP / IP connection is almost the same in two languages. In Java, you can build it with the following command and receive TCP / IP connections:
Serversocket Server = New Serversocket (23);
Socket Accept = Server.Accept ();
The implementation in C # is as follows:
TCPListener Server = New TCPListener (23);
Server.Start ();
Socket Accept = Server.Accept ();
In two languages, each received socket requires separate processing. In Java, a better method (until Java 1.4) is a thread to each received socket. In C # can also be used as the same processing, the Socket class provides an interface that uses event-driven events with the SELECT method. (Programming in the event driving mode has exceeded the scope of this article.) Original socket layer:
The content of this part is unfamiliar for most Java programmers. Because two classes of java.net.socket and java.net.Datagramsocket, only the programmers using Java programming languages have almost no need to understand the implementation of the Berkeley socket. The flow function in Java can also be achieved by working on the Berkeley Socket class.
At this point, we have used the commands in the C # to achieve most of the abstraction of the I / O and the network in Java.