* HP-Socket 官方网站:http://www.jessma.org
* HP-Socket 项目主页:http://www.oschina.net/p/hp-socket
* HP-Socket 开发文档:http://www.oschina.net/p/hp-socket/doc

HP-Socket 源代码下载地址:https://github.com/ldcsaa/HP-Socket

自动处理了底层通信的很多东西封装的很好,使用方便 ,提供了丰富的事件支持

//服务端

//显示消息
delegate void ShowMsg(int connid, string msg);

public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

HPSocketCS.TcpServer tcpServer = new HPSocketCS.TcpServer()
{
IpAddress = "192.168.3.61",
Port = 8858
};

private void Form1_Load(object sender, EventArgs e)
{
tcpServer.OnAccept += httpServer_OnAccept;
tcpServer.OnReceive += httpServer_OnReceive;
}

//启动
private void button1_Click(object sender, EventArgs e)
{
textBox1.Text += tcpServer.IpAddress + ":" + tcpServer.Port + " 开始监听:\n";
tcpServer.Start();
}

/// <summary>
/// 监听到连接
/// </summary>
/// <param name="connId"></param>
/// <param name="pClient"></param>
/// <returns></returns>
public HandleResult httpServer_OnAccept(IntPtr connId, IntPtr pClient)
{
ShowMsg showMsg = new ShowMsg((connid, msg) => { textBox1.Text += string.Format("客户端{0}:{1}\n", connid, msg); });
this.Invoke(showMsg, new object[] { connId.ToInt32(), "我上线了..." });
return HandleResult.Ok;
}

/// <summary>
///
/// </summary>
/// <param name="connId"></param>
/// <param name="bytes"></param>
/// <returns></returns>
public HandleResult httpServer_OnReceive(IntPtr connId, byte[] bytes)
{
ShowMsg showMsg = new ShowMsg((connid, msg) => { textBox1.Text += string.Format("客户端{0}说:{1}\n", connid, msg); });
this.Invoke(showMsg, new object[] { connId.ToInt32(), Encoding.Default.GetString(bytes) });
return HandleResult.Ok;
}

}

//客户端

public partial class Form1 : Form
{
HPSocketCS.TcpClient tcpClient;
public Form1()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)
{
tcpClient = new HPSocketCS.TcpClient();
bool connected = tcpClient.Connect("192.168.3.61", 8858);
if (connected)
{
textBox1.Text += (tcpClient.ConnectionId + ":已经连接");
}
}

private void button1_Click(object sender, EventArgs e)
{
string msg = textBox2.Text.Trim();
var bytes=Encoding.Default.GetBytes(msg);
bool sended = tcpClient.Send(bytes, 0, bytes.Length);
if (sended)
{
textBox1.Text += (tcpClient.ConnectionId + ":" + msg);
textBox2.Text = "";
}
}
}

最新文章

  1. Jquery(1)
  2. js判断地址转向
  3. Chrome 中的 JavaScript 断点设置和调试技巧
  4. python之萌新入门的第一天
  5. ZOJ 3407 Doraemon&#39;s Cake Machine [数学]
  6. ServiceStack简介
  7. SOLID architecture principles using simple C# examples
  8. php json_encode()和json_decode()
  9. setWillNotDraw和setFillViewport
  10. 【MFC学习笔记-作业8-蝴蝶飞~】【什么鬼作业】
  11. MYSQL知识点
  12. centos6.4-x86-64系统更新系统自带Apache Http Server
  13. Solr多核心及分词器(IK)配置
  14. 回味Python2.7——笔记4
  15. 自测-4 Have Fun with Numbers
  16. 【NOIP2015提高组】 Day1 T2 信息传递
  17. Google Chrome Plus&mdash;&mdash;绿色便携多功能谷歌浏览器
  18. jsp+postgresql学习笔记(1)用户登录与注册
  19. 基于timestamp和nonce的防重放攻击
  20. .AVLFile Extension

热门文章

  1. hive启动出错
  2. BootstrapBlazor-ValidateForm 表单验证组件
  3. Struct2中三种获取表单数据的方式
  4. Java设计模式——抽象工厂模式
  5. C++五子棋(三)——判断鼠标有效点击
  6. 一文搞懂MySQL事务的隔离性如何实现|MVCC
  7. vue动态绑定属性--基本用法及动态绑定class
  8. Mozi.HttpEmbedded嵌入式Web服务器
  9. 打基础丨Python图像处理入门知识详解
  10. linux脚本执行jar包运行