应该不是长度的问题。
ns的watch如下:- ns {System.Net.Sockets.NetworkStream} System.Net.Sockets.NetworkStream
+ base {System.Net.Sockets.NetworkStream} System.IO.Stream {System.Net.Sockets.NetworkStream}
CanRead true bool
CanSeek false bool
CanTimeout true bool
CanWrite true bool
DataAvailable false bool
- Length 'ns.Length' threw an exception of type 'System.NotSupportedException' long {System.NotSupportedException}
+ base {"此流不支持查找操作。"} System.SystemException {System.NotSupportedException}
- Position 'ns.Position' threw an exception of type 'System.NotSupportedException' long {System.NotSupportedException}
+ base {"此流不支持查找操作。"} System.SystemException {System.NotSupportedException}
ReadTimeout -1 int
WriteTimeout -1 int
- Non-Public members
+ base {System.Net.Sockets.NetworkStream} System.IO.Stream {System.Net.Sockets.NetworkStream}
Connected false bool
+ InternalSocket {System.Net.Sockets.Socket} System.Net.Sockets.Socket
m_CleanedUp false bool
m_CloseTimeout -1 int
m_CurrentReadTimeout -1 int
m_CurrentWriteTimeout -1 int
m_OwnsSocket false bool
m_Readable true bool
+ m_StreamSocket {System.Net.Sockets.Socket} System.Net.Sockets.Socket
m_Writeable true bool
Readable true bool
- Socket {System.Net.Sockets.Socket} System.Net.Sockets.Socket
AddressFamily InterNetwork System.Net.Sockets.AddressFamily
Available 0 int
Blocking true bool
Connected false bool
DontFragment false bool
+ EnableBroadcast 'ns.Socket.EnableBroadcast' threw an exception of type 'System.Net.Sockets.SocketException' bool {System.Net.Sockets.SocketException}
ExclusiveAddressUse false bool
+ Handle 1516 System.IntPtr
IsBound true bool
+ LingerState {System.Net.Sockets.LingerOption} System.Net.Sockets.LingerOption
+ LocalEndPoint {127.0.0.1:2588} System.Net.EndPoint {System.Net.IPEndPoint}
+ MulticastLoopback 'ns.Socket.MulticastLoopback' threw an exception of type 'System.Net.Sockets.SocketException' bool {System.Net.Sockets.SocketException}
NoDelay false bool
ProtocolType Tcp System.Net.Sockets.ProtocolType
ReceiveBufferSize 8192 int
ReceiveTimeout 0 int
+ RemoteEndPoint {127.0.0.1:1987} System.Net.EndPoint {System.Net.IPEndPoint}
SendBufferSize 8192 int
SendTimeout 0 int
SocketType Stream System.Net.Sockets.SocketType
Ttl 32 short
UseOnlyOverlappedIO false bool
+ Static members
+ Non-Public members
Writeable true bool
ns的watch如下:- ns {System.Net.Sockets.NetworkStream} System.Net.Sockets.NetworkStream
+ base {System.Net.Sockets.NetworkStream} System.IO.Stream {System.Net.Sockets.NetworkStream}
CanRead true bool
CanSeek false bool
CanTimeout true bool
CanWrite true bool
DataAvailable false bool
- Length 'ns.Length' threw an exception of type 'System.NotSupportedException' long {System.NotSupportedException}
+ base {"此流不支持查找操作。"} System.SystemException {System.NotSupportedException}
- Position 'ns.Position' threw an exception of type 'System.NotSupportedException' long {System.NotSupportedException}
+ base {"此流不支持查找操作。"} System.SystemException {System.NotSupportedException}
ReadTimeout -1 int
WriteTimeout -1 int
- Non-Public members
+ base {System.Net.Sockets.NetworkStream} System.IO.Stream {System.Net.Sockets.NetworkStream}
Connected false bool
+ InternalSocket {System.Net.Sockets.Socket} System.Net.Sockets.Socket
m_CleanedUp false bool
m_CloseTimeout -1 int
m_CurrentReadTimeout -1 int
m_CurrentWriteTimeout -1 int
m_OwnsSocket false bool
m_Readable true bool
+ m_StreamSocket {System.Net.Sockets.Socket} System.Net.Sockets.Socket
m_Writeable true bool
Readable true bool
- Socket {System.Net.Sockets.Socket} System.Net.Sockets.Socket
AddressFamily InterNetwork System.Net.Sockets.AddressFamily
Available 0 int
Blocking true bool
Connected false bool
DontFragment false bool
+ EnableBroadcast 'ns.Socket.EnableBroadcast' threw an exception of type 'System.Net.Sockets.SocketException' bool {System.Net.Sockets.SocketException}
ExclusiveAddressUse false bool
+ Handle 1516 System.IntPtr
IsBound true bool
+ LingerState {System.Net.Sockets.LingerOption} System.Net.Sockets.LingerOption
+ LocalEndPoint {127.0.0.1:2588} System.Net.EndPoint {System.Net.IPEndPoint}
+ MulticastLoopback 'ns.Socket.MulticastLoopback' threw an exception of type 'System.Net.Sockets.SocketException' bool {System.Net.Sockets.SocketException}
NoDelay false bool
ProtocolType Tcp System.Net.Sockets.ProtocolType
ReceiveBufferSize 8192 int
ReceiveTimeout 0 int
+ RemoteEndPoint {127.0.0.1:1987} System.Net.EndPoint {System.Net.IPEndPoint}
SendBufferSize 8192 int
SendTimeout 0 int
SocketType Stream System.Net.Sockets.SocketType
Ttl 32 short
UseOnlyOverlappedIO false bool
+ Static members
+ Non-Public members
Writeable true bool
read是同步读取,会阻塞,想不阻塞,可以用异步read,也就是用ns.BeginRead
或者把同步read的代码放到线程里面,你的客户端界面就不会卡死了
BackgroundWorker bgWorkder = new BackgroundWorker();
bgWorkder.DoWork += new DoWorkEventHandler(bgWorkder_DoWork);
bgWorkder.RunWorkerAsync(); void bgWorkder_DoWork(object sender, DoWorkEventArgs e)
{T IPAddress ip = IPAddress.Parse(txtIp.Text);
IPEndPoint ipPoint = new IPEndPoint(ip, Convert.ToInt16(txtPort.Text));
tcpClient = new TcpClient();
tcpClient.Connect(ipPoint);
while (true)
{
NetworkStream nStream = tcpClient.GetStream();
byte[] buffer = new byte[102400];
int received = nStream.Read(buffer, 0, buffer.Length);
txtRec.AppendText(Encoding.GetEncoding("GB2312").GetString(buffer));
txtRec.AppendText("\r\n");
}}至于beginRead,应该更方便,有人能给出demo否?
问题在于服务器一直没给客户端发送数据,应该把
Socket socket = listener.AcceptSocket();
clientService.Start();的代码改成:while (true) {
TcpClient client = listener.AcceptTcpClient();byte[] data = Encoding.Unicode.GetBytes("要发送的数据");client.GetStream().Write(data, 0, data.Length);client.GetStream().Close();
client.Close();
}这样客户端Read就可以执行下去了