Find Out Connected clients IP's? - c#

How to find out Connected Clients Ip Address. And how we can store that address to an array of datatype IPAddress?

Check the RemoteEndPoint of your socket:
If you are using a connection-oriented
protocol, the RemoteEndPoint property
gets the EndPoint that contains the
remote IP address and port number to
which the Socket is connected. If you
are using a connectionless protocol,
RemoteEndPoint contains the default
remote IP address and port number with
which the Socket will communicate. You
must cast this EndPoint to an
IPEndPoint before retrieving any
information. You can then call the
IPEndPoint.Address method to retrieve
the remote IPAddress, and the
IPEndPoint.Port method to retrieve the
remote port number.
If you use higher level components like TcpListener and TcpClient then you can access the underlying socket and retrieve the remote end point.
If you use other technologies like ASP.Net, WCF or Remoting then you must say so in your post.
To store an IPAddress you retrieve the underlying bytes using IPAddress.GetAddressBytes. You reconstruct the address from the bytes using the byte[] constructor.

Are we in Windows? Do you need to use this information inside an application or a console command could do the trick?
maybe you could try with netstat -na in a shell.

Related

Udpclient polling UDP multicast address

I want to check if data is avaiable on the UDP multicast address, the only mechanism I found was the Poll method.
Setup:
client = new UdpClient(localEp);
client.JoinMulticastGroup(multicastAddr, localIpAddress);
client.Connect(multicastAddr, receiveport);
Polling:
if (!client.Client.Poll(100, SelectMode.SelectRead))
The client is connected (I checked) but never returns true.
In wireshark I can see the udp datagrams are sent correctly.
Suggestions for fixes?
Edit:
IPEndpoint localEp = local ipv4 unicast address, port for multicast udps
receiveport = port for receiving multicasts
multicastaddr = IPAddress
Connecting UDP socket means restricting datagram source address and port to the specified pair when receiving, and setting default destination address and port when sending.
Remove call to Connect().
Edit 0:
You need to bind to the multicast address, not the local IP. Either remove localEp from the constructor, or replace it with pair of the multicast group/port. See examples on MSDN.
And you are wrong, you can Receive() just one datagram.

DatagramPacket Equivalent

What is a equivalent for this java code in c#
DatagramPacket dp = new DatagramPacket(output,output.length,pack.getAddress(),pack.getPort());
socket.send(dp);
where pack - DatagramPacket, and socket - DatagramSocket?
System.Net.Sockets.UdpClient provides User Datagram Protocol (UDP) network services.
The UdpClient class provides simple methods for sending and receiving connectionless UDP datagrams in blocking synchronous mode. Because UDP is a connectionless transport protocol, you do not need to establish a remote host connection prior to sending and receiving data. You do, however, have the option of establishing a default remote host in one of the following two ways:
Create an instance of the UdpClient class using the remote host name and port number as parameters.
Create an instance of the UdpClient class and then call the Connect method.
And:
Send(Byte[], Int32) Sends a UDP datagram to a remote host.
Or alternatively:
Send(Byte[], Int32, IPEndPoint) Sends a UDP datagram to the host at the specified remote endpoint.
This last one more closely matches your example code.

Get local IP address for socket

I'm looking to get the local IP address of the socket I just created. I need to be able to support a server with more than one NIC and communicate back to the requesting client what the direct IP address is to connect later on. I'm using for following code:
Socket rsock = null;
rsock= new Socket(AddressFamily.InterNetworkV6, SocketType.Stream, ProtocolType.Tcp);
rsock.SetSocketOption(SocketOptionLevel.IPv6, SocketOptionName.IPv6Only, 0);
rsock.Bind(new IPEndPoint(IPAddress.IPv6Any, port));
rsock.Listen((int)SocketOptionName.MaxConnections);
After this point, .LocalEndPoint kicks out: [::]:PORT.
Background:
The reason I need the IP address is that a secondary connection by another client will need to return to this specific server. These servers will likely be behind a load balancer for the initial server selection so the client cannot resolve the IP address based on the host name.
Since you're binding to IPAddress.IPv6Any, the endpoint information will not be available before the first I/O operation occurs. The documentation says:
If you allow the system to assign your socket's local IP address and
port number, the LocalEndPoint property will be set after the first
I/O operation. For connection-oriented protocols, the first I/O
operation would be a call to the Connect or Accept method.
So, in your case, you will have to call Accept() before accessing LocalEndPoint in order to obtain meaningful information.

How to send request from different static IP address to a any website?

I am sending HTTP request using C#. (http://codesamplez.com/programming/http-request-c-sharp)
I have dedicated server on.I have purchased more static IPs.
How can I send request using these different IPs.
What you want to do is bind to a specific network adapter. By default the LocalEndpoint is null so your connection will be to assigned an adapter. You can specify what to bind to using HttpWebRequest.ServicePoint.BindIPEndPointDelegate.
var req = (HttpWebRequest)WebRequest.Create("http://google.com/");
req.ServicePoint.BindIPEndPointDelegate = BindTo;
using (req.GetResponse());
static IPEndPoint BindTo(ServicePoint servicepoint, IPEndPoint remoteendpoint, int retrycount)
{
IPAddress ip = IPAddress.Any; //This is where you specify the network adapter's address
int port = 0; //This in most cases should stay 0. This when 0 will bind to any port available.
return new IPEndPoint(ip, port);
}
Here is some more information on binding from msdn.
Use the Bind method if you need to use a specific local endpoint. You
must call Bind before you can call the Listen method. You do not need
to call Bind before using the Connect method unless you need to use a
specific local endpoint. You can use the Bind method on both
connectionless and connection-oriented protocols.
Before calling Bind, you must first create the local IPEndPoint from
which you intend to communicate data. If you do not care which local
address is assigned, you can create an IPEndPoint using IPAddress.Any
as the address parameter, and the underlying service provider will
assign the most appropriate network address. This might help simplify
your application if you have multiple network interfaces. If you do
not care which local port is used, you can create an IPEndPoint using
0 for the port number. In this case, the service provider will assign
an available port number between 1024 and 5000.
If you use the above approach, you can discover what local network
address and port number has been assigned by calling the
LocalEndPoint. If you are using a connection-oriented protocol,
LocalEndPoint will not return the locally assigned network address
until after you have made a call to the Connect or EndConnect method.
If you are using a connectionless protocol, you will not have access
to this information until you have completed a send or receive.

Sending a message through a particular client port

I am doing projects in sockets.usually the server listens in a particular port,and the client has to connect to the port then the send and receives will happen.but we don't specify any port number in the client side,but i am in a situation to use a port in a client side, through this port only the messages will delivered to the server.how to do this?
In my client side they are restricting the ports ,so if want use to a valid free port i have to set it in the client program,instead of OS Choosing it.This is my problem.
Bind the client socket to local address (ip and port number) before connecting to server. Be ready to handle errors e.g. when the port is not available (choose next port, retry).
I guess you're using the System.Net.Sockets namespace?
If so, classes like NetworkStream take the Socket as a constructor parameter:
http://msdn.microsoft.com/en-us/library/system.net.sockets.networkstream.aspx
Similarly, the TcpClient takes Port and Server as constructor arguments, Port is the same as socket in this context:
http://msdn.microsoft.com/en-us/library/system.net.sockets.tcpclient.aspx
Finally. you can control the number of this socket in a few ways:
Command Line Parameter
Setting in an Application.Config file
Read it from the Registry
There are a few methods for this type of thing.

Categories