写的也很简单,自己觉得挺有意思了

程序如图

主要代码

    public class Message
{
Form1 mainfrom = null;
public Message() { }
public Message(Form1 form)
{
mainfrom = form;
}
public bool StartReceive(int port)
{
try
{
IPEndPoint iep = new IPEndPoint(IPAddress.Loopback, port);
Socket tcpServer = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
tcpServer.Bind(iep);
tcpServer.Listen();
tcpServer.BeginAccept(new AsyncCallback(Accept), tcpServer);
return true;
}
catch (Exception ex) { return false; }
} private void Accept(IAsyncResult ia)
{
Socket socket = ia.AsyncState as Socket;
var client = socket.EndAccept(ia);
byte[] buf = new byte[];
socket.BeginAccept(new AsyncCallback(Accept), socket);
StateObject state = new StateObject();
state.workSocket = client;
try
{
client.BeginReceive(state.buffer, , StateObject.BufferSize, SocketFlags.None, new AsyncCallback(Receive), state);
}
catch (Exception ex)
{
//Console.WriteLine("监听请求时出错:\r\n" + ex.ToString());
}
} private void Receive(IAsyncResult ia)
{
StateObject state = ia.AsyncState as StateObject;
if (state == null)
{
return;
}
Socket client = state.workSocket;
if (client == null)
{
return;
}
try
{
int count = client.EndReceive(ia);
if (count > )
{
try
{
client.BeginReceive(state.buffer, , StateObject.BufferSize, SocketFlags.None, new AsyncCallback(Receive), client);
string context = Encoding.GetEncoding("gb2312").GetString(state.buffer, , count);
//显示接收消息
mainfrom.LoginFormTextChange(context);
}
catch (Exception ex)
{
//Console.WriteLine("接收的数据出错:\r\n{0}", ex.ToString());
}
}
}
catch (Exception err)
{ }
} public void SendMessage(int port, string m)
{
System.Text.Encoding CharSet = System.Text.Encoding.GetEncoding("gb2312");
Socket tcpClient = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
try
{
tcpClient.Connect(IPAddress.Loopback, port);
string sendmsg = m;
byte[] buff = CharSet.GetBytes(sendmsg);
tcpClient.Send(buff, buff.Length, );
}
catch (SocketException e)
{
}
}
} public class StateObject
{
// Client socket.
public Socket workSocket = null;
// Size of receive buffer.
public const int BufferSize = ;
// Receive buffer.
public byte[] buffer = new byte[BufferSize];
// Received data string.
public StringBuilder sb = new StringBuilder();
}

源码 http://yun.baidu.com/s/1i39XtjR

最新文章

  1. jQuery插件库代码分享 - 进阶者系列 - 学习者系列文章
  2. 0024 Java学习笔记-面向对象-包装类、对象的比较、String常量池问题
  3. MyBatis关联查询 (association) 时遇到的某些问题/mybatis映射
  4. sphinx 增量索引 实现近实时更新
  5. -XX:+PrintHeapAtGC 每次一次GC后,都打印堆信息
  6. 229. Majority Element II
  7. 根据N种规格中的M种规格值生成的全部规格组合的一种算法
  8. 019C#中使用移位运算符获取汉字编码值
  9. jvm 之 国际酒店 8 月 19 一次full GC 导致的事故
  10. cdoj 1136 邱老师玩游戏 树形背包
  11. 检查class排座位
  12. Q105971:Converting a Regular GUID to a Compressed GUID
  13. SQL server 2008数据库的备份与还原、分离(转)
  14. SEO高手在扯蛋?
  15. 张高兴的 UWP 开发笔记:定制 ContentDialog 样式
  16. iOS 将对象的属性和属性值拆分成key、value,通过字符串key来获取该属性的值
  17. Failed to load JavaHL Library. These are the errors that were encountered:
  18. JMeter-自动生成测试报告
  19. LeetCode(36)- Implement Stack using Queues
  20. JS简单实现分页显示

热门文章

  1. Fsu0413's Qt builds
  2. 表格无边框,有内框,在table嵌套时,防止出现重复边线
  3. C++多字节字符转换为宽字符的两种方法
  4. Linux下设置开机自启动Tomcat
  5. L1 正则 和 L2 正则的区别
  6. aix archPlat
  7. linux学习之(三)-文件操作命令
  8. 此证书的签发者无效Missing iOS Distribution signing identity问题解决
  9. 动画原理——绘画API
  10. 移动前端不得不了解的HTML5 head 头标签(2016最新版)