You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
234 lines
6.6 KiB
C#
234 lines
6.6 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Net.Sockets;
|
|
using System.Net;
|
|
using System.Threading;
|
|
using System.Collections;
|
|
|
|
namespace demo.ClassHelper.NetOperate
|
|
{
|
|
class TcpServerHelper
|
|
{
|
|
public static Dictionary<string, Socket> dicConnectedClient = new Dictionary<string, Socket>();//存储监听到的客户端
|
|
|
|
public static Thread threadWatch = null;//负责监听客户端的线程
|
|
|
|
public ManualResetEvent allDone = new ManualResetEvent(false);
|
|
|
|
public static Socket CreateTcpServer()
|
|
{
|
|
return new Socket(AddressFamily.InterNetwork,
|
|
SocketType.Stream, ProtocolType.Tcp);
|
|
}
|
|
|
|
public static bool BindEndPoint(Socket server, IPEndPoint endPoint)
|
|
{
|
|
bool ret = false;
|
|
try
|
|
{
|
|
if (!server.IsBound)
|
|
{
|
|
server.Bind(endPoint);
|
|
ret = true;
|
|
}
|
|
}
|
|
catch { }
|
|
return ret;
|
|
}
|
|
|
|
public static bool BindEndPoint(Socket server, string ip, int port)
|
|
{
|
|
bool ret = false;
|
|
try
|
|
{
|
|
IPEndPoint endPoint = new IPEndPoint(IPAddress.Parse(ip), port);
|
|
if (!server.IsBound)
|
|
{
|
|
server.Bind(endPoint);
|
|
ret = true;
|
|
}
|
|
}
|
|
catch { }
|
|
return ret;
|
|
}
|
|
|
|
public static void Disconnect(Socket server)
|
|
{
|
|
if (server != null)
|
|
{
|
|
server.Shutdown(SocketShutdown.Both);
|
|
server.Disconnect(true);
|
|
}
|
|
}
|
|
|
|
public static void Close(Socket server)
|
|
{
|
|
if (server != null)
|
|
{
|
|
server.Shutdown(SocketShutdown.Both);
|
|
server.Close();
|
|
}
|
|
}
|
|
|
|
public static bool SetListenQueueLength(Socket server, int backlog)
|
|
{
|
|
bool ret = false;
|
|
try
|
|
{
|
|
server.Listen(backlog);
|
|
ret = true;
|
|
}
|
|
catch { }
|
|
return ret;
|
|
}
|
|
|
|
public static void WatchConnecting(Socket server, Dictionary<string, Socket> dicConnectedClient)
|
|
{
|
|
Socket connecClient = null;
|
|
while (true)
|
|
{
|
|
try
|
|
{
|
|
connecClient = server.Accept();
|
|
string connectEndPoint = connecClient.RemoteEndPoint.ToString();
|
|
dicConnectedClient.Add(connectEndPoint, connecClient);
|
|
}
|
|
catch (System.Exception ex)
|
|
{
|
|
break;
|
|
throw new Exception(ex.Message);
|
|
|
|
}
|
|
}
|
|
}
|
|
|
|
public static void Recv(object socketclientpara)
|
|
{
|
|
Socket socketClient = socketclientpara as Socket;
|
|
while (true)
|
|
{
|
|
byte[] arrServerRecMsg = new byte[1024 * 1024];
|
|
try
|
|
{
|
|
int length = socketClient.Receive(arrServerRecMsg);
|
|
string strSRecMsg = Encoding.UTF8.GetString(arrServerRecMsg, 0, length);
|
|
//WriteLogToListView(string.Format("客户端{0} {1}", socketClient.RemoteEndPoint, strSRecMsg));
|
|
|
|
//socketClient.Send(Encoding.UTF8.GetBytes("测试server 是否可以发送数据给client "));
|
|
|
|
}
|
|
catch
|
|
{
|
|
dicConnectedClient.Remove(socketClient.RemoteEndPoint.ToString());
|
|
//WriteLogToListView(string.Format("客户端{0} 已经中断", socketClient.RemoteEndPoint));
|
|
socketClient.Close();
|
|
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
public static bool SendMsgToPointClient(Socket client, string msg)
|
|
{
|
|
bool ret = false;
|
|
try
|
|
{
|
|
if (client!=null&&msg!=null)
|
|
{
|
|
byte[] bytes = Encoding.Default.GetBytes(msg);
|
|
client.Send(bytes);
|
|
ret = true;
|
|
}
|
|
}
|
|
catch { }
|
|
return ret;
|
|
}
|
|
|
|
public static bool SendMsgToPointClient(Socket client, byte[] msg)
|
|
{
|
|
bool ret = false;
|
|
try
|
|
{
|
|
if (client != null && msg != null)
|
|
{
|
|
client.Send(msg);
|
|
ret = true;
|
|
}
|
|
}
|
|
catch { }
|
|
return ret;
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public static bool RecvMsgFromClient(Socket server, ref string remoteEndPoint, ref string msg, int timeOut = 0)
|
|
{
|
|
bool ret = false;
|
|
try
|
|
{
|
|
server.ReceiveTimeout = timeOut;
|
|
byte[] bytes = null;
|
|
int len = server.Available;
|
|
if (len>0)
|
|
{
|
|
bytes = new byte[len];
|
|
int recvLen = server.Receive(bytes);
|
|
if (recvLen == len)
|
|
{
|
|
ret = true;
|
|
msg = Encoding.UTF8.GetString(bytes, 0, recvLen);
|
|
}
|
|
remoteEndPoint = server.RemoteEndPoint.ToString();
|
|
}
|
|
}
|
|
catch { }
|
|
return ret;
|
|
}
|
|
|
|
public static bool SendMsgToPoinrClient(Socket server, string ip, int port, byte[] msg, int timeOut = 0)
|
|
{
|
|
bool ret = false;
|
|
try
|
|
{
|
|
server.SendTimeout = timeOut;
|
|
EndPoint endPoint = new IPEndPoint(IPAddress.Parse(ip), port);
|
|
int len = server.SendTo(msg, endPoint);
|
|
if (len == msg.Length)
|
|
{
|
|
ret = true;
|
|
}
|
|
}
|
|
catch { }
|
|
return ret;
|
|
}
|
|
|
|
public static bool SendMsgToPoinrClient(Socket server, string ip, int port, string msg, int timeOut = 0)
|
|
{
|
|
bool ret = false;
|
|
try
|
|
{
|
|
byte[] byteMsg = new byte[msg.Length];
|
|
server.SendTimeout = timeOut;
|
|
EndPoint endPoint = new IPEndPoint(IPAddress.Parse(ip), port);
|
|
int len = server.SendTo(byteMsg, endPoint);
|
|
if (len == msg.Length)
|
|
{
|
|
ret = true;
|
|
}
|
|
}
|
|
catch { }
|
|
return ret;
|
|
}
|
|
}
|
|
}
|