基于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
基于socket的客户端和服务端聊天机器人的更多相关文章
- 基于socket的客户端和服务端聊天简单使用 附Demo
功能使用 服务端 分离一个不停接受客户端请求的线程 接受不客户端请求的线程中,再分离就收消息的线程 几大对象分别是 IPEndPoint IP终结点 服务端Socket,绑定终结点Bind,启动监听L ...
- 基于socket.io客户端与服务端的相互通讯
socket.io是对websocket的封装,用于客户端与服务端的相互通讯.官网:https://socket.io/. 下面是socket.io的用法: 1.由于使用express开的本地服务,先 ...
- c++ 网络编程(一)TCP/UDP windows/linux 下入门级socket通信 客户端与服务端交互代码
原文作者:aircraft 原文地址:https://www.cnblogs.com/DOMLX/p/9601511.html c++ 网络编程(一)TCP/UDP 入门级客户端与服务端交互代码 网 ...
- 实验09——java基于TCP实现客户端与服务端通信
TCP通信 需要先创建连接 - 并且在创建连接的过程中 需要经过三次握手 底层通过 流 发送数据 数据没有大小限制 可靠的传输机制 - 丢包重发 包的顺序的 ...
- 用PHP的socket实现客户端到服务端的通信
服务端 <?php error_reporting(E_ALL); set_time_limit(0); ob_implicit_flush(); //本地IP $address = 'loca ...
- Python socket编程客户端与服务端通信
[本文出自天外归云的博客园] 目标:实现客户端与服务端的socket通信,消息传输. 客户端 客户端代码: from socket import socket,AF_INET,SOCK_STREAM ...
- Socket通信客户端和服务端代码
这两天研究了下Socket通信,简单实现的客户端和服务端代码 先上winfrom图片,客户端和服务端一样 服务端代码: using System; using System.Collections.G ...
- C# Winform 通过Socket实现客户端和服务端TCP通信
操作界面如下: 1.声明Socket 第一个参数:寻址方式,第二个参数:传输数据的方式,第三个参数:通信协议 Socket socket = new Socket(AddressFamily.Inte ...
- 在Java中使用Socket模拟客户端和服务端(多线程)
1:Socket与ServerSocket的交互 2.Socket和ServerSocket介绍 Socket 构造函数 Socket() Socket(InetAddress address, in ...
随机推荐
- [翻译]AlphaGO留给我们的东西
来源:http://headlines.yahoo.co.jp/hl?a=20160317-00000049-cnippou-krhttp://headlines.yahoo.co.jp/hl?a=2 ...
- Flex4 自定义通用的ImageButton
Flex4与之前版本的一个极大区别就是外观皮肤的分离,虽然进一步解耦,但存在一个不爽的地方就是增加了编码的工作量,你能想象为你的每个自定义组件都写一个对应的皮肤吗?可能仅仅和你之前写过的组件差了那么一 ...
- 学习总结 html 表格标签的使用
表格: <table></table>表格 width:宽度.可以用像素或百分比表示. 常用960像素. border:边框,常用值为0. cellpadding:内容跟边框的 ...
- PNG图片数据解析
PNG是一种非常流行的图片格式,它不仅支持透明效果,而且图片数据经过了压缩处理,所以广泛用于web等应用. PNG的文件格式: PNG文件中的数据,总是以一个固定的8个字节开头: (图片来自http: ...
- 避免使用CreateThread函数,导致的内存泄露
原文链接:http://blog.csdn.net/solosure/article/details/6262877
- JavaScript常用代码段
总结一下在各种地方看到的还有自己使用的一些实用代码 1)区分IE和非IE浏览器 if(!+[1,]){ alert("这是IE浏览器"); } else{ alert(" ...
- 有没有好用的开源sql语法分析器? - 匿名用户的回答 - 知乎
有没有好用的开源sql语法分析器? - 匿名用户的回答 - 知乎 presto,hive,drill,calcite,sparksq
- Oracle多线程并行使用、关联与指定索引执行
nologging AS SELECT /*+parallel(4) leading(s a) use_hash(A) index(s IDX_CS_SERVICE_RECORD_MD2_04) */ ...
- sotower1.5-LS_工作流容易出错的地方
流程代码如下: 1>LeiSheng.WorkFlow.FlowInfo fInfo = new LeiSheng.WorkFlow.FlowInfo(); 2&g ...
- Shiro使用总结
Shiro已经添加到项目中,现阶段管理两个功能: 1.身份验证:(已经能够满足现阶段需求) 2.权限管理: 权限管理,需要在界面中加一些标签,后台角色.资源的管理也需要整理好,然后在前端添加管理. 1 ...