初始化上传

This commit is contained in:
2025-08-26 08:37:44 +08:00
commit 31d81b91b6
448 changed files with 80981 additions and 0 deletions

View File

@@ -0,0 +1,81 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace .Utility.Network
{
public class TimeOutSocket
{
private bool IsConnectionSuccessful = false;
private Exception socketexception;
private ManualResetEvent TimeoutObject = new ManualResetEvent(false);
private string ip;
private int port;
private int timeout;
private TcpClient tcpClient;
public TimeOutSocket(string hostname, int port, int timeout_milliseconds)
{
this.ip = hostname;
this.port = port;
this.timeout = timeout_milliseconds;
}
public TcpClient Connect()
{
TimeoutObject.Reset();
socketexception = null;
TcpClient tcpclient = new TcpClient();
tcpclient.BeginConnect(this.ip, this.port, new AsyncCallback(CallBackMethod), tcpclient);
if (TimeoutObject.WaitOne(timeout, false))
{
if (IsConnectionSuccessful)
{
return tcpclient;
}
else
{
throw socketexception;
}
}
else
{
tcpclient.Close();
throw new TimeoutException("TimeOut Exception");
}
}
public void Disconnect()
{
tcpClient.Close();
}
private void CallBackMethod(IAsyncResult asyncresult)
{
try
{
IsConnectionSuccessful = false;
TcpClient tcpclient = asyncresult.AsyncState as TcpClient;
if (tcpclient.Client != null)
{
tcpclient.EndConnect(asyncresult);
IsConnectionSuccessful = true;
}
}
catch (Exception ex)
{
IsConnectionSuccessful = false;
socketexception = ex;
}
finally
{
TimeoutObject.Set();
}
}
}
}