1234567891011121314151617181920212223242526272829303132333435363738394041424344454647 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Net.Sockets;
- using System.Text;
- using System.Threading.Tasks;
- namespace Ant.Service.Utility
- {
- public class SocketClient : Object
- {
- private TcpClient tcpClient;
- public SocketClient()
- {
- tcpClient = new TcpClient();
- tcpClient.Client.Blocking = true;
- }
- /// <summary>
- /// 连接
- /// </summary>
- /// <param name="host"></param>
- /// <param name="port"></param>
- public void Connect(string host, int port)
- {
- tcpClient.Connect(host, port);
- }
- /// <summary>
- /// 关闭连接
- /// </summary>
- public void Disconnect()
- {
- tcpClient.Close();
- }
- /// <summary>
- /// 发送数据
- /// </summary>
- /// <param name="json"></param>
- /// <returns></returns>
- public bool SendData(byte[] buffer)
- {
- var len = tcpClient.Client.Send(buffer, 0, buffer.Length, SocketFlags.None);
- return len > 0;
- }
- }
- }
|