本文整理汇总了C#中System.Net.Sockets.Socket.DuplicateAndClose方法的典型用法代码示例。如果您正苦于以下问题:C# Socket.DuplicateAndClose方法的具体用法?C# Socket.DuplicateAndClose怎么用?C# Socket.DuplicateAndClose使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Net.Sockets.Socket
的用法示例。
在下文中一共展示了Socket.DuplicateAndClose方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Main
static void Main(string[] args)
{
byte[] bytes = new byte[1024];
// Connect to a remote device.
try {
// Establish the remote endpoint for the socket.
// This example uses port 11000 on the local computer.
string serverIP = args[0]; //"192.168.56.1";
string client2IP = args[1];//192.168.56.1";
IPAddress ipAddress = IPAddress.Parse(serverIP);
IPEndPoint remoteEP = new IPEndPoint(ipAddress,1010);
Console.WriteLine("Client1: I will create a socket object to talk to the server");
// Create a TCP/IP socket.
Socket sender = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp );
// Connect the socket to the remote endpoint. Catch any errors.
try {
sender.Connect(remoteEP);
Console.WriteLine("Client1: Got connected to {0}",sender.RemoteEndPoint.ToString());
byte[] msg = Encoding.ASCII.GetBytes("This is Client1 saying hello!\n ");
// Send the data through the socket.[
Console.WriteLine("Client1: Sending message to server");
int bytesSent = sender.Send(msg);
Console.WriteLine("Client1: Message sent successfully! ");
System.Net.IPHostEntry ipHostEntry = System.Net.Dns.GetHostEntry(client2IP);
Process clientTwoProcess = Process.GetProcessesByName("Client2VM", ipHostEntry.HostName)[0];
SocketInformation sinfo = sender.DuplicateAndClose(clientTwoProcess.Id);
IPAddress client2IPAddress = IPAddress.Parse(client2IP);
remoteEP = new IPEndPoint(client2IPAddress, 2010);
// Create a TCP/IP socket.
Socket sender1 = new Socket(AddressFamily.InterNetwork,SocketType.Stream, ProtocolType.Tcp);
sender1.Connect(remoteEP);
Console.WriteLine("Client1: Let me send the socket object to Client2");
bytesSent = sender1.Send(sinfo.ProtocolInformation);
sender1.Close();
Console.WriteLine("Client1: GoodBye!");
Console.ReadLine();
} catch (ArgumentNullException ane) {
Console.WriteLine("ArgumentNullException : {0}",ane.ToString());
} catch (SocketException se) {
Console.WriteLine("SocketException : {0}",se.ToString());
} catch (Exception e) {
Console.WriteLine("Unexpected exception : {0}", e.ToString());
}
} catch (Exception e) {
Console.WriteLine( e.ToString());
}
}
示例2: Handle
public override void Handle(Socket accepted)
{
var si = accepted.DuplicateAndClose(Process.GetCurrentProcess().Id);
var ad = DomainHelper.Use(HandlerName);
log.i($"Handling socket in domain: {AppDomain.CurrentDomain.FriendlyName}");
DomainHelper.Execute(ad, (args) =>
{
var info = (SocketInformation)args[0];
ProcessSocket(info);
}, si);
}
示例3: ConvertSocketToCipheredCrosSocket
public static CipheredCrosSocket ConvertSocketToCipheredCrosSocket(Socket socket)
{
return new CipheredCrosSocket(socket.DuplicateAndClose(Process.GetCurrentProcess().Id));
}
示例4: acceptClientConnection
public void acceptClientConnection(Socket oldsock)
{
int TimeOut = 0;
string sEndPoint = "";
//CLIENT_COUNT++;
Socket sock = new Socket(oldsock.DuplicateAndClose(Process.GetCurrentProcess().Id));
sEndPoint = sock.RemoteEndPoint.ToString();
sock.ReceiveTimeout = 25000;
NetworkStream st = new NetworkStream(sock);
StreamReader rd = null;
bool Connected = true;
bool valid = true;
int base_cnt = 0;
int keep_alive = 0;
string current="";
int y = 0;
string test = "";
while (sock.Connected)
{
if (st.DataAvailable)
{
rd = new StreamReader(st);
test = rd.ReadLine();
if (test.IndexOf("MESSAGE") > -1)
{
string[] arr = test.Split('|');
Allarm(DateTime.Now.ToString(), arr[0], arr[2]);
current = arr[0];
if (!Connessioni.ContainsKey(arr[0]))
Connessioni.Add(arr[0], st);
}
if (test.IndexOf("GPS") > -1)
{
string[] arr = test.Split('|');
validGPSTablet(arr[2] + "," + arr[3] + "," + arr[4] + "," + arr[5], arr[0], DateTime.Now.ToString(), true);
current = arr[0];
if (!Connessioni.ContainsKey(arr[0]))
Connessioni.Add(arr[0], st);
}
if (test.IndexOf("ALLARME") > -1)
{
string[] arr = test.Split('|');
Allarm(DateTime.Now.ToString(), arr[0], arr[2]);
current = arr[0];
if (!Connessioni.ContainsKey(arr[0]))
Connessioni.Add(arr[0], st);
}
if (test.IndexOf("READY") > -1)
{
keep_alive--;
string[] arr = test.Split('|');
current = arr[0];
if (!Connessioni.ContainsKey(arr[0]))
Connessioni.Add(arr[0], st);
}
}
else
{
Thread.Sleep(100);
base_cnt++;
if (base_cnt >= 100)
{
base_cnt = 0;
StreamWriter wr = new StreamWriter(st);
wr.Write("KEEP ALIVE");
wr.Flush();
keep_alive++;
if (keep_alive >= 5)
{
Output("L'unità: " + current + " non risponde.");
}
}
}
}
// Alla Disconnessione
Connessioni.Remove(current);
Output(current + " si è disconnesso.");
}