专注收集记录技术开发学习笔记、技术难点、解决方案
网站信息搜索 >> 请输入关键词:
您当前的位置: 首页 > WinForm

基于socket的客户端跟服务端聊天机器人

发布时间:2011-06-23 13:54:22 文章来源:www.iduyao.cn 采编人员:星星草
基于socket的客户端和服务端聊天机器人

服务端代码如下:

using System;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using System.Windows.Forms;

namespace Client
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
//对 Windows 窗体控件进行线程安全调用
RichTextBox.CheckForIllegalCrossThreadCalls = false;
}
private Socket serverSocket;
/// <summary>
/// 服务端开启监听
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button2_Click(object sender, EventArgs e)
{
int port = 6000;
string host = "127.0.0.1";
IPAddress ip = IPAddress.Parse(host);
IPEndPoint ipe = new IPEndPoint(ip, port);
Socket sSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
sSocket.Bind(ipe);
sSocket.Listen(100);
ParameterizedThreadStart par = ServerListenMethod;
Thread thread = new Thread(par);
thread.Start(sSocket);
}
public void ServerListenMethod(object sSocket)
{
while (true)
{
Socket serSocket = ((Socket)sSocket).Accept();
ParameterizedThreadStart p = ServerCommunicationMethod;
Thread t = new Thread(p);
t.Start(serSocket);
}
}
public void ServerCommunicationMethod(object sSocket)
{
serverSocket = (Socket)sSocket;
string recStr = "";

//创建内存缓存区
byte[] recByte = new byte[1024 * 1024 * 5];
while (true)
{
int bytes = serverSocket.Receive(recByte, recByte.Length, 0);
recStr = Encoding.Default.GetString(recByte, 0, bytes);
this.richTextBox1.AppendText("服务器端获得信息:" + recStr);
}
}
/// <summary>
/// 服务端发送消息
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button1_Click(object sender, EventArgs e)
{
var str = this.textBox1.Text;
this.textBox1.Text = "";
byte[] sendByte = Encoding.Default.GetBytes(str);
serverSocket.Send(sendByte, sendByte.Length, 0);
this.richTextBox1.AppendText("服务器端发送信息:" + str);
}
}
}

服务端启动如图:

----------------------------------------------------------------------------------------------------------------------

客户端代码如下

using System;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using System.Windows.Forms;

namespace Servers
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
//对 Windows 窗体控件进行线程安全调用
RichTextBox.CheckForIllegalCrossThreadCalls = false;
}
private Socket clientSocket;
/// <summary>
/// 客户端发送消息
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void BtnMsg_Click(object sender, EventArgs e)
{
var str = this.textBox1.Text;
clientSocket.Send(Encoding.Default.GetBytes(str));
this.richTextBox1.AppendText("客户端发送信息:" + str);
this.textBox1.Text = "";
}
/// <summary>
/// 建立连接
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button1_Click(object sender, EventArgs e)
{
int port = 6000;
string host = "127.0.0.1";
IPAddress ip = IPAddress.Parse(host);
IPEndPoint ipe = new IPEndPoint(ip, port);
clientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
clientSocket.Connect(ipe);
ThreadStart start = ClientListenMethod;
Thread t = new Thread(start);
t.Start();
}
public void ClientListenMethod()
{

//创建内存缓存区
byte[] recByte = new byte[1024 * 1024 * 5];
while (true)
{
int bytes = clientSocket.Receive(recByte, recByte.Length, 0);
string recStr = Encoding.Default.GetString(recByte, 0, bytes);
this.richTextBox1.AppendText("客户端端获得信息:" + recStr);
}
}
}
}

客户端启动如图:

 

注:如果需要项目源代码,可以发1870902607@qq.com邮箱告诉我,我会将项目源代码发给你。

----分享不仅帮助了别人,同时也提升了自己。luchao_it

1楼『大雪无痕』
这段代码,一瞟。,,只是 Socket 通讯的 最简单 Demo。,“机器人” —— 没看到。,,——————————————,现在不知道怎么了:,gt;基于 大数据 穷举的 聊天软件 —— 称之为 机器人,人工智能;,gt;就连 群主 都要跳出啦 说自己 写了一个 机器人;,,搞的 ,真正的 “人工智能” —— 只能叫做 “顶级人工智能”;,真正的 “机器人” —— 只能叫做 “顶级机器人”;
友情提示:
信息收集于互联网,如果您发现错误或造成侵权,请及时通知本站更正或删除,具体联系方式见页面底部联系我们,谢谢。

其他相似内容:

热门推荐: