How can i specify specific ipaddress:port for udpclient to receive? im confused with updclient.client.localendpoint and udpclient.client.remoteendpoint. i was thinking maybe its remoteendpoint, but i cannot display remoteendpoint value. plus my local endpoint ip is 0.0.0.0. what is that mean?
0.0.0.0 means "any address". The local endpoint is the IP address and port you will listen for packets on. The remote endpoint is not required for UdpClient objects. It only specifies the default host to send a packet to, if you do not specify one when sending the packet.
You can leave the local endpoint as 0.0.0.0 if you want to listen for traffic on all of your assigned IP addresses on all of your network connections. You only need to change this if you only want to listen on one particular address/connection.
Related
I want to listen for HTTP requests and TCP connections on the same port but on different IP addresses.
string prefix = "http://192.168.1.2:40000/";
HttpListener http = new HttpListener();
http.Prefixes.Add(prefix);
IPEndPoint ep = new IPEndPoint(IPAddress.Parse("192.168.1.3"), 40000);
TcpListener tcp = new TcpListener(ep);
If I start the HttpListener first, I get an error when starting the TcpListener.
An attempt was made to access a socket in a way forbidden by its access permissions
If I start the TcpListener first, I get an error when starting the HttpListener.
The process cannot access the file because it is being used by another process
When the HttpListener is running, netstat reveals that it's only listening on the IP address specified, but it's running in the System process (PID 4).
Proto Local Address Foreign Address State PID
TCP 192.168.1.2:40000 0.0.0.0:0 LISTENING 4
When the TcpListener is running, it's also only listening on the IP address specified, but it's running in my application's process.
Proto Local Address Foreign Address State PID
TCP 192.168.1.3:40000 0.0.0.0:0 LISTENING 18316
Even though they listen on different IP addresses, there's still a conflict that won't let me do both at the same time.
I am able to run two HttpListeners and two TcpListeners on different IP addresses with the same port, however.
Update
The question was asked:
How do you have assigned two local IP addresses on the same LAN segment?
Originally I had two IP addresses on the same network adapter in the same subnet (255.255.0.0). (See how this is possible at https://superuser.com/questions/571575/connect-to-two-lan-networks-with-a-single-card).
To rule out this as an issue, I setup a virtual machine with two network adapters on different subnets. The results were the same.
Apparently you must tell HTTP.sys which IP addresses to listen on because it hijacks them all by default.
In my case, running the following command allowed me to run HttpListener and TcpListener on the same port on different IP addresses.
netsh http add iplisten 192.168.1.2
Sources
Disabling HTTPS Socket Pooling
add iplisten
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.
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.
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.
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.