c#网络编程问题

JAYXIANJIAN 2012-05-08 03:09:56
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Net.Sockets;
using System.Threading;

namespace ChatClient
{
public partial class Form1 : Form
{
private bool isExit = false;
private delegate void SetListBoxCallBack(string str);
private SetListBoxCallBack setlistboxcallback;
private delegate void SetRichTextBoxReceiveCallBack(string str);
private SetRichTextBoxReceiveCallBack setRichTextBoxReceiveCallBack;
private TcpClient client;
private NetworkStream ns;
private ManualResetEvent allDone = new ManualResetEvent(false);

public Form1()
{
InitializeComponent();
setlistboxcallback = new SetListBoxCallBack(SetListBox);
setRichTextBoxReceiveCallBack = new SetRichTextBoxReceiveCallBack(SetRichTextBoxReceive);
}

private void button1_Click(object sender, EventArgs e)
{
//声明客户端的对象
client = new TcpClient(AddressFamily.InterNetwork);
IPAddress[] serverIP = Dns.GetHostAddresses(Dns.GetHostName());
//采用的是异步的方式
AsyncCallback requestCallBack = new AsyncCallback(RequestCallBack);//回调函数RequestCallBack
allDone.Reset();
//开始连接
client.BeginConnect(serverIP[0],51888,requestCallBack,client);//这一行出错,说不支持此协议版本
listBox1.Invoke(setlistboxcallback,string.Format("本机终结点:{0}",client.Client.LocalEndPoint));
listBox1.Invoke(setlistboxcallback,"开始与服务器连接");
allDone.WaitOne();
}

private void RequestCallBack(IAsyncResult iar)
{
allDone.Set();
try
{
client = (TcpClient)iar.AsyncState;
client.EndConnect(iar);
listBox1.Invoke(setlistboxcallback,string.Format("与服务器{0}连接成功",client.Client.RemoteEndPoint));
ns = client.GetStream();
DataRead dataRead = new DataRead(ns,client.ReceiveBufferSize);
ns.BeginRead(dataRead.msg, 0, dataRead.msg.Length, ReadCallBack, dataRead);//回调函数ReadCallBack
}
catch(Exception e)
{
listBox1.Invoke(setlistboxcallback ,e.Message);
return;
}
finally
{
}
}

private void ReadCallBack(IAsyncResult iar)
{
try
{
DataRead dataRead = (DataRead)iar.AsyncState;
int recv = dataRead.ns.EndRead(iar);
//在文本框里设置得到的数据
richTextBox2.Invoke(setRichTextBoxReceiveCallBack,Encoding.UTF8.GetString(dataRead.msg,0,recv));
if (isExit == false)
{
dataRead = new DataRead(ns,client.ReceiveBufferSize);
ns.BeginRead(dataRead.msg,0,dataRead.msg.Length,ReadCallBack,dataRead);
}
}
catch(Exception e)
{
listBox1.Invoke(setlistboxcallback,e.Message);
}
finally
{}
}

private void SetListBox(string str)
{
listBox1.Items.Add(str);
listBox1.SelectedIndex = listBox1.Items.Count - 1;
listBox1.ClearSelected();
}

private void SetRichTextBoxReceive(string str)
{
richTextBox2.AppendText(str);
}

private void button2_Click(object sender, EventArgs e)
{
SendString(richTextBox1.Text);
richTextBox2.Clear();
}

private void SendString(string str)
{
try
{
byte [] bytesdata=Encoding.ASCII.GetBytes(str+"\r\n");
ns.BeginWrite(bytesdata, 0, bytesdata.Length, new AsyncCallback(SendCallBack), ns);//回调函数SendCallBack
ns.Flush();
}
catch(Exception e)
{
listBox1.Items.Add(e.Message);
}
finally
{}
}

private void SendCallBack(IAsyncResult iar)
{
try
{
ns.EndWrite(iar);
}
catch(Exception e)
{
listBox1.Invoke(setlistboxcallback,e.Message);
}
finally
{}
}

}
}
...全文
98 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
rjxyan 2012-08-18
  • 打赏
  • 举报
回复
直接写client.BeginConnect("127.0.0.1", 51888, requestCallback, client);
JAYXIANJIAN 2012-05-09
  • 打赏
  • 举报
回复
serverIP[0]是null。。。。我用的是win7系统 那应该怎么修改呢?
qldsrx 2012-05-08
  • 打赏
  • 举报
回复
断点看下这个serverIP[0]是什么,如果你是Win7系统,那么这个地址就是IPv6的地址,而目前中国不支持IPv6

111,126

社区成员

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

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

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