c#通过Socket实现图片、文件和文字的通讯。 private void button1_Click(object sender, EventArgs e)
{
try
{
server = TxStart.startServer(int.Parse(textBox_port.Text));
server.AcceptString = new TxDelegate<IPEndPoint, string>(acceptString);
server.AcceptByte = new TxDelegate<IPEndPoint, byte[]>(acceptBytes);
server.Connect = new TxDelegate<IPEndPoint>(connect);
server.dateSuccess = new TxDelegate<IPEndPoint>(dateSuccess);
server.Disconnection = new TxDelegate<IPEndPoint, string>(disconnection);
server.EngineClose = new TxDelegate(engineClose);
server.EngineLost = new TxDelegate<string>(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); }
}
/// <summary>
/// 发送按钮
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
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); }
}
/// <summary>
/// 下面显示的
/// </summary>
/// <param name="ipEndPoint"></param>
/// <param name="str"></param>
private void show(IPEndPoint ipEndPoint, string str)
{
label_zt.Text = ipEndPoint.ToString() ":" str;
label_all.Text = "当前在线人数:" this.server.ClientNumber.ToString();
}
/// <summary>
/// 当组合框按下的时候
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void comboBox1_DropDown_1(object sender, EventArgs e)
{
try
{
List<IPEndPoint> list = this.server.ClientAll;
this.comboBox1.DataSource = list;
}
catch { }
}
/// <summary>
/// 关闭客户端的按钮
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
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
评论