Differences between socket and tcplistener, tcpclient, and udpclient
Abstract: applications can use the Transmission Control Protocol (TCP) and user data packet protocol (UDP) services through tcpclient, tcplistener, and udpclient ......
Applications can use the Transmission Control Protocol (TCP) and user data packet protocol (UDP) services through tcpclient, tcplistener, and udpclient. These protocol classes are based on the system. net. sockets. Socket class, responsible for data transmission
Applications can use the Transmission Control Protocol (TCP) and user data packet protocol (UDP) services through tcpclient, tcplistener, and udpclient. These protocol classes are based on the system. net. sockets. Socket class, responsible for data transmission details. (Tcpclient, tcplistener, and udpclient are used to simplify socket)
Tcpclient and tcplistener use the networkstream class to represent the network. Use the getstream method to return
legendary server.2 start listeningLen. Start ();3. Pending connection requestsSocket S = Len. acceptsocket ();If the acceptsocket () is successful, a socket type is returned.4. Create a working ChannelNetworkstream NS = new networkstream (s); Establish an NS channel on this socket. Note that ns is of the networkstream type.For the client, you need to use the following:1 tcpclient usage:Tcpclient CLI = new
smtp| Server | client | Experience with the TcpClient connection, for example, first get the data stream sent back by the server.
NetworkStream Streamaccount=tcpclient.getstream ();
When we send a request to an SMTP server, such as a connection, a user name, a password, the server returns an answer stream.
We have to read the server back to the data stream, which I have undergone 3 changes.
The first program is in accordance with the Visaul C #. N
servers, server code.
Connect using socket sockets.
1 using System; 2 using System.Net; 3 using System.Net.Sockets; 4 using System.IO; 5 6 Publi C Class Echoserver 7 { 8//entry Point of the main method. 9 public static void main () { One//tcp Listener is listening on the given port Int32 port = 1234; \ //ipaddress is CONNETCT IP address//ipaddress addr = Ipaddress.parse ("127.0.0.1"); IPAddress ipaddress = dns.resolve ("localhost"). ADDRESSLIST[0]; TcpListener tcplistener = new
returned only if a connection request is received. If the connection is successful, you can start sending the file to the customer.
if (socketforclient.connected)
{
???
Next, we need to create a NetworkStream class that passes the report to constructor:
Networkstreamnetworkstream=newnetworkstream (socketforclient);
Then create a StreamWriter object, just this time not on the file but on the NetworkStream
(1) (2) (3) (4)]
Read files from the network
In C ++, reading files on the network requires considerable programming skills. NET provides extensive support for this. In fact, reading files on the network is only another application of the Stream class in the base class library.
First, to listen to the TCP/IP Port (65000 in this example), we need to create an instance of the TCPListener class.
TCPListenertcpListener = newTCPListener (65000 );
Once created, let it start listening.
TcpListener. St
code section of the main form, because we need to use network and network stream objects, we must first Add the following important Namespace ):
Using System. Net;
Using System. Net. Sockets;
Using System. IO;
Using System. Text;
Next, add a NetworkStream object to our class to control the stream in the network.
Private NetworkStream netStream;
Add the following code to the constructor of this class to set
the byte stream and the string does not deviate from the encoding method. Causes a subsequent decryption operation to occur unexpectedly.Client1//Create TCP connection 2 TcpClient TCP = new TcpClient ("localhost", 11000); 3 4//Get network traffic from the TCP connection 5 NetworkStream NetStream = tcp. GetStream (); 6 7//for easy display, stream the encrypted data bytes into a Base64 encoded string of 8 string encryptBase64 = Convert.tobase64strin
A socket is a high-level interface based on TCP and UDP protocols. It defines the format for sending and receiving data. The Socket used in Java's TCP Service is a stream mechanism. For programmers, to process a socket, they only need to obtain the stream from the Socket, then, you can send and receive data like a local stream.
For example:
DataOutputStream outToClient = new DataOutputStream (socket. getOutputStream ());
BufferedReader inFromClient = new BufferedReader (new InputStreamReader (so
Void Reconnect (); // Manual Reconnection 16 Void Initialize (); 17 Void Disposeconnections (); // Release all connections in the pool. You can use reconnect to establish a new connection. 18 Void Setstreamdamaged ( Int Streamhashcode ); 19 20 Networkstream implements tcpstream (); 21 Void Givebacktcpstream ( Int Streamhashcode ); // Return TCP connection rules to the connection pool 22 } As And e
tcpclient = new tcpclient ("www.abcd.com ", 51888); 3132 3. the getstring () method in the tcpclient object can obtain the networkstream object 33 For example: this. client = client; 34 networkstream = client. getstream (); 35. networkstream object 36 is used for data transmission between the server and the customer, for example: Public binaryreader BR; 37 publi
Header instance: HOST:RSS.SINA.COM.CNUSER-AGENT:MOZILLA/5, 0 (Windows; U Windows NT 5, 1; ZH-CN; Rv:1, 8, 1, +) gecko/20080404 firefox/2, 0, 0, 14accept:text/xml,application/xml,application/xhtml+xml,text/html;q=0, 9,text/plain;q=0, 8,image/png,*/*;q=0, 5accept-language:zh-cn,zh;q=0, 5accept-encoding:gzip, Deflateaccept-charset:gb2312,utf-8;q=0, 7,*;q=0, 7KEEP-ALIVE:300CONNECTION:KEEP-ALIVECOOKIE:USERID=C5BYPXRIMDMSIQMSBPNE1VN8ZQMDWSM3WRLEB3VRWTNRTW Then here is an example, or download the prev
the writing class through a stringByte [] buffer = new byte [RSA_KEY_SIZE_BYTES];NetworkStream ns = client. GetStream ();MemoryStream MS = new MemoryStream ();BinaryFormatter bf = new BinaryFormatter ();RSAParameters result;
Int len = 0;Int totalLen = 0;
While (totalLen(Len = ns. Read (buffer, 0, buffer. Length)> 0){TotalLen + = len;Ms. Write (buffer, 0, len );}
Ms. Position = 0;
Result = (RSAParameters) bf. Deserialize (MS );Ms. Close ();
Return res
serverNetworkStream networkStream = tcpClient. GetStream ();// Send the user name to the serverByte [] userName_byte = Encoding. Unicode. GetBytes (userNameBox. Text. Trim ());NetworkStream. Write (userName_byte, 0, userName_byte.Length );NetworkStream. Flush ();// Read the information returned by the serverByte [] inforBuffer = new byte [1, 100];
through the network in synchronous blocking mode.Take a simple console chatbot as an example:Server:
Using System;Using System. Collections. Generic;Using System. Linq;Using System. Text;Using System. Net. Sockets;Using System. Net;Using System. IO;Using System. Threading;
Namespace Server{Class Program{Static void Main (string [] args){Console. WriteLine ("server ");TcpListener server = new TcpListener (IPAddress. Parse ("***. ***"), 9999 );
// Ip address of the serverServer. Start ();TcpClien
) from the client \n . (Translator Note: A line is a message)TCP Server using NetworkStream
Disclaimer: As with all performance-sensitive work, you should measure the reality of each scenario in your application. Depending on the scale of your network application needs to be processed, you may not need to care about the overhead of various technologies.
The typical code written in. NET before pipelines is as follows:async Task ProcessLin
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.