using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.Net.Sockets;
namespace BestUdpClient
...{
class Program
...{
private static byte[] data = new byte[1024];
private static IPEndPoint sender = new IPEndPoint(IPAddress.Any, 9050);
private static EndPoint remote = (EndPoint)sender;
private static int size = 30;
private static int AdvSndRcvData(Socket s, byte[] message, EndPoint rmtdevice)
...{
int recv = 0;
int retry = 0;
while (true)
...{
Console.WriteLine("Attemp #{0}", retry);
try
...{
s.SendTo(message, message.Length, SocketFlags.None, rmtdevice);
data = new byte[size];
recv = s.ReceiveFrom(data, ref remote);
}
catch (SocketException e)
...{
if (e.ErrorCode == 10054)
recv = 0;
else if (e.ErrorCode == 10040)
...{
Console.WriteLine("Error receiving packet");
size += 10;
recv = 0;
}
}
if (recv > 0)
return recv;
else
...{
retry++;
if (retry > 4)
...{
return 0;
}
}
}
}
static void Main(string[] args)
...{
string input, stringdata;
int recv;
IPEndPoint ipep = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 9050);
Socket server = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
int sockopt = (int)server.GetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReceiveTimeout);
Console.WriteLine("Default timeout:{0}", sockopt);
server.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReceiveTimeout, 3000);
sockopt = (int)server.GetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReceiveTimeout);
Console.WriteLine("New timeout:{0}", sockopt);
string welcome = "Hello,are you there";
data = Encoding.ASCII.GetBytes(welcome);
recv = AdvSndRcvData(server,data, ipep);
if (recv > 0)
...{
stringdata = Encoding.ASCII.GetString(data, 0, recv);
Console.WriteLine(stringdata);
}
else
...{
Console.WriteLine("Unable to communicate with remote host");
return;
}
while (true)
...{
input = Console.ReadLine();
if (input == "exit")
...{
break;
}
recv = AdvSndRcvData(server, Encoding.ASCII.GetBytes(input), ipep);
if (recv > 0)
...{
stringdata = Encoding.ASCII.GetString(data, 0, recv);
Console.WriteLine(stringdata);
}
else
Console.WriteLine("Did not receive an answer");
}
Console.WriteLine("Stopping client");
server.Close();
}
}
}
1944

被折叠的 条评论
为什么被折叠?



