java Socket??C#????????????????????????
???????????? ???????[ 2012/12/13 9:53:40 ] ????????
??ServerListen??
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using System;
namespace TCPServer
{
public class ServerListen
{
public static void Run(Object iGetClientData)
{
IPEndPoint serverIP = new IPEndPoint(IPAddress.Parse("IP???")?? ???);//?????????IP????
Socket skServer = new Socket(AddressFamily.InterNetwork?? SocketType.Stream?? ProtocolType.Tcp);
skServer.Bind(serverIP);
skServer.Listen(100);//?????????????
while (true)
{
Socket skClient;
try
{
//???п??????????????????У???????????μ?socket???????????????????
skClient = skServer.Accept();
}
catch (Exception ex)
{
LogHelper.Err("?????????????? ServerListen.Run Socket.Accept"?? ex);
continue;
}
ThrClient thrC = new ThrClient(skServer?? skClient?? (IGetClientData)iGetClientData);
Thread t = new Thread(thrC.Run);
t.Start();
}
}
}
}
????ThrClient??
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.Threading;
namespace TCPServer
{
public class ThrClient
{
private static readonly StringBuilder sb = new StringBuilder();
private Socket skServer;
private Socket skClient;
private IPEndPoint clientIP;
private IGetClientData iGetClientData;
//??ν?????????????
private byte[] sendData;//????????
private byte[] receiveData = new byte[1024];//???????
private int receiveN;
private int netID;
/// <summary>
/// ThrClient??????
/// </summary>
/// <param name="pSkServer">?????</param>
/// <param name="pSkClient">?????</param>
public ThrClient(Socket pSkServer?? Socket pSkClient??IGetClientData iGetClientData)
{
this.skServer = pSkServer;
this.skClient = pSkClient;
this.iGetClientData = iGetClientData;
this.clientIP = (IPEndPoint)this.skClient.RemoteEndPoint;
//???????????UTF8?????????Uncode
this.sendData = Encoding.UTF8.GetBytes("success");
try
{
this.skClient.Send(sendData?? sendData.Length?? SocketFlags.None);//???????
}
catch (Exception ex)
{
LogHelper.Err("????????????????? ThrClient.ThrClient Socket.Send"?? ex);
}
}
public void Run()
{
while (true)
{
try
{
this.receiveN = skClient.Receive(this.receiveData);//????
if (this.receiveN != 0)
{
string removeMsg = Encoding.UTF8 .GetString(receiveData?? 0?? receiveN);
iGetClientData.getThread(removeMsg);
}
}
catch (Exception ex)
{
iGetClientData.getClientIP(((IPEndPoint)skClient.RemoteEndPoint).Address);
break;
}
}
}
}
??????????????????????????????????????????????????
???????????????????????????????????????????????????2???????????????????????????????????????????????????????
??????
???·???
??????????????????
2023/3/23 14:23:39???д?ò??????????
2023/3/22 16:17:39????????????????????Щ??
2022/6/14 16:14:27??????????????????????????
2021/10/18 15:37:44???????????????
2021/9/17 15:19:29???·???????·
2021/9/14 15:42:25?????????????
2021/5/28 17:25:47??????APP??????????
2021/5/8 17:01:11