Mostafa
Mostafa

Reputation: 21

How to find socket's ip C# BeginReceive

I Listen to specific port with BeginReceive() method.
How I can find sender's IP?

Thank you.

Upvotes: 1

Views: 1504

Answers (1)

Fulvio
Fulvio

Reputation: 1647

I think you should elaborate your questions with more details.

Anyway you can find a complete example on how to use BeginReceive() and the other asynchronous methods of the Socket class in a clear and detailed "Asynchronous Server Socket Example" MSDN Sample.

The trick as you will read, is to initialize a custom state object (where you can put the opened Socket information) when you accept the connection and you are going to call BeginReceive, so that the receive callback will be able to retrieve that state with IAsyncResult.AsyncResult property.

Following the linked example, you can retrieve the "sender" (remote) ip address this way (in AcceptCallback, ReadCallback, where handler is an istance of the Socket class):

IPAddress remoteAddress = ((IPEndPoint)handler.RemoteEndPoint).Address;

Upvotes: 3

Related Questions