本文整理汇总了C#中System.Net.Sockets.Socket.BeginReceiveFrom方法的典型用法代码示例。如果您正苦于以下问题:C# Socket.BeginReceiveFrom方法的具体用法?C# Socket.BeginReceiveFrom怎么用?C# Socket.BeginReceiveFrom使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Net.Sockets.Socket
的用法示例。
在下文中一共展示了Socket.BeginReceiveFrom方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: btnConnect_Click
//metodo eseguito al click sul pulsante connetti
private void btnConnect_Click(object sender, EventArgs e)
{
//vengono memorizzati il nome utente e
//viene aggiornata la casella di testo
_userName = txtUserName.Text.Trim();
_setText = SetText;
//viene creato e riempito un pacchetto di richiesta di join alla chat
Packet sendData = new Packet();
sendData.DataIdentifier = DataTypeIdentifier.Login;
sendData.UserName = _userName;
//viene creato un Socket UDP
_clientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
//viene creato un oggetto contenente l'indirizzo IP e la porta del server
IPAddress serverIP = IPAddress.Parse(txtServerAddress.Text);
int serverPort = int.Parse(txtServerPort.Text);
_epServer = new IPEndPoint(serverIP, serverPort);
//il pacchetto creato viene convertito in un'array di byte
_dataStream = sendData.ToByteArray();
//il pacchetto creato viene spedito al server
_clientSocket.BeginSendTo(_dataStream, 0, _dataStream.Length, SocketFlags.None, _epServer, SendData, null);
//tutti gli oggetti vengono sempre passati per referenza
//il client si mette ora in ascolto dei messaggi provenienti dal server
EndPoint ep = _epServer;
_dataStream = new byte[Properties.Settings.Default.MAX_PACKET_LENGTH];
_clientSocket.BeginReceiveFrom(_dataStream, 0, _dataStream.Length, SocketFlags.None, ref ep, ReceiveData, null);
}
示例2: StartListening
public void StartListening()
{
EndPoint localEp = ( EndPoint )_iep;
Socket listener = new Socket( AddressFamily.InterNetwork,
SocketType.Dgram,
ProtocolType.Udp );
try
{
listener.Bind( localEp );
while ( true )
{
allDone.Reset();
StateObject so = new StateObject();
so.workSocket = listener;
Console.WriteLine( "Waiting for a connection..." );
IAsyncResult result =
listener.BeginReceiveFrom( so.buffer,
0,
StateObject.BUFFER_SIZE,
SocketFlags.None,
ref localEp,
new AsyncCallback( this.OnReceive ), so );
allDone.WaitOne();
}
}
catch ( Exception e )
{
Console.WriteLine( e.ToString() );
}
}
示例3: CreateListener
private IAsyncResult CreateListener()
{
_testSocket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
_testSocket.Bind(new IPEndPoint(IPAddress.Any, 12721));
byte[] inbuffer = _bufferPool.Dequeue();
EndPoint ep = new IPEndPoint(IPAddress.Any, 1234);
return _testSocket.BeginReceiveFrom(inbuffer, 0, inbuffer.Length, SocketFlags.None, ref ep, null, null);
}
示例4: Bf2Available
public Bf2Available()
{
// Creates an IPEndPoint to reference the IP Address and port number of the sender.
EndPoint RemoteIpEndPoint = new IPEndPoint(IPAddress.Any, 0);
// Bind to local address and begin accepting connections
UdpSock = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
UdpSock.Bind(new IPEndPoint(IPAddress.Loopback, 27900));
UdpSock.BeginReceiveFrom(buffer, 0, buffer.Length, SocketFlags.None, ref RemoteIpEndPoint, DoReceiveFrom, UdpSock);
}
示例5: process
/**
* Connect and receive data for the thread
*/
public override void process()
{
this.receiveSocket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
this.bindEndPoint = new IPEndPoint(IPAddress.Parse(this.host), this.port);
this.recBuffer = new byte[this.BufferSize];
receiveSocket.Bind(bindEndPoint);
receiveSocket.BeginReceiveFrom(recBuffer, 0, recBuffer.Length,
SocketFlags.None, ref bindEndPoint,
new AsyncCallback(MessageReceivedCallback), (object)this);
}
示例6: MulticastCommandServer
public MulticastCommandServer(string uid)
{
this.uid = uid;
this.DscLog = new DscLog();
this.sock = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
EndPoint ep = (EndPoint)(new IPEndPoint(IPAddress.Any, MulticastEP.Port));
this.sock.Bind(ep);
this.sock.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.AddMembership, new MulticastOption(MulticastEP.Address));
Trace.WriteLine(String.Format("Multicast JSON-RPC server: {0}:{1}", MulticastEP.Address, MulticastEP.Port));
sock.BeginReceiveFrom(dataIn, 0, dataIn.Length, SocketFlags.None, ref ep, new AsyncCallback(ReceiveData), ep);
}
示例7: Start
public void Start()
{
serverSocket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
IPEndPoint ipEndPoint = new IPEndPoint(IPAddress.Any, Port);
serverSocket.Bind(ipEndPoint);
IPEndPoint ipeSender = new IPEndPoint(IPAddress.Any, 0);
EndPoint epSender = (EndPoint)ipeSender;
serverSocket.BeginReceiveFrom(byteData, 0, byteData.Length, SocketFlags.None, ref epSender, new AsyncCallback(OnReceive), epSender);
}
示例8: UdpListener
/// <summary>
/// Bind to the
/// </summary>
/// <param name="port"></param>
public UdpListener(IPAddress serverAddress, int port, ServerFilter filter)
{
this.filter = filter;
UDPSocket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
UDPSocket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Broadcast, true);
UDPSocket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
UDPSocket.Bind(new IPEndPoint(serverAddress, port));
EndPoint tempRemoteEP = (EndPoint)sender;
UDPSocket.BeginReceiveFrom(buff, 0, buff.Length, SocketFlags.None, ref tempRemoteEP, GotUdpMessage, tempRemoteEP);
}
示例9: ProxySocket
public ProxySocket(int port)
{
//Setup the socket and message buffer
udpSock = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
udpSock.Bind(new IPEndPoint(IPAddress.Any, port));
buffer = new byte[1024];
//Start listening for a new message.
EndPoint newClientEP = new IPEndPoint(IPAddress.Any, 0);
udpSock.BeginReceiveFrom(buffer, 0, buffer.Length, SocketFlags.None, ref newClientEP, DoReceiveFrom, udpSock);
}
示例10: Start
public void Start(int port, int buf_len)
{
buffer = new byte[buf_len];
// setup the socket
socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
socket.Bind(new IPEndPoint(IPAddress.Any, port));
// start listening
EndPoint newClientEP = new IPEndPoint(IPAddress.Any, 0);
socket.BeginReceiveFrom(buffer, 0, buffer.Length, SocketFlags.None, ref newClientEP, new AsyncCallback(DoReceiveFrom), socket);
}
示例11: Start
public void Start()
{
GkDatabase = DescriptorsManager.GkDatabases.FirstOrDefault();
serverSocket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
IPEndPoint ipEndPoint = new IPEndPoint(IPAddress.Any, 1025);
serverSocket.Bind(ipEndPoint);
IPEndPoint ipeSender = new IPEndPoint(IPAddress.Any, 0);
EndPoint epSender = (EndPoint)ipeSender;
serverSocket.BeginReceiveFrom(byteData, 0, byteData.Length, SocketFlags.None, ref epSender, new AsyncCallback(OnReceive), epSender);
}
示例12: Start
public static void Start()
{
_thread = new Thread(new ThreadStart(Run));
_thread.Start();
_connection = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
_connection.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReceiveTimeout, 500);
_connection.Bind(new IPEndPoint(IPAddress.Any, 0));
obtainedData = new byte[100 * 1024];
obtainedIP = new IPEndPoint(IPAddress.Loopback, 12345); // should be useless?
_connection.BeginReceiveFrom(obtainedData, 0, obtainedData.Length, SocketFlags.None, ref obtainedIP, new AsyncCallback(QueryReceived), null);
Servers = new Dictionary<IPEndPoint, ServerData>();
}
示例13: AwaitConnectionTimer_Tick
private void AwaitConnectionTimer_Tick(object sender, EventArgs e)
{
if (!_connected)
{
LogMessage("Server not found. I am server now!");
_awaitConnectionTimer.Stop();
_udpSocket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
_udpSocket.Bind(_ipep);
EndPoint ep = (EndPoint)_ipep;
_udpSocket.BeginReceiveFrom(_buffer, 0, _buffer.Length, SocketFlags.None, ref ep, new AsyncCallback(ListenerRecieveCallback), _udpSocket);
TcpServerInit();
}
}
示例14: button1_Click
private void button1_Click(object sender, EventArgs e)
{
if (socket != null)
return;
socket=new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.IP);
socket.Bind(new IPEndPoint(IPAddress.Parse("10.2.21.129"), 100));
state.workSocket = socket;
RcptRes = socket.BeginReceiveFrom(state.buffer,
0,
StateObject.BufferSize,
SocketFlags.None,
ref ClientEP,
new AsyncCallback(Receive_Completed), state);
}
示例15: AwaitConnectionTimer_Tick
private void AwaitConnectionTimer_Tick(object sender, EventArgs e)
{
LogMessage("Connection timer elapsed.");
_awaitConnectionTimer.Stop();
// TODO: а нам это надо?
if (!_connected)
{
_udpSocket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
_udpSocket.Bind(_ipep);
EndPoint ep = (EndPoint)_ipep;
_udpSocket.BeginReceiveFrom(_buffer, 0, _buffer.Length, SocketFlags.None, ref ep, new AsyncCallback(ListenerRecieveCallback), _udpSocket);
LogMessage("Waiting for clients...");
}
}