A client I recently wroteProgramIf a problem occurs, that is, when many users are offline at the same time (for example, unplugging the network cable from a computer on many clients), the server may have an exception at a very low probability. Abnormal trace shows that this is caused by writing data to closed tcpclient. Specifically, when networkstream. beginwrite method is called, invalidoperationexception occurs. The message is "write operation not
I have seen many times that some people call the networkstream. Flush method to immediately send data during network transmission. Indeed, calling the flush method of stream will clear the buffer, but the problem is that networkstream is not a stream with a buffer, which is described in msdn. And its source code can also be seen. Whether it is. NET 2.0 or the latest. Net 4.5, check the source code of
I wroteArticle: Indicates the socket type of Windows Phone 7. In fact, there are more than the network stream type in Windows Phone 7.
So I manually wrote one, which is very simple, as follows:
> It inherits the stream type and is named networkstream. The namespace must be system. net.
> Create a constructor. Pay attention to the fileaccess parameter in networkstream and set the stream. Canread
stream stream processing technology to make it read and write data more convenient and intuitive, at the same time. NET Framework is responsible for providing richer structures to handle streams throughout. NET Framework with more extensive compatibility, a common approach built on more general flow operations allows us to no longer need to be confused with the actual content of the file (HTML, XML, or anything else), and the application will use a consistent approach (Stream.Write, Stream.read
client data:
At this time, the Socket instance has been generated. If there is a request on the network, after the request passes, the Socket instance constructs a "NetworkStream" object, and the "NetworkStream" Object provides the basic data stream for network access. We use the two classes "StreamReader" and "StreamWriter" encapsulated in the namespace "System. IO"ImplementationAccess to the "
. AcceptSocket ();
(2). Accept and send client data:
At this time, the Socket instance has been generated. If there is a request on the network, after the request passes, the Socket instance constructs a "NetworkStream" object, and the "NetworkStream" Object provides the basic data stream for network access. We use the two classes "StreamReader" and "StreamWriter" encapsulated in the namespace "System. IO"
higher level of abstraction in the TCP service, which is reflected in the sending and receiving of network data, and is tcpclient use standard stream stream processing technology to make it read and write data more convenient and intuitive, at the same time. NET Framework is responsible for providing richer structures to handle streams throughout. NET Framework with more extensive compatibility, a common approach built on more general flow operations allows us to no longer need to be confused w
transmission and acceptance of network data, tcpclient uses the standard stream processing technology to make it easier and more intuitive to read and write data ,. the. NET Framework is responsible for providing richer structures for stream processing throughout the entire process. the stream in the. NET Framework has wider compatibility, the general method built on more general stream operations makes us no longer need to be confused with the actual content of the file (HTML, XML, or any othe
processing throughout the entire process. the stream in the. NET Framework has wider compatibility, the general method built on more general stream operations makes us no longer need to be confused with the actual content of the file (HTML, XML, or any other content ), all applications will use the same method (stream. write, stream. read) sends and receives data. In addition, the stream provides real-time access to data when data is downloaded from the Internet. You can process some data immed
. Net provides a NetworkStream for TCP read/write. In actual use, it is found that the direct operation efficiency is very low, even if the TCP sending cache and receiving cache settings are not greatly improved. Later, the cache was set before NetworkStream read/write, and the performance was greatly improved.
According to the actual test results, setting your own write cache has the most significant perfo
flow-related classes, first defining an abstract base class stream, and then inheriting various, such as Filestream,networkstream,memorystream. The convection is then extended to operations such as cryptographic operations, copy operations, and so on.
Business Operation class Stream{public:virtual char Read (int number) =0; virtual void Seek (int position) =0; virtual void Write (char data) =0; Virtual ~stream () {}};//principal class, File
Two Methods for sending data and receiving data can be used to Send data using the Socket-class Send method and the NetworkStream-class Write method to Receive data using the Socket-class Receive method and the NetworkStream-class Read method Socket. the following is a prototype of the Send method of the class: publicintSend (byte [] buffer) publicintSend (byte []
Two Methods for sending data and receiving
( ) ;
(2). Accept and send client data:
At this point the socket instance has been generated, if there is a request on the network, after the request passes, the socket instance constructs a "NetworkStream" object, and the "NetworkStream" object provides the underlying data stream for network access. We implement access to the "NetworkStream" object through th
specified host name and port number(3) BeginConnect: Asynchronous request to start a remote host connection(4) GetStream: Gets the NetworkStream object that can send and receive data
General steps for TCP programming1. The most basic premise of network communication is that the client should establish a TCP connection with the server first.2. The server should continuously monitor whether the client has a connection request, and the server can ident
If there are server programs, chatserver and client program chatclient, the client can send messages to the server.
Operation steps,
1. Start listen on the server,
2. Connect the client.
3. The client sends messages
As long as the server starts listen, and the client also connects. After the connection is established, it is convenient to accept the sending information. You only need to use writer and reader to operate networkstream.
Server chatserver
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.