I'm now using a socket server in C# but I can only bind it to 127.0.0.1.
But it has to bind to my VPS IP. Even if I try to bind to my hamachi IP, it doesn't work.
I use:
ServerSocketSettings Settings = new ServerSocketSettings
{
MaxConnections = Config.ServerMaxConnections,
NumOfSaeaForRec = Config.ServerMaxConnections,
Backlog = 30,
MaxSimultaneousAcceptOps = 15,
BufferSize = 512,
Endpoint = new IPEndPoint(IPAddress.Parse("25.168.77.190"), Config.ServerPort)
};
this._serverSocket = new ServerSocket(Settings);
Then I do:
this.ListenSocket = new Socket(this.Settings.Endpoint.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
this.ListenSocket.Bind(this.Settings.Endpoint);
this.ListenSocket.Listen(this.Settings.Backlog);
the this.Settings is the value of the code above.
When I run it, I get:
The requested address is not valid in its context
I'm wondering why it doesn't work.
You should bind the Listening Socket only to a specific Interface if you want to limit the availability of the service to this network segment.
If this is not needed you can just bind it to any ip address with
this.ListenSocket.Bind(new IPEndPoint(IPAddress.Any));
Related
I am trying to make some UDP communication between 2 different address, so my sending code looks like:
var localEndpoint = new IPEndPoint(IpAddress.Parse("192.168.2.10"), 51111)
var remoteEndpoint = new IPEndPoint(IpAddress.Parse("192.168.1.10"), 51111)
var sender = new UdpClient (localEndpoint)
sender.Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.DontRoute, 1);
sender.Send (request.RawRequest, request.RawRequest.Length, remoteEndpoint) |> ignore
sender.Close ()
As you see those are 2 different address, so I am binding interface address to my client and turning off routing. But when I try this I am getting unreachable network message, but when I try same address families well it works.
I also do broadcast to this destination (that is 192.168.1.2) where endpoint is:
new IPEndPoint(IpAddress.Parse("255.255.255.255"), 51111)
sender.Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Broadcast, 1);
And it works.
What am I doing wrong?
I am working on a service discovery implementation in C#. In an attempt to get around some issues, I have implemented both broadcast and multicast, simplified to the snippets shown.
Client side broadcast:
var requestData = new byte[]{ /* Whatever */}
// Configure broadcast
var ipBroadcastEndPoint = new IPEndPoint(IPAddress.Broadcast, 12301);
var broadcastServerEp = new IPEndPoint(IPAddress.Any, 0);
var broadcastClient = new UdpClient {EnableBroadcast = true};
broadcastClient.Client.Bind(_broadcastServerEp);
// Send a request:
broadcastClient.Send(requestData, requestData.Length, ipBroadcastEndPoint);
// Listen for a reply:
var broadcastserverEp = new IPEndPoint(IPAddress.Any, 0);
var broadcastserverResponseData = broadcastClient.Receive(ref broadcastserverEp);
Client side multicast:
var requestData = new byte[]{ /* Whatever */}
// Configure multicast
var ipMulticastEndPoint = new IPEndPoint(IPAddress.Parse("239.255.255.253"), 12302);
var multicastServerEp = new IPEndPoint(IPAddress.Any, 0);
var multicastClient = new UdpClient(AddressFamily.InterNetwork);
multicastClient.JoinMulticastGroup(IPAddress.Parse("239.255.255.253"), 32);
multicastClient.Client.Bind(_multicastServerEp);
// Send a request:
multicastClient.Send(requestData, requestData.Length, ipMulticastEndPoint);
// Listen for a reply:
var multicastserverEp = new IPEndPoint(IPAddress.Any, 0);
var multicastserverResponseData = multicastClient.Receive(ref multicastserverEp);
Server side broadcast:
// Listen for broadcast
Task.Run(() =>
{
var broadcastServer = new UdpClient(12301);
while(/**/)
{
var broadcastclientEp = new IPEndPoint(IPAddress.Any, 0);
var broadcastclientRequestData = broadcastServer.Receive(ref broadcastclientEp);
// Reply to broadcast:
var responseData = new byte[]{ /* Whatever */}
broadcastServer.Send(responseData, responseData.Length, broadcastclientEp);
}
}
Server side multicast:
// Listen for multicast
Task.Run(() =>
{
var multicastServer = new UdpClient(12302);
multicastServer.JoinMulticastGroup(IPAddress.Parse("239.255.255.253"));
while(/**/)
{
var multicastclientEp = new IPEndPoint(IPAddress.Any, 0);
var multicastclientRequestData = multicastServer.Receive(ref multicastclientEp);
// Reply to multicast:
var responseData = new byte[]{ /* Whatever */}
multicastServer.Send(responseData, responseData.Length, multicastclientEp);
}
}
If both client and server components are running on the same computer, both broadcast and multicast work as expected.
If both client and server components are on different computers in the same subnet, broadcast works, multicast works in one direction only (two computers in a given WLAN topology, multicast works when one of them is in the server role, but not the other way around).
If both client and server components are on different subnets in the same corporate network, neither broadcast or multicast works.
I'd like to get this to work reliably across subnets. From what I've learned so far, I know broadcast won't work, but multicast should.
Is there some magic to the selection of multicast addresses and/or port numbers? In other words, are there specific addresses and ports I should or should not be using for it to work reliably regardless of network topology? Are there address/port combinations which are customarily filtered or customarily kept open? Are there any configuration options I have overlooked?
Allowing multicast traffic to pass between subnets is controlled entirely by the routers between those subnets.
The routers need to be configured to either with static multicast routes, or to accept IGMP messages from hosts to set up multicast groups as they are joined.
I dont have much knowledge on WCF. I have a WPF tablet application using WCF service to communicate with another instance of WPF application on other tablet.
WPF application places a pull request from other tablet's database via WCF service. This WCF service on the tablet fetches the data from database and sends it over TCP back to the requesting application. Same happens vice versa. Hence the synchronization is achieved. It happens over WiFi interface.
private const string WCFSERVICE_URL = "net.tcp://{0}:{1}/SynchronizeService"
Some code where we configure the client:
private DrillSynchronizeClient CreateConfigureClient(DrillNetInfo Drill)
{
DrillSynchronizeClient synchClient = new DrillSynchronizeClient();
string endpointUrl = String.Format(WCFSERVICE_URL, Drill.Ip, wcfServiceNetworkPort);
EndpointAddress serviceAddress = new EndpointAddress(endpointUrl);
NetTcpBinding netTcpBinding = new NetTcpBinding();
netTcpBinding.Security.Mode = SecurityMode.None;
netTcpBinding.Security.Transport.ClientCredentialType = TcpClientCredentialType.None;
netTcpBinding.OpenTimeout = new TimeSpan(0, 5, 0);
netTcpBinding.CloseTimeout = new TimeSpan(0, 5, 0);
netTcpBinding.SendTimeout = new TimeSpan(0, 5, 0);
netTcpBinding.ReceiveTimeout = new TimeSpan(0, 5, 0);
netTcpBinding.MaxBufferPoolSize = int.MaxValue;
netTcpBinding.MaxBufferSize = int.MaxValue;
netTcpBinding.MaxReceivedMessageSize = int.MaxValue;
synchClient.Endpoint.Address = serviceAddress;
synchClient.Endpoint.Binding = netTcpBinding;
return synchClient;
}
This works fine when there is not internet connection available on these tablets.
When tablets are connected to internet via internet USB dongle, this synchronization stops working. Is the WCF trying to use the internet adapter(dongle) to resolve the WCF service IP address endpoint ?? Not sure, how to bind the WCF service to use only WLAN interface for communication.
Also please let me know where to specify/bind the source interface IP address to use when calling WCF request.
Thanks.
I've made a programm, which sends HTTP-webrequests.
To secure these requests being sent, I want to have the ability to define an IP, over which the request should be sent.
So in case of the default exit not working the request should be sent via another exit.
I've tried doing that by using IPEndPoint and Sockets but somehow it's not working.
Here is my Code so far
public Response ExecuteRequest(RequestData requestData, NetworkAddress exit) {
Tracer.Info("Versuche Request über HTTP abzusetzen.");
if (!PrepareIpEndPoint(Address, exit)) {
return null;
}
Address = new Uri(PlaceholderReplacer.ReplacePlaceholders(Address.ToString(), requestData));
return RequestExecutor.ExecuteRequest(Address);
}
private bool PrepareIpEndPoint(Uri url, NetworkAddress exit) {
Tracer.Debug(string.Format("Setze den IP-Endpoint auf{0}", exit));
var ipEndPoint = new IPEndPoint(IPAddress.Parse(exit.Address), 0);
var tempSocket = new Socket(ipEndPoint.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
tempSocket.Bind(ipEndPoint);
tempSocket.Connect(ipEndPoint);
return tempSocket.Connected;
}
the above code is throwing SocketExceptions.
Any ideas are greatly appreciated.
Thanks, Josh
Remove this line: (it's for opening up a port for accepting clients)
tempSocket.Bind(ipEndPoint);
You want to act as a client by connecting to the server with:
tempSocket.Connect(ipEndPoint);
Are you really sure that you want to connet to port zero, i'm pretty sure this is not a valid choice. (standard HTTP = 80, HTTPS = 443)
var ipEndPoint = new IPEndPoint(IPAddress.Parse(exit.Address), 80);
I have this code:
IPHostEntry host = null;
Socket sock;
host = Dns.GetHostEntry("ip..");
foreach (IPAddress address in host.AddressList)
{
IPEndPoint ipe = new IPEndPoint(address, 7777);
sock = new Socket(ipe.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
sock.Connect(ipe);
if (sock.Connected)
{
sock.SendTo(Encoding.UTF8.GetBytes("Hello world"), ipe);
}
}
This code works ok on localhost, but when I write vps ip, code not working, what's problem?
It would seem your DNS is set up incorrectly and Dns.GetHostEntry(string) fails at the second point below. If the DNS server fails to do a reverse lookup it won't return a hostname, so Dns.GetHostEntry(string) doesn't know what to look up and will return an empty address list.
From MSDN: https://msdn.microsoft.com/en-us/library/ms143998.aspx
The method tries to parse the address. If the hostNameOrAddress parameter contains a legal IP string literal, then the first phase succeeds.
A reverse lookup using the IP address of the IP string literal is attempted to obtain the host name. This result is set as the HostName property.
The host name from this reverse lookup is used again to obtain all the possible IP addresses associated with the name and set as the AddressList property.