One, file streamThe FileStream class is primarily used to read and write disk files . Often used to store data or read configuration files to disk.Read file://File stream: Read FileStream FileStream = File.Open (@ "D:\test.txt", FileMode.Open); Initialize file stream byte[] array = new byte[filestream.length]; Initializes a byte array that is used to stage the read to byte filestream.read (array, 0, array. Length); // read data in stream, write to byte array filestream.close (); // Close strea
The tcpsend window is used to send messages, and an application is written to receive messages.ProgramAfter a message is received, you must close the message receiving window to receive new messages.
Source code:
Message sending windowCode
Using system;Using system. Collections. Generic;Using system. componentmodel;Using system. Data;Using system. drawing;Using system. text;Using system. Windows. forms;Using system. net;Using system. net. Sockets;Using system. IO;
Namespace tcpsocket{P
read and write operations through the socket descriptor.
. Netframework provides the system. net. Socket namespace for socket communication. The namespace contains the following important classes:
· The socket class is a low-level class used internally to manage connections, webrequest, tcpclient, and udpclient.
· The networkstream class is derived from stream, which indicates data streams from the network.
· Tcpclient class allows you to create and
(buffer, 0 );6 // write the commandheader into the Array7 Buffer [4] = (byte) This. Command;8 // write the primary version number to the array9 buffer [5] = (byte) This. mainversion;10 // write the version number to the array11 buffer [6] = (byte) This. secondversion;1213 // The message header has been written, and the message body is now written.14 socketfactory. defaultencoding. getbytes (this. messagebody, 0, this. messagelength-7, buffer, 7 );15 return buffer;16}
The byte array we read from
established with the server. Then, you can perform read and write operations through the socket descriptor.
. Netframework provides the system. net. Socket namespace for socket communication. The namespace contains the following important classes:
· The socket class is a low-level class used internally to manage connections, webrequest, tcpclient, and udpclient.
· The networkstream class is derived from stream, which indicates data streams from
read and write operations through the socket descriptor.
. Netframework provides the system. net. Socket namespace for socket communication. The namespace contains the following important classes:
· The socket class is a low-level class used internally to manage connections, webrequest, tcpclient, and udpclient.
· The networkstream class is derived from stream, which indicates data streams from the network.
· Tcpclient class allows you to create and
mail more, your mailbox exploded. The format of the DELE command is:DELE If you delete the error, you can use the RSet command to recover all messages that have been deleted. The condition is that you have not quit, once the exit, then all bye bye. When all is done, enter the QUIT command to exit the POP3 server.Above, I briefly introduce to you the basic process and principle of POP3 mail receiving, below is a Simple mail receiving program which is realized by using the above principle. In thi
Localip = dns.gethostaddresses (Dns.gethostname ()) [0]; // hfy:: Get local IP New 8088); // hfy:: Create TcpListener object Tcplistener.start (); // hfy:: Start monitoring2) in a separate thread, the loop calls the AcceptTcpClient method to accept the client's connection request, which returns the TcpClient object, using the GetStream method of the returned TcpClient object to get the NetworkStream object. The BinaryReader objec
This article code just to be able to deeply understand the network communication, without the code back out, only need to understand, can change on the lineFirst you need to create a socketNote the need to write in VS2010: New Socket (AddressFamily.InterNetwork, SocketType.Stream, protocoltype.tcp);Socket socket = new socket (SocketType.Stream, protocoltype.tcp),//tcp, UDP.Socket. Connect (new Dnsendpoint ("127.0.0.1", 8080));//Connect to the server. The default port number for the HTTP protocol
. read reads the response information sent from the server in the current stream. Its parameters are consistent with the Write method parameters. The k value is the length of the Read string for (int I = 0; I
The preceding figure shows the simple communication between the client and the server. As an example program, we will record the connection to the camera server in our own project.
First, take the camera as the server side and define the client:
TcpClient client = new TcpClient ("IP", 8080
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 Bu
is used to read and write.
. NETFramework provides a System.Net.Socket namespace for socket communication, in which there are several important classes that are commonly used:
· Socket class This low-level class is used to manage connections, and webrequest,tcpclient and UdpClient use this class internally.
· NetworkStream class This class is derived from the stream, which represents the flow of data from the network
· TcpClient class allows
response information is not as rich and varied as the SMTP protocol, with only two: +ok or-err. Where +ok indicates a successful connection, while-err indicates that the connection failed. Next, the client enters user ImplementAbove, I briefly introduced to you the basic process and principles of POP3 mail reception, the following is a use of the principle of the implementation of the Simple Mail receiving program. With an understanding of the fundamentals, programming work becomes quite easy.
()
{
Console.WriteLine ("Attempts to get a URI.");
Console.WriteLine ("\r\nusage:");
Console.WriteLine ("Clientget URI");
Console.WriteLine ("Example:");
Console.WriteLine ("Clientget http://www.contoso.com/");
}
}
Using streams on the network
network resources are represented as streams in the. NET framework. The. NET Framework provides the following features for general processing through convection:
A common way to send and receive WEB data. The application uses Stream.Write and Stream
Imports System.IO
Imports System.Net
Imports System.Net.Sockets
......
Private Function Connect (ByVal strserver As String, ByVal struser As String, ByVal strpwd as String) as NetworkStream
Dim Sender as New tcpclient (strserver, 110)
Dim outbytes as Byte ()
Dim Input as String
Dim Ns as NetworkStream = Nothing
Try
Ns = Sender.getstream ()
Dim SR as New StreamReader (Ns)
input = "User" struser VbCrLf
Ou
server to indicate our identity. Here we choose sina.com.cn; the response time varies depending on the amount of free time on the network and the recipient's email server. Here, the waiting time is 3 seconds. When the network or the other server is very busy, reducing the wait time will increase the possibility of test failure.
Public Function CheckEmail (ByVal sEmail As String) As Long
Dim oStream As NetworkStream
Dim sFrom As String 'sen
the receiving file for the server.ReceiveFile (protocol );} Else if (protocol. Mode = FileRequestMode. Receive ){// The client receives the file, which is the sending file for the server.SendFile (protocol );}}
// Send a filePrivate void sendFile (FileProtocol protocol ){TcpClient localClient;NetworkStream streamToClient = getStreamToClient (protocol, out localClient );
// Obtain the file pathString filePath = Environment. CurrentDirectory + "/" + pr
client sends the file, which is the receiving file for the server.ReceiveFile (protocol );} Else if (protocol. Mode = FileRequestMode. Receive ){// The client receives the file, which is the sending file for the server.SendFile (protocol );}}
// Send a filePrivate void sendFile (FileProtocol protocol ){TcpClient localClient;NetworkStream streamToClient = getStreamToClient (protocol, out localClient );
// Obtain the file pathString filePath = Environm
connected to a single client Public Networkstream ns; // Network Flow Public Streamreader SR; // Stream reading Public Streamwriter SW; // Stream writing Private Delegate Void Settextcallback (); // Used to operate the main program-controlled parts
(2) The next step is to design the management of client connections, mainly including the connection between the server and the client and sending and receiving data. Put them in a f
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.