Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What's the difference between Java and C#

2025-04-07 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

Editor to share with you what is the difference between Java and C#, I believe most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's learn about it!

Both Java and C # provide different abstraction layers for the network, and programmers can use different network interfaces to complete the operation of the network.

Java C#

Reply / request:

Java.net.URL and java.net.URLConnection.

System.Net.WebRequest .

Agreement:

The TCP/IP protocol uses java.net.Socket and java.net.ServerSocket

The UDP protocol uses java.net.DatagramSocket and java.net.MulticastSocket.

The CP/IP protocol uses System.Net.Sockets.TCPListener and System.Net.Sockets.TCPClient

UDP protocol uses TSystem.Net.Sockets.UDPClient

Original socket layer:

No.

System.Net.Sockets.Socket

The reply / request layer can be used for requests of the HTTP class, where one end starts a connection, sends some bytes of data, and then stops, waiting for some bytes that the other party sends back as a reply. The protocol layer is more useful for more flexible operations such as streams. For most Java programmers, there is no need for direct control of sockets unless you need to complete very high-performance network operations. If necessary. C # still provides the ability to control the original Berkeley socket.

Reply / request layer:

This level abstracts all the details of the network layer and provides a stream-like interface that can transmit data in both directions. Java can accept HTTP URL and complete the GET command with 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 # performs the same function through the System.Net.WebRequest object:

WebRequest request= WebRequestFactory.Create (

"http://to.post.to.com")

Stream input request.GetResponse () .GetResponseStream ()

... Read stuff from input...

Input.Close ()

Both languages hide the underlying socket creation HTTP protocol requirements, but instead provide streams that programmers can use to send and receive data. Like the Stream class in C #, the WebRequest class has methods that asynchronously get streams to write or read data from, or WebResponse objects from which data can be read.

Protocol layer:

Java programmers who are familiar with java.net.Socket should be very familiar with System.Net.Sockets.TCPClient because the two are very similar. Because programmers do not have to deal with the implementation of sockets and only need to return a stream that can be used, the API and functionality of both are very similar.

A very simple telnet client can be implemented in Java using the following command:

Socket telnet= new Socket ("telnet.host.com", 23)

OutputStream output= telnet.getOutputStream ()

InputStream input= telnet.getInputStream ()

Both streams can be used in the connection with telnet and telnet.host.com. Programs with the same functions can be implemented in C# in the same style in the following ways:

TCPClient telnet= new TCPClient ("telnet.host.com", 23)

Stream telnetStream= telnet.GetStream ()

StreamWriter output = new StreamWriter (telnetStream)

StreamReader input = new StreamReader (telnetStream)

The receive TCP/IP connection is almost the same in both languages. In Java, you can establish and receive TCP/IP connections with the following command:

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 both languages, each received socket needs to be processed separately. In Java, a better approach (until Java 1. 4) is to generate a thread for each received socket. The same can be done in C #, where the Socket class provides an interface to use event-driven methods with select methods. (programming sockets in an event-driven manner is beyond the scope of this article. )

Original socket layer:

This section is new to most Java programmers. Because it is abstracted by the java.net.Socket and java.net.DatagramSocket classes, programmers who only use the Java programming language need little knowledge of the implementation of Berkeley sockets. By manipulating the Berkeley Socket class, you can also implement the streaming function in Java.

So far, we have implemented most of the functions abstracted in Java by using the commands in C# to operate on ━━ O and the network.

The above is all the content of the article "what's the difference between Java and C#". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report