networkstream对象read()时挂了怎么回事?

简单生活_cocoa 2012-08-30 10:20:56

服务器端:
int listenPort = 1987;
IPAddress ipAddress = IPAddress.Parse("127.0.0.1");

TcpListener listener = new TcpListener(ipAddress, listenPort);
listener.Start();
while(true)
{
try
{
Socket socket = listener.AcceptSocket();
clientSocket = socket;
clientService = new Thread(new ThreadStart(ServiceClient));
clientService.Start();
}catch(Exception ex){
Console.WriteLine(ex.ToString());
}
}
客户端:
TcpClient clientsocket = new TcpClient("127.0.0.1", 1987);
NetworkStream ns = new NetworkStream(clientsocket.Client);
Byte[] bytes = new Byte[1024];
ns.Read(bytes, 0, bytes.Length);

客户端执行到ns.Read(bytes, 0, bytes.Length);时,客户端死掉了。
...全文
470 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
午夜忄 2014-04-01
  • 打赏
  • 举报
回复
版主头像真给力
火星大能猫 2012-08-31
  • 打赏
  • 举报
回复

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否?
飞小猪 2012-08-31
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 的回复:]

引用 4 楼 的回复:

不是客户端死掉了,而是read方法在没有数据可读时会阻塞,正如console.readline在没有输入会阻塞一样

那该怎么处理呢?
我刚接触这个,还是不是很懂
[/Quote]

read是同步读取,会阻塞,想不阻塞,可以用异步read,也就是用ns.BeginRead
或者把同步read的代码放到线程里面,你的客户端界面就不会卡死了
简单生活_cocoa 2012-08-31
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 的回复:]

不是客户端死掉了,而是read方法在没有数据可读时会阻塞,正如console.readline在没有输入会阻塞一样
[/Quote]
那该怎么处理呢?
我刚接触这个,还是不是很懂
BosWorks 2012-08-31
  • 打赏
  • 举报
回复
Read阻塞没有问题,不管同步还是异步,在没有数据可读时,Read所在的线程都会阻塞。
问题在于服务器一直没给客户端发送数据,应该把
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就可以执行下去了
简单生活_cocoa 2012-08-30
  • 打赏
  • 举报
回复
这个里面的localEndPoint和RemoteEndPoint的端口不晓得为什么不一样。
简单生活_cocoa 2012-08-30
  • 打赏
  • 举报
回复
应该不是长度的问题。
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
bdmh 2012-08-30
  • 打赏
  • 举报
回复
ns 的长度是1024吗,是不是长度不匹配
BosWorks 2012-08-30
  • 打赏
  • 举报
回复
不是客户端死掉了,而是read方法在没有数据可读时会阻塞,正如console.readline在没有输入会阻塞一样

111,125

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Creator Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧