那位高手帮找找我的“c#通信程序“问题

zhanghongliang.ok 2010-01-08 04:30:51
程序如下:
using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Threading;

namespace net_communication_client
{
public partial class chat_client : Form
{
public chat_client()
{
InitializeComponent();
initialize();

}
public bool connected = false;
public IPEndPoint ip_point = null;
public Socket socket = null;
public NetworkStream net_stream = null;
public TextReader reader = null;
public TextWriter writer = null;
public Thread thread1 = null;
public string ip = "10.1.3.86";
public string port ="3001";

private void initialize()
{
bool flag=true ;
ip_point = new IPEndPoint(IPAddress.Parse(ip),int.Parse(port));
socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

while (flag)
{
socket.Connect(ip_point);
if (socket.Connected)
{
flag = false;
MessageBox.Show("connect successfully,go on!");
}
}


if (socket.Connected)
{
net_stream = new NetworkStream(socket);
reader = new StreamReader(net_stream);
writer = new StreamWriter(net_stream);
thread1 = new Thread(new ThreadStart(this.ThreadListen));
thread1.Start();
connected = true;
}


}
public void ThreadListen()
{
string tmp;
while (connected)
{
try
{
tmp = reader.ReadLine();
if (tmp.Length != 0)
{
lock (this)
{
record.Text += "He say:" + " " + tmp + "\n";
}
}
}
catch (Exception exception)
{
MessageBox.Show(exception.Message);
}

}
socket.Shutdown(SocketShutdown.Both);
socket.Close();
}

private void send_Click(object sender, EventArgs e)
{
if (connected)
{
lock (this)
{
try
{
record.Text += "I say:" + chat_word.Text + "\n";
writer.WriteLine(chat_word.Text);
writer.Flush();
}
catch
{
MessageBox.Show("connection cut off");
}
}
}

else
{
MessageBox.Show("sorry,not connected with others,cannot communication");
}
chat_word.Text = null;
chat_word.Focus();
}

}
}
红线部分调试时总是提示:
An unhandled exception of type 'System.Net.Sockets.SocketException' occurred in System.dll

Additional information: A socket operation was attempted to an unreachable host
...全文
63 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
缭绕飘渺 2010-01-12
  • 打赏
  • 举报
回复
楼主这个是客户端的吧
你先把服务器端的程序跑起来先
你客户端一直没有连接
程序不会往下执行
zjwchcchen 2010-01-12
  • 打赏
  • 举报
回复
看你的注释,感觉好像没找到主机,是不是没开服务啊
zhangkewen1234 2010-01-08
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 zhangkewen1234 的回复:]
socket.Connect(ip_point);
写错了吧
socket.Connect(ip,point);是逗号

[/Quote]
看错了,不好意思
zhangkewen1234 2010-01-08
  • 打赏
  • 举报
回复
socket.Connect(ip_point);
写错了吧
socket.Connect(ip,point);是逗号
_autotest 2010-01-08
  • 打赏
  • 举报
回复
先telnent下ip_point能否连上吧
Windy_juhua 2010-01-08
  • 打赏
  • 举报
回复
bushihendong,dingding!
nkboy 2010-01-08
  • 打赏
  • 举报
回复
while (flag)
{
socket.Connect(ip_point);
if (socket.Connected)
{
flag = false;
MessageBox.Show("connect successfully,go on!");
}
}
循环多余了 connect会一直尝试连接知道连接上为止。。。

111,120

社区成员

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

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

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