When using the online hosted version of Visual Studio Team Services, my unit tests are unable to connect to a service listening on a TCP port on the localhost of the build agent. The service is able to start and open the TCP port but it seems unreachable for the unit test.
Error message:
2017-06-20T12:05:00.8231306Z ##[error]------------
System.Net.Http.HttpRequestException : An error occurred while sending
the request. 2017-06-20T12:05:00.8231306Z ##[error]----------------
System.Net.WebException : Unable to connect to the remote server
2017-06-20T12:05:00.8231306Z ##[error]--------------------
System.Net.Sockets.SocketException : No connection could be made
because the target machine actively refused it 127.0.0.1:41670
The service that opens the TCP port is started with:
public void Start()
{
HttpPort = ObtainFreePort();
TcpPort = ObtainFreePort();
ClusterVNode node = EmbeddedVNodeBuilder.AsSingleNode()
.WithInternalTcpOn(new IPEndPoint(IPAddress.Loopback, TcpPort))
.WithExternalTcpOn(new IPEndPoint(IPAddress.Loopback, TcpPort))
.WithInternalHttpOn(new IPEndPoint(IPAddress.Loopback, HttpPort))
.WithExternalHttpOn(new IPEndPoint(IPAddress.Loopback, HttpPort))
.AddExternalHttpPrefix($"http://+:{HttpPort}/")
.RunProjections(ProjectionsMode.All)
.StartStandardProjections()
.RunInMemory()
.Build();
node.StartAndWaitUntilReady().Wait();
}
static int ObtainFreePort()
{
using (var sock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp))
{
sock.Bind(new IPEndPoint(IPAddress.Loopback, 0));
var port = ((IPEndPoint)sock.LocalEndPoint).Port;
sock.Close();
return port;
}
}
This does work on my local machine :) Is this not supported in Visual Studio Team Services online?
If you're using the hosted agent, you can't open up ports or change anything about the machine's configuration. You'll need to set up your own agent for builds.
Also, if a test requires TCP communication, it's no longer a unit test. Unit tests have no external dependencies.
Related
I develop for hololens 2 in unity and now got a problem with socket communication.
I am trying to get TCP client on hololens 2 and if I want to connect to TCP server I got the following error on my glasses (in unity it works):
System.Exception: An attempt was made to access a socket in a way forbidden by its access permissions.
I already checked the permissions in Player Settings: InternetClient, InternetClientServer, PrivateNetworkClientServer
Using:
Unity 2019.4.2f1
Api Compatibility Level .NET 4.x
Scripting Backend: IL2CPP
Target SDK Version: 10.0.18362.0
using: Windows.Networking.Sockets.StreamSocket
any suggests?
I faced the connection issue a long time ago while working on Hololens1. I haven't worked on Hololens2 network protocols. But one of the biggest problem I faced was Hololens UWP C# and Unity C# are different from each other and they create a conflict. You may need to specify the directive for the communication. Check out this link. This guys explained the problem and solution really well.
My suggestion is if you are not sending constant requests and periodically sending the data, try creating a server based application which accepts REST API calls. That will be a much robust solution.
I am using Unity 2019.4.13f1 and tcp connection works fine.
Example code:
public string ServerIp = "192.168.31.12";
private const int PORT = 9999;
private Socket Client;
public void CreateClient()
{
Debug.Log("\r\nCreating Client...");
Thread createClientAndConnect = new Thread(() =>
{
try
{
IPEndPoint serverEP = new IPEndPoint(IPAddress.Parse(ServerIp), PORT);
Client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
Client.Connect(serverEP);
Thread listenToServer = new Thread(Receive);
listenToServer.IsBackground = true;
listenToServer.Start(Client);
}
catch (Exception e)
{
Debug.LogError("Error when creating client");
Debug.LogError(e.Message);
}
});
createClientAndConnect.IsBackground = true;
createClientAndConnect.Start();
Debug.Log("Client is created");
}
I've implemented a Xamarin app that successfully peers to another instance of itself running on another device using WiFiP2PManager, OnPeersAvailable and OnConnectionInfoAvailable (etc.).
My challenge now is I'd like the group owner to specify the PORT the client(s) will use to connect.
SERVER
var serverSocket = new ServerSocket(port); <<< PASS THIS PORT NUMBER TO THE CLIENT.
client = serverSocket.Accept();
CLIENT
client = new Socket();
InetSocketAddress socketAddress = new InetSocketAddress(address, port); <<< PORT PASSED FROM THE SERVER
client.Connect(socketAddress);
Is there any way I can pass additional information to the peer that it can use for connection, such as the port number?
Thanks
-John
I am developing a peer to peer call. I am using de.javawi.jstun.test .
I found this constructor in de.javawi.jstun.test.DiscoveryTest .
public DiscoveryTest(InetAddress sourceIaddress, int sourcePort, String stunServer, int stunServerPort) {
this.sourceIaddress = sourceIaddress;
this.sourcePort = sourcePort;
this.stunServer = stunServer;
this.stunServerPort = stunServerPort;
}
My question is What is the difference between the Source Port and the StunServerPort??
stunServerPort is the port the STUN server listens on for incoming binding requests. This is typically one of the standard STUN ports: 3478 or 3479.
sourcePort is the port the client behind a NAT has obtained locally to create a socket with. Most often, the client attempting to do P2P will ask the OS to randomly pick an available local port to send/receive from. You can probably pass 0 for sourcePort and let it pick the port for you as well. Or if you already have a socket, use the same port as your local, and DiscoveryTest will set the reuseaddr flag so it can have a socket co-exist.
I wrote a simple client side program that creates a socket using
CFSteamCreatePairWithSocketToHost function
and connects to the server that runs on the local host on port 8080. It creates the socket just fine but it never connects to the server. I wrote the server in C. It didn't work and gave me a
kCFErrorDomainCFNetwork error 72000
and the only information that relays is that apparently the TCP connection couldn't be made don't know why though. So I tried to write the client side script in C too and added it to my Swift project bridging header and all but it still doesn't connect. It creates the socket just fine but it fails to connect to the server and I have no idea why.
But the same C client script worked when I compiled it using clang and ran it but didn't connect when I ran it with my swift project in Xcode. Is my mac blocking the libraries from making a TCP connection or something?
I don't even know what to search for. The only thing I found was an issue on a Github library called starscream which had the same errors I had and I'm not even using that library and the reply there was "the only thing we can discern from this error is that the TCP connection was unsuccessful".
Here's the code I used to connect to the server using Swift 4. The server is running on port 8080 on localhost.
class client:NSObject {
var inputstream = InputStream!
var outputstream = OutputStream!
func setupNetworkCom() {
var readstream = Unmanaged<CFReadStream>?
var writestream = Unmanaged<CFWriteStream>?
CFStreamCreatePairWithSocketToHost(kCFAllocatorDefault, "localhost" as CFString, 8080, &readstream, &writestream)
inputstream = readstream!.takeRetainedValue()
outputstream = writestream!.takeReatainedValue()
inputstream.schedule(in: .current, forMode: .common)
outputstream.schedule(in: .current, forMode: .common)
inputstream.open()
outputstream.open()
}
}
I've also tried replacing "localhost" with "127.0.0.1" which is the IP I specified for the server to run on but it still doesn't work.
click on your project settings and go to capabilities there you'll see the app sandbox. make sure it's turned on and then enable incoming connections and outgoing connections.
I am running kafka10 on a remote server and the JMX is enabled on port 9999.
JMX URL :- service:jmx:rmi:///jndi/rmi://kafka_host:9999/jmxrmi
When i try to connect to the JMX from my local network, it works fine and getting response within few secs.
But when i try to connect from my test servers, we get connection refused.
The ACL is opened from this host to the kafka host on port 9999.
Application code,
TopicJMXMetrics jmxMetrics = new TopicJMXMetrics();
JMXServiceURL serviceURL = new JMXServiceURL(jmxUrl);
JMXConnector connector = JMXConnectorFactory.connect(serviceURL, null);
MBeanServerConnection connection = connector.getMBeanServerConnection();
Is there any other setting required from Kafka or on the Application Side? Or, any other port to be opened
Thanks