. Comparison of socket mechanism between NET and Java

Source: Internet
Author: User
Tags comparison socket

Socket is a high-level interface based on TCP and UDP protocol, which defines the format of sending and receiving data. The socket used in the Java TCP Service is a flow mechanism, that is, for programmers, processing a socket requires only a stream to be fetched from the socket, and then the data can be sent and received as if the local stream were being processed.

For example:

DataOutputStream outtoclient =new DataOutputStream (Socket.getoutputstream ());
BufferedReader infromclient =new BufferedReader (New InputStreamReader (Socket.getinputstream

()));
String requestmessageline = Infromclient.readline ();
Outtoclient.writebytes (Requestmessageline);

. NET socket mechanism provides two kinds of implementations: one is directly with the socket class, one is to use the socket again encapsulated Tcpclient,udpclient class to implement. NET provides a unified transceiver mechanism: NetworkStream.

The bottom of the tcpclient and socket are all networkstream to transmit data. They can all produce a networkstream. For example:

TcpClient client = server. AcceptTcpClient ();
NetworkStream stream = client. GetStream ();

. NET socket and Java socket in the sending and receiving data is the biggest difference is: Java socket can be like a normal flow operation. NET socket has a variety of implementation methods, you can directly use Socket.send () to send, can also be sent with Networdstream, can also be sent with TcpClient. But each implementation, when sending and receiving data, is manipulated by the display of the buffer. For example:

byte[] datasize = new Byte[4]; Networkstream.write (datasize,0,4);
Socket. Send (DataSize, 0,datasize. Length, 0);

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.