TCP Socket not receiving connections - c#

I am working a local proxy app. I have already implemented the proxy code in C# and it is working fine and bug free on Windows, Linux, and Mac OSx. However when I try to run the system on Android, I face this issue:
The socket starts without any problem
The socket starts listening to 127.0.0.1 8080
I set a global proxy by going to Wi-Fi and connection setting and I point it to 127.0.0.1 8080
But when I start browsing something, the TCP socket receives no connection
The TCP socket has been implemented using the following approach:
Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
socket.Bind(new IPEndPoint(IPAddress.Parse("127.0.0.1"), 8080));
socket.Listen(1000);
while (true)
{
try
{
Socket accept = socket.Accept();
// I don't reach here
}
}
I start it in another thread so that the app would not get irresponsive.
In the beginning I thought it's a permission problem so I set the following permissions as well:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
<application android:allowBackup="true" android:usesCleartextTraffic="true" android:icon="#mipmap/appicon" android:roundIcon="#mipmap/appicon_round" android:supportsRtl="true"></application>
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
</manifest>
But the socket starts without any issue as I use break points to see what is going on, and the global proxy setting is correct. Now I expect to receive connections but nothing is received by the socket. In other platforms it works as expected.

Related

Multicast Socket Not Receiving After Disabling/Enabling Network Connection

I'm using Socket with following configuration to receive multicast packets and the code is working properly.
socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
socket.Bind(bindPoint);
socket.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.AddMembership, new MulticastOption(multicastAddress, IPAddress.Any));
socket.ReceiveTimeout = 1000;
But after disabling the network connection from Control Panel and enabling, the socket cannot receive the multicast data. I can see the packets with Wireshark. I tried closing the socket, resetting he socket but restarting is the only solution I came up with. The application will be used in an environment where the user typically remove the ethernet cable and plug another one.
Initially I was using UdpClient but the same problem persists on that too. The operating system is Windows 7 and I use .NET 4.5.2.
After disabling a network connection, Windows automatically uses another connection if you have one (virtual host etc.). After enabling the previous connection, the application doesn't use the correct connection since there were no interface selection made. Disabling other connections or specifying the interface solves the problem.

Can't communicate with service using DNS in service fabric

I'm trying to get a stateless service to send a value to another, just to achieve communication between services by using the DNS-service in service fabric. I've tested both applications with postman, and they work fine. I'm following this tutorial where it seems pretty straight forward to do this.
The DNS-service is enabled:
The stateless service has a DNS-name:
The DNS-name is configured in the ApplicationManifest.xml
<Service Name="SocketService"
ServiceDnsName="SocketService.TimeSeriesActorApplication"
ServicePackageActivationMode="ExclusiveProcess">
<StatelessService ServiceTypeName="SocketServiceType"
InstanceCount="[SocketService_InstanceCount]">
<SingletonPartition />
</StatelessService>
</Service>
I then try to send a http get to the service, just like in the tutorial.
using (var client = new HttpClient())
{
client.BaseAddress = new Uri("http://socketservice.timeseriesactorapplication:8712/api/");
var response = await client.GetAsync("values");
}
But I get an exception:
WebException: The remote name could not be resolved: 'socketservice.timeseriesactorapplication'
This happens both when I use port 8080 like the tutorial suggests, and when I use the port I specify in ServiceManifest.xml.
<Endpoints>
<Endpoint Protocol="http" Name="ServiceEndpoint" Type="Input" Port="8712" />
</Endpoints>
What am I missing here?
Update:
Using localhost instead of dns-name also works fine.
Issue on Github: Unable to resolve service DNS name #332
I see you are running service fabric 5.6.210. The recent 5.6.220 release (https://blogs.msdn.microsoft.com/azureservicefabric/2017/06/20/release-of-sdk-2-6-220-and-runtime-5-6-220-refresh-for-windows/) contains some fixes for the DNS service. Note that even though I am running 5.6.220, I have noticed that the DNS name resolution does not always seem to start working straight away after deployment on my local machine (I have to redeploy or wait a few minutes). If you are running locally you can test the name resolution in a terminal window - just ping your service's DNS name.
I fixed it by using ipconfig /flushdns to refresh DNS.
I also found that local IP is the first DNS server in my DNS chain.

Server Client Application with .NET and Xamarin

I searched around the internet a lot of hours but I couldn't find anything that matches my case.
I simply want to implement a Server/Client App with TCP or UDP where my Android App (Xamarin) acts as a server and my .NET application as Client. Since I have not much experience with app development and no experience with Xamarin, I was looking for an example. All I found was this:
http://www.codeproject.com/Articles/340714/Android-How-to-communicate-with-NET-application-vi
First of all this is the opposite way (Server on .NET and Client as App) and additionaly it is for Android Studio so it's hard for me to translate these things into Xamarin without errors.
Please can someone help and give me an example how to realize my issue?
Thank you!
On Xamarin.Android you can use all of the regular .Net socket classes:
Namespaces:
using System.Net;
using System.Net.Sockets;
Example:
IPHostEntry ipHostInfo = Dns.GetHostEntry (Dns.GetHostName ());
IPAddress ipAddress = ipHostInfo.AddressList [0];
IPEndPoint localEndPoint = new IPEndPoint (ipAddress, 11000);
System.Diagnostics.Debug.WriteLine(ipAddress.ToString());
// Create a TCP/IP socket.
Socket listener = new Socket (AddressFamily.InterNetwork,
SocketType.Stream, ProtocolType.Tcp);
AndroidManifest.xml Required Permissions are:
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
The MSDN-based Asynchronous Server Socket example works as a cut/paste example with no changes.
i.e.
Using the MSDN code, you can call the static method, AsynchronousSocketListener.StartListening, in a thread to start listening on port 11000 defined in the AsynchronousSocketListener class.
new Thread (new ThreadStart (delegate {
AsynchronousSocketListener.StartListening();
})).Start ();
Once it is running on your device/emulator, you can telnet into your Android TCP socket server:
>telnet 10.71.34.100 11000
Trying 10.71.34.100...
Connected to 10.71.34.100.
Escape character is '^]'.
Once connected, type in This is a test<EOF> and the Android will echo it back:
This is a test<EOF>
You do this like in normal .net, except you have to ask permissions to use sockets.
There are tons of simple example of creating a listening tcp connection in c#.
The problem you will have is to know the IP address of your server (in the phone) as it will likely change often when the user is moving.

500 remote exception while Process A try to access remote object from Process B connected via .NET remoting.(Client/server in the same machine)

I have a product with architecture that there are two processes . say Process A and Process B.
Process A is the main process calling Process B to provide some specific values.
Process B is running on port xxxx.
Process A makes a call to Process B (with request). Process B receives the request and makes a proper response but that response never go to Process A. We get a 500 Remote exception in "Charles Web debugging" as a response from Process B.
<system.runtime.remoting>
<application>
<service>
<wellknown type="Some name of the class".
mode="Singleton"
objectUri="ClassName" />
</service>
<channels>
<channel ref="http" port="123456" />
</channels>
</application>
</system.runtime.remoting>
<startup>
<supportedRuntime version="v2.0.50727" />
</startup>
I have checked all the networking, proxies but couldn't find any issue. Independently Process A and process B not giving any errors.

How to connect with TcpClient in C# from behind a proxy?

I'm behind a proxy and can't connect with TcpClient to GMail's POP3. I get the following error:
System.Net.Sockets.SocketException (0x80004005): No such host is known
Any clues?
Code:
var tcpClient = new TcpClient();
try
{
tcpClient.ReceiveTimeout = 60000;
tcpClient.SendTimeout = 60000;
tcpClient.Connect("pop.gmail.com", 995);
output.AppendLine("Connection OK!");
}
catch (SocketException e)
{
output.AppendLine(e.ToString());
}
finally
{
tcpClient.Close();
}
app.config (proxy is set up in Internet Explorer):
<?xml version="1.0"?>
<configuration>
<system.net>
<defaultProxy>
<proxy usesystemdefault="True" />
</defaultProxy>
</system.net>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/>
</startup>
</configuration>
Nothing. Not possible. It looks like:
Your company DNS does not include public DNS information.
So your comptuers ask a proxy which asks an outside DNS.
This heavily implies you also have no possible routing
Without Routing no TCP Connection is possible anyway. By design.
Possible workarounds:
Use a SOCKS proxy. Unlikely to exist.
Ask your IT Department - the proper way to solve this inot to bypass the firwewall, but to send the emails using the proper channel (internal SMTP Service).
If the proper way is using gmail, some not too smart person (i.e. a manager) put you into a corner. Then they have to open a TCP connection possibiltiy for you and make the public DNS information available for your computer.
At the end, you can have it both ways - either you force all clients through a proxy, or you have outgiong direct TCP connections.

Categories