C#实现Socket通讯Demo
c#通过Socket实现图片、文件和文字的通讯。
private void button1_Click(object sender, EventArgs e)
{
try
{
server = TxStart.startServer(int.Parse(textBox_port.Text));
server.AcceptString = new TxDelegate(acceptString);
server.AcceptByte = new TxDelegate(acceptBytes);
server.Connect = new TxDelegate(connect);
server.dateSuccess = new TxDelegate(dateSuccess);
server.Disconnection = new TxDelegate(disconnection);
server.EngineClose = new TxDelegate(engineClose);
server.EngineLost = new TxDelegate(engineLost);
//server.BufferSize=12048;
//server.FileLog = "C:\test.txt";
server.StartEngine();
this.button1.Enabled = false;
this.button2.Enabled = true;
this.button3.Enabled = true;
}
catch (Exception Ex) { MessageBox.Show(Ex.Message); }
}
///
/// 发送按钮
///
///
///
private void button2_Click(object sender, EventArgs e)
{
try
{
IPEndPoint client = (IPEndPoint)this.comboBox1.SelectedItem;
if (client == null)
{
MessageBox.Show("没有选中任何在线客户端!");
return;
}
if (!this.server.clientCheck(client))
{
MessageBox.Show("目标客户端不在线!");
return;
}
server.sendMessage(client, textBox_msg.Text);
}
catch (Exception Ex) { MessageBox.Show(Ex.Message); }
}
///
/// 下面显示的
///
///
///
private void show(IPEndPoint ipEndPoint, string str)
{
label_zt.Text = ipEndPoint.ToString() ":" str;
label_all.Text = "当前在线人数:" this.server.ClientNumber.ToString();
}
///
/// 当组合框按下的时候
///
///
///
private void comboBox1_DropDown_1(object sender, EventArgs e)
{
try
{
List list = this.server.ClientAll;
this.comboBox1.DataSource = list;
}
catch { }
}
///
/// 关闭客户端的按钮
///
///
///
private void button3_Click_1(object sender, EventArgs e)
{
IPEndPoint client = (IPEndPoint)this.comboBox1.SelectedItem;
if (client == null)
{
MessageBox.Show("没有选中任何在线客户端!");
return;
}
if (!this.server.clientCheck(client))
{
MessageBox.Show("目标客户端不在线!");
return;
}
server.clientClose(client);
}
#endregion
- 2021-08-22 00:31:02下载
- 积分:1