实例C#开发TCP/IP聊天室
来源:互联网 发布:网络直播主播作用 编辑:程序博客网 时间:2024/05/22 10:46
服务器端原代码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Threading;
using System.Net.Sockets;
namespace Win_Talk
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
#region//定义变量
IPAddress HostIP = IPAddress.Parse("127.0.0.1");
IPEndPoint point;
Socket socket;
bool flag = true;
Socket acceptedSocket;
#endregion
#region//声名委托
delegate void SetTextCallback(string text);
private void SetText(string text)
{
textBox2.AppendText(text + "/r/n");
}
#endregion
#region//进程方法
private void Proccess()
{
if (acceptedSocket.Connected)
{
while (flag)
{
byte[] receiveByte = new byte[64];
acceptedSocket.Receive(receiveByte, receiveByte.Length, 0);
string strInfo = Encoding.BigEndianUnicode.GetString(receiveByte);
this.Invoke(new SetTextCallback(SetText), new object[] { strInfo });
}
}
}
#endregion
private void button3_Click(object sender, EventArgs e)
{
try
{
socket.Close();
acceptedSocket.Close();
}
catch
{
}
finally
{
Close();
}
}
private void button2_Click_1(object sender, EventArgs e)
{
try
{
Byte[] sendByte = new Byte[64];
string sendStr = this.textBox1.Text + ":" + this.textBox3.Text + "/r/n";
sendByte = Encoding.BigEndianUnicode.GetBytes(sendStr.ToCharArray());
acceptedSocket.Send(sendByte, sendByte.Length, 0);
}
catch { }
}
private void button1_Click_1(object sender, EventArgs e)
{
HostIP = IPAddress.Parse("127.0.0.1");
try
{
point = new IPEndPoint(HostIP, Int32.Parse("11000"));
socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
socket.Bind(point);
socket.Listen(50);
acceptedSocket = socket.Accept();
Thread thread = new Thread(new ThreadStart(Proccess));
thread.Start();
}
catch (Exception ey)
{
MessageBox.Show(ey.Message);
}
}
}
}
客户端程序代码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Threading;
using System.Net.Sockets;
namespace win_talkClient
{
public partial class Form1 : Form
{
#region//声名变量
IPAddress HostIP = IPAddress.Parse("127.0.0.1");
IPEndPoint point;
Socket socket;
bool flag = true;
#endregion
#region//声名委托
delegate void SetTextCallback(string text);
private void SetText(string text)
{
textBox2.AppendText(text + "/r/n");
}
#endregion
#region//进程
private void Proccess()
{
if (socket.Connected)
{
while (flag)
{
byte[] receiveByte = new byte[64];
socket.Receive(receiveByte, receiveByte.Length, 0);
string strInfo = Encoding.BigEndianUnicode.GetString(receiveByte);
this.Invoke(new SetTextCallback(SetText),new object[]{strInfo});
}
}
}
#endregion
private void button2_Click(object sender, EventArgs e)
{
try
{
Byte[] sendByte = new Byte[64];
string sendStr = this.textBox1.Text + ":" + this.textBox3.Text+"/r/n";
sendByte = Encoding.BigEndianUnicode.GetBytes(sendStr.ToCharArray());
socket.Send(sendByte, sendByte.Length, 0);
}
catch { }
}
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
HostIP = IPAddress.Parse("127.0.0.1");
try
{
point = new IPEndPoint(HostIP, Int32.Parse("11000"));
socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
socket.Connect(point);
Thread thread = new Thread(new ThreadStart(Proccess));
thread.Start();
}
catch(Exception ey)
{
MessageBox.Show("服务器没有开启/r/n"+ey.Message);
}
}
private void button3_Click(object sender, EventArgs e)
{
try
{
socket.Close();
}
catch
{
}
finally
{
Close();
}
}
}
}
- 实例C#开发TCP/IP聊天室
- 实例C#开发TCP/IP聊天室
- 实例开发聊天室 tcp协议
- C#网络编程(一)——C#开发TCP/IP聊天室
- TCP/IP聊天室
- 基于TCP的C#开发的聊天室C/S程序
- 简易聊天室一 TCP/IP框架搭建
- 多线程及TCP通信 实例之 聊天室
- TCP/IP 程序实例
- Linux TCP/IP 实例
- tcp/ip通信实例
- C# TCP/IP数据包
- Flex 与 c# socket 完整实例:聊天室
- TCP聊天室
- TCP 聊天室
- TCP聊天室
- linux网络编程练习---聊天室(TCP/IP实现)
- Unity基于TCP/IP的小聊天室实现
- C#实现阳历转阴历的方法
- 分布式编程->Remoting的一个代码示例(借助Remoting实现发送信息功能)
- 热点_C#实现蜘蛛_爬虫程序的多线程控制
- C++之父元旦专访(8+13个问题,关于C++的学习&使用和未来)
- 关于UpdatePanel中验证控件失效的原因分析
- 实例C#开发TCP/IP聊天室
- C#UDP的多路广播组的发送和接收
- C#使用UDP Client编写聊天程序
- C#获取电脑硬件配置信息
- C#对指定的网页进行截图
- JS实现网站主页轮换广告
- Asp.Net文件和文件夹操作大全
- 关于使用UpdatePanel局部刷新后页面title变化为无标题的原因
- 图片缩放_ASP.NET实现图片缩略图