关于网络编程

blueteeth_yl 2009-11-19 08:57:24

private void FrmMain_Load(object sender, EventArgs e)
{
FrmMain.CheckForIllegalCrossThreadCalls = false;
FrmMain.Data = new Thread(new ThreadStart(this.DataStart));
FrmMain.Data.Start();

}
窗口启动,开启data线程;
public void DataStart()
{
while (true)
{
try
{
if (client.Connected)
{

NetworkStream ns = new NetworkStream(client);
StreamReader reader = new StreamReader(ns);

string result = reader.ReadLine();
byte[] u = utf8.GetBytes(result);
byte[] gb = Encoding.Convert(utf8, gb2312, u);
string sGb = gb2312.GetString(gb);
}
}
}
}

上面的方法来接受服务端的信息,请高手看看那边出错了.while (true)
{
try
{
if (client.Connected)
{
NetworkStream ns = new NetworkStream(client);
StreamReader reader = new StreamReader(ns);

这边调试时ns会出异常.IOexception


...全文
139 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
qldsrx 2009-11-20
  • 打赏
  • 举报
回复
为什么不用TcpClient.GetStream()来获取NetworkStream?
不过你还是用TcpClient.Client来接收发送数据的好,NetworkStream没有直接操作Socket的好。
walkghost 2009-11-20
  • 打赏
  • 举报
回复
呃,能多发点儿代码吗?
blueteeth_yl 2009-11-20
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 wuyq11 的回复:]
NetworkStream ns = new NetworkStream(client);
是否获取到值,应该是IO问题
[/Quote]

详细说明下吧
blueteeth_yl 2009-11-20
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 solawing 的回复:]
socket 未连接。



socket 的 SocketType 属性不是 SocketType.Stream。

- 或 -

socket 处于非阻止状态。

[/Quote]




client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
这个是定义的socket
Lucky_6Q 2009-11-19
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 sandy945 的回复:]
NetworkStream ns = new NetworkStream(client);
if(ns!=null)
{
                        StreamReader reader = new StreamReader(ns);

                        string result = reader.ReadLine();
                        byte[] u = utf8.GetBytes(result);
                        byte[] gb = Encoding.Convert(utf8, gb2312, u);
                        string sGb = gb2312.GetString(gb);
}

断点跟一下,看看ns 为什么为null

[/Quote]
试试看
萨拉嘿 2009-11-19
  • 打赏
  • 举报
回复
弱弱的问一句 你那client对象是什么?socket还是TcpClient
tl6630617 2009-11-19
  • 打赏
  • 举报
回复
SolaWing 2009-11-19
  • 打赏
  • 举报
回复
socket 未连接。



socket 的 SocketType 属性不是 SocketType.Stream。

- 或 -

socket 处于非阻止状态。
wuyq11 2009-11-19
  • 打赏
  • 举报
回复
NetworkStream ns = new NetworkStream(client);
是否获取到值,应该是IO问题
阿非 2009-11-19
  • 打赏
  • 举报
回复
NetworkStream ns = new NetworkStream(client);
if(ns!=null)
{
StreamReader reader = new StreamReader(ns);

string result = reader.ReadLine();
byte[] u = utf8.GetBytes(result);
byte[] gb = Encoding.Convert(utf8, gb2312, u);
string sGb = gb2312.GetString(gb);
}

断点跟一下,看看ns 为什么为null
blueteeth_yl 2009-11-19
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 lzsh0622 的回复:]
什么异常信息
[/Quote]
ns是空的.但是clinet不是空的.把clinet!=null换成clinet.connected,也还是会有这个异常
lzsh0622 2009-11-19
  • 打赏
  • 举报
回复
什么异常信息

110,536

社区成员

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

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

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