求C#socket通信代码 关于socket通信,服务器与客户端传递信息,一方发送一方接收,我刚接触这个,不是很懂,请大家帮帮忙给个全代码,急用昂!谢谢 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 直接 搜索 "在C#中使用异步Socket编程实现TCP网络服务的C/S的通讯构架"直接编译 保证好用 服务器 private string _ServerIp = string.Empty; private Thread processor; private TcpListener tcpListener; string clientStr; public delegate void TextBoxDelegate(string content); private void StartListening() { //创建一个监听对象 tcpListener = new TcpListener(IPAddress.Any, 8888); tcpListener.Start(); //循环监听 while (true) { try { //取得客户端的连接 TcpClient tcpClient = tcpListener.AcceptTcpClient(); //取得客户端发过来的字节流 NetworkStream clientStream = tcpClient.GetStream(); //把字节流读入字节数组 byte[] buffer = new byte[1024]; clientStream.Read(buffer, 0, 1024); clientStr = System.Text.Encoding.UTF8.GetString(buffer); ReceiveContent( tcpListener.Server.AddressFamily.ToString() +"说:"+ clientStr + "\n"); tcpClient.Close(); clientStream.Close(); Thread.Sleep(500); } catch (SocketException se) { throw new Exception(se.Message); } } } /// <summary> /// 处理线程UI控件的问题 /// </summary> /// <param name="RecContent"></param> private void ReceiveContent(string RecContent) { if (this.textBoxContent.InvokeRequired)//如果在UI主线程操作 { this.textBoxContent.Invoke(new TextBoxDelegate(ReceiveContent), new object[] { RecContent }); } else this.textBoxContent.AppendText(RecContent); } private void Form1_Load(object sender, EventArgs e) { processor = new Thread(new ThreadStart(StartListening)); processor.IsBackground = true; processor.Start(); }客户端 private string _ServerIp = string.Empty; private Thread processor; private TcpListener tcpListener; string clientStr; public delegate void TextBoxDelegate(string content); private void buttonSub_Click(object sender, EventArgs e) { if (this.textBoxSend.Text == string.Empty) { MessageBox.Show("总得说点什么吧!"); return; } //得到服务器端IP,然后建立一个连接 IPAddress ip; if (IPAddress.TryParse(this.textBoxIP.Text, out ip)) { try { TcpClient tcpClient = new TcpClient(this.textBoxIP.Text,8888); //取得数据流 _ServerIp = this.textBoxIP.Text; NetworkStream clientStream = tcpClient.GetStream(); //只能向服务器发送字节流,所以要编码 byte[] response = new byte[1024]; response = System.Text.Encoding.UTF8.GetBytes(this.textBoxSend.Text.ToCharArray()); //通过Write方法把客户端数据发向服务器 clientStream.Write(response, 0, response.Length); //通过Read方法读取过来的数据 if (this.textBoxContent.Text == string.Empty) this.textBoxContent.AppendText("我说:" + this.textBoxSend.Text + "\n"); else this.textBoxContent.AppendText(Environment.NewLine + "我说:" + this.textBoxSend.Text + "\n"); clientStream.Close(); tcpClient.Close(); } catch { MessageBox.Show("无法访问对方主机"); } } else { MessageBox.Show("非法的IP地址!"); } } 程序后台处理时button 禁用的属性,progressBar怎么做呀…… 读卡器的问题请教各位! 用rdlc打印报表,怎么可以在ReportViewer控件里预览的时候显示背景图片,打印的时候不打印背景图片? DataGridView合并行奇怪的问题 如何自定义窗体的显示位置? c#如何制作dll和lib文件? 要给别人的VC程序服务 关于C#串口 输入格式错误 谁能提供分层设计的ASP.net程序示例 net 该如何入门?在先等1!! C#执行无参数存储过程代码 CheckBoxList选中的问题100分
private Thread processor;
private TcpListener tcpListener;
string clientStr;
public delegate void TextBoxDelegate(string content);
private void StartListening()
{
//创建一个监听对象
tcpListener = new TcpListener(IPAddress.Any, 8888);
tcpListener.Start();
//循环监听
while (true)
{
try
{
//取得客户端的连接
TcpClient tcpClient = tcpListener.AcceptTcpClient();
//取得客户端发过来的字节流
NetworkStream clientStream = tcpClient.GetStream();
//把字节流读入字节数组
byte[] buffer = new byte[1024];
clientStream.Read(buffer, 0, 1024);
clientStr = System.Text.Encoding.UTF8.GetString(buffer);
ReceiveContent( tcpListener.Server.AddressFamily.ToString() +"说:"+ clientStr + "\n");
tcpClient.Close();
clientStream.Close();
Thread.Sleep(500);
}
catch (SocketException se)
{
throw new Exception(se.Message);
}
}
}
/// <summary>
/// 处理线程UI控件的问题
/// </summary>
/// <param name="RecContent"></param>
private void ReceiveContent(string RecContent)
{
if (this.textBoxContent.InvokeRequired)//如果在UI主线程操作
{
this.textBoxContent.Invoke(new TextBoxDelegate(ReceiveContent), new object[] { RecContent });
}
else
this.textBoxContent.AppendText(RecContent);
}
private void Form1_Load(object sender, EventArgs e)
{
processor = new Thread(new ThreadStart(StartListening));
processor.IsBackground = true;
processor.Start();
}
客户端 private string _ServerIp = string.Empty;
private Thread processor;
private TcpListener tcpListener;
string clientStr;
public delegate void TextBoxDelegate(string content);
private void buttonSub_Click(object sender, EventArgs e)
{
if (this.textBoxSend.Text == string.Empty)
{ MessageBox.Show("总得说点什么吧!"); return; }
//得到服务器端IP,然后建立一个连接
IPAddress ip;
if (IPAddress.TryParse(this.textBoxIP.Text, out ip))
{
try
{
TcpClient tcpClient = new TcpClient(this.textBoxIP.Text,8888);
//取得数据流
_ServerIp = this.textBoxIP.Text;
NetworkStream clientStream = tcpClient.GetStream();
//只能向服务器发送字节流,所以要编码
byte[] response = new byte[1024];
response = System.Text.Encoding.UTF8.GetBytes(this.textBoxSend.Text.ToCharArray());
//通过Write方法把客户端数据发向服务器
clientStream.Write(response, 0, response.Length);
//通过Read方法读取过来的数据
if (this.textBoxContent.Text == string.Empty)
this.textBoxContent.AppendText("我说:" + this.textBoxSend.Text + "\n");
else
this.textBoxContent.AppendText(Environment.NewLine + "我说:" + this.textBoxSend.Text + "\n");
clientStream.Close();
tcpClient.Close();
}
catch
{
MessageBox.Show("无法访问对方主机");
}
}
else { MessageBox.Show("非法的IP地址!"); }
}