直接上代码吧。这个短连接客户端只能发一次消息哦:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;
using ClientModel;
using LaserGate.common;
namespace LaserGate
{
public class TCPD
{
public void StartTCPVideo() {
Boolean running = true;
//============================================================================//
// 1、socket服务地址和监听端口
//============================================================================//
// socket服务地址
string host = "127.0.0.1";
// socket服务端口
int port = 8090;
// 将将IP地址字符串转换为IPAddress对象
IPAddress ip = IPAddress.Any;
// 终结点EndPoint
IPEndPoint ipe = new IPEndPoint(ip, port);
//============================================================================//
// 2、创建socket连接服务端并监听端口
//============================================================================//
//创建TCP Socket对象
Socket server = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
//绑定EndPoint对象(地址)
server.Bind(ipe);
//开始监听
server.Listen(0);
Console.WriteLine("已经处于监听状态,等待客户端连接 . . . ");
//============================================================================//
// 3、与客户端交互
//============================================================================//
while (running)
{
Socket remote = server.Accept();
Console.WriteLine("客户端连接 . . . ");
// 接收客户端消息
Receive(remote);
// 发送给客户端消息
Send(remote);
}
//============================================================================//
// 4、关闭服务
//============================================================================//
if (server != null)
{
server.Close();
server = null;
}
Console.WriteLine("ok");
Console.ReadKey(true);
}
static void Receive(Socket socket)
{
try
{
byte[] bytes = new byte[1024];
//从客户端接收消息
int len = socket.Receive(bytes, bytes.Length, 0);
//将消息转为字符串
string recvStr = Encoding.ASCII.GetString(bytes, 0, len);
Console.WriteLine("接收的客户端消息 : {0}", recvStr);
getVideoData(recvStr);
}
catch (Exception ex) {
Console.WriteLine(ex.ToString());
}
}
static void Send(Socket socket)
{
string sendStr = "ok";
Console.WriteLine("发送给客户端消息 : {0}", sendStr);
// 将字符串消息转为数组
byte[] bytes = Encoding.ASCII.GetBytes(sendStr);
//发送消息给客户端
socket.Send(bytes, bytes.Length, 0);
}
private static void getVideoData(string result) {
#region 业务
if (result.Split(',').Length > 0)
{
try
{
string[] arr = result.Split(',');
FaceRecognitionInfo faceRecognition = new FaceRecognitionInfo();
string personName = arr[6].ToString().Trim();
faceRecognition.cardId = personName.Substring(0,5).Trim();
faceRecognition.videoIp = "";
int resultAdd = ClientDAL.FaceRecognitionDAL.addFaceRecognitionLog(faceRecognition);
if (resultAdd == 1)
{
//service.Send(System.Text.Encoding.UTF8.GetBytes("ok"));
}
else
{
Log4netHelper.Error("插入识别到脸部信息失败:" + result);
}
}
catch (Exception ex)
{
Log4netHelper.Error("解析人脸识别数据失败:" + result + " " + ex.ToString());
}
}
#endregion
}
}
}
标签:Console,socket,C#,bytes,System,using,连接,客户端 From: https://blog.51cto.com/51souta/5805152