I have a VUE CLI web application, with router, runs on port 8080 (localhost:8080)
Also, I have a PHP MVC Project, with another router, runs on port 8888 (localhost:8888)
VUE CLI is my front-end part, that should have many connections to my PHP MVC, using AJAX (I can use fetch or jQuery.ajax or WebSocket protocol)
But automatically AND when I make a new WebSocket connection, I get these errors:
(192.168.1.1 is myself, on network)
In this image, first error occurs automatically many times (I think that defined in VUE CLI)
And second error occurs when I make a new WebSocket connection to my back-end server (PHP MVC):
win.mySocket = new win.WebSocket('ws://localhost:8888/login');
win.mySocket.addEventListener('message', e => {
win.console.log('Message from server ', e.data);
})
win.mySocket.addEventListener('error', e => {
win.console.log('Error from server ', e);
})
(win is same as window, defined in data(){} method in script tag of this router view)
Note: I've Solved the CORS-ORIGIN problem, so this isn't the problem:
header('Access-Control-Allow-Credentials: true');
header('Access-Control-Allow-Origin: *');
I'm using the WCF4.0 template -REST. I'm trying to make a method that uploads a file using a stream.
The problem always occur at
Stream serverStream = request.GetRequestStream();
Class for streaming:
namespace LogicClass
{
public class StreamClass : IStreamClass
{
public bool UploadFile(string filename, Stream fileStream)
{
try
{
FileStream fileToupload = new FileStream(filename, FileMode.Create);
byte[] bytearray = new byte[10000];
int bytesRead, totalBytesRead = 0;
do
{
bytesRead = fileStream.Read(bytearray, 0, bytearray.Length);
totalBytesRead += bytesRead;
} while (bytesRead > 0);
fileToupload.Write(bytearray, 0, bytearray.Length);
fileToupload.Close();
fileToupload.Dispose();
}
catch (Exception ex) { throw new Exception(ex.Message); }
return true;
}
}
}
REST project:
[WebInvoke(UriTemplate = "AddStream/{filename}", Method = "POST", BodyStyle = WebMessageBodyStyle.Bare)]
public bool AddStream(string filename, System.IO.Stream fileStream)
{
LogicClass.FileComponent rest = new LogicClass.FileComponent();
return rest.AddStream(filename, fileStream);
}
Windows Form project: for testing
private void button24_Click(object sender, EventArgs e)
{
byte[] fileStream;
using (FileStream fs = new FileStream("E:\\stream.txt", FileMode.Open, FileAccess.Read, FileShare.Read))
{
fileStream = new byte[fs.Length];
fs.Read(fileStream, 0, (int)fs.Length);
fs.Close();
fs.Dispose();
}
string baseAddress = "http://localhost:3446/File/AddStream/stream.txt";
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(baseAddress);
request.Method = "POST";
request.ContentType = "text/plain";
Stream serverStream = request.GetRequestStream();
serverStream.Write(fileStream, 0, fileStream.Length);
serverStream.Close();
using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
{
int statusCode = (int)response.StatusCode;
StreamReader reader = new StreamReader(response.GetResponseStream());
}
}
I've turned off the firewall and my Internet connection, but the error still exists. Is there a better way of testing the uploading method?
Stack trace:
at System.Net.Sockets.Socket.DoConnect(EndPoint endPointSnapshot, SocketAddress socketAddress)
at System.Net.ServicePoint.ConnectSocketInternal(Boolean connectFailure, Socket s4, Socket s6, Socket& socket, IPAddress& address, ConnectSocketState state, IAsyncResult asyncResult, Int32 timeout, Exception& exception)
"Actively refused it" means that the host sent a reset instead of an ack when you tried to connect. It is therefore not a problem in your code. Either there is a firewall blocking the connection or the process that is hosting the service is not listening on that port. This may be because it is not running at all or because it is listening on a different port.
Once you start the process hosting your service, try netstat -anb (requires admin privileges) to verify that it is running and listening on the expected port.
update: On Linux you may need to do netstat -anp instead.
You don't have to restart the PC. Restart IIS instead.
Run -> 'cmd'(as admin) and type "iisreset"
You must set up your system proxy
You have to go through this path
controlpanel>>internet option>>connnection>>LAN settings>>
proxy
no tik:use proxy server
I got a similar error message like TCP error code 10061: No connection could be made because the target machine actively refused it in my current project. I find this 10061 error code cannot distinguish the case that the service endpoint is not started and the case that it is blocked by the firewall. Often, the firewall can be switched off, but the problem is still there.
You can test your code in the below two ways.
Insert code to get time A that service is started and time B that client sends the request to the server. If B is earlier than A, it can cause this problem.
Change your server port to another port that is also available in the system. You will find the same error code reported.
Above is my fix. It works on my machine. I hope it helps!
Check if any other program is using that port.
If an instance of the same program is still active, kill that process.
I had a similar issue. In my case the service would work fine on the developer machine but fail when on a QA machine. It turned out that on the QA machine the application wasn't being run as an administrator and didn't have permission to register the endpoint:
HTTP could not register URL http://+:12345/Foo.svc/]. Your process does
not have access rights to this namespace (see
http://go.microsoft.com/fwlink/?LinkId=70353 for details).
Refer here for how to get it working without being an admin user: https://stackoverflow.com/a/885765/38258
If you use WCF storm, can you even log-in to the WCF service endpoint? If not, and you are hosting it in a Windows service, you probably forgot to register that namespace. It's not very well advertised that this step is required, and it is actually annoying to do.
I use this tool to do this; it automates all those cumbersome steps.
Check whether the port number in file Web.config of your webpage is the same as the one that is hosted on IIS.
I had the same problem on my web server "No connection could be made because the target machine actively refused it 161.x.x.235:5672". I asked the Admin to open the port 5672 on the web server, then it worked fine.
I had a similar problem
rejecting localhost and 127.0.0.1.
cmd(admin) netstat -anb found the port running on 169.254.80.80 (dont know were that ip came from because my network ip was 10.0.0.5.
after putting in this IP it worked.
This Gives correct IP:
IPAddress ipAddress = ipHostInfo.AddressList[0];
Console.WriteLine(ipAddress.ToString());
I also faced problem in .Net Remoting Service in C#.
I got it solved in 3 steps:
Change Port of Protocol in all the files whereever it is being used.
Run your Host Server Program and make it active.
Now run your client program.
I could not restart IIexpress. This is the solution that worked for me
Cleaned the build
Rebuild
With this error I was able to trace it, thanks to #Yaur, you need to basically check the service (WCF) if it's running and also check the outbound and inbound TCP properties on your advance firewall settings.
With similar pattern, my rest client is calling the service API, the service called successfully when debugging, but not working on the published code. Error was: Unable to connect to the remote server.
Inner Exception: System.Net.Sockets.SocketException (0x80004005): No connection could be made because the target machine actively refused it serviceIP:443 at System.Net.Sockets.Socket.DoConnect(EndPoint endPointSnapshot, SocketAddress socketAddress) at System.Net.ServicePoint.ConnectSocketInternal(Boolean connectFailure, Socket s4, Socket s6, Socket& socket, IPAddress& address, ConnectSocketState state, IAsyncResult asyncResult, Exception& exception)
Resolution: Set the proxy in Web config.
<system.net>
<defaultProxy useDefaultCredentials="true">
<proxy proxyaddress="http://proxy_ip:portno/" usesystemdefault="True"/>
</defaultProxy>
</system.net>
I had a similar issue. In my case VPN proxy app such as Psiphon ، changed the proxy setup in windows so follow this :
in Windows 10, search change proxy settings and turn of use proxy server in the manual proxy
Make Sure all services used by your application are on, which are using host and port to utilize them . For e.g, as in my case, This error can come, If your application is utilizing a Redis server and it is not being able to connect to it on given port and host, thus producing this error .
For my case, I had an Angular SLA project template with ASP.NET Core.
I was trying to run the IIS Express from the Visual Studio WebUI solution, triggering the "Actively refused it" error.
The problem, in this case, wasn't connected with the Firewall blocking the connection.
It turned out that I had to run the Angular server independently of the Kestrel run because the Server was expecting the UI to run on a specific port but wasn't actually.
For more information, check the official Microsoft documentation.
I had similar problem. In launchSettings, my IIS Express was configured on one port, and there was another launch profile that started on another ApplicationUrl with another port.
Starting the web app up with the IIS Express profile led me to have the error.
I am using the Apache ActiveMQ Artemis AMQP message broker. I started getting the "No connection could be made because the target machine actively refused it" exception when trying to send and receive messages with the broker after a reboot. On a computer where the same type of broker still worked, netstat -anb showed that the broker was listening on the expected port 5672. On the computer with the error, the broker was not listening. On the computer with the error, starting the broker resulted in the following warning's appearing in Microsoft Event Viewer's "Windows Logs > System":
The system failed to register host (A or AAAA) resource records for network adapter
with settings:
Adapter Name : {286EE2DA-3D81-41AE-VE5G-5D761FD3925E}
Host Name : mypc
Primary Domain Suffix : myco.com
DNS server list :
55.77.168.1, 74.86.130.1
Sent update to server : 186.952.335.157:45
IP Address(es) :
182.269.1.437
Either the DNS server does not support the DNS dynamic update protocol or the authoritative zone for the specified DNS domain name does not accept dynamic updates.
To register the DNS host (A or AAAA) resource records using the specific DNS domain name and IP addresses for this adapter, contact your DNS server or network systems administrator.
I was able to use the broker without error after I ran the following in a cmd.exe with administrative privileges, rebooted, and waited about fifteen minutes:
ipconfig /flushdns
ipconfig /renew
ipconfig /registerdns
I'm running ReactPHP in a Docker environment to listen for WebSocket messages. I use the port mapping feature like so:
docker run \
--detach \
-p 10002:8081 \
missive-controller
Thus, the external port 10002 maps to 8081 inside the container.
Inside my implementation of MessageComponentInterface, I have this event handler:
public function onMessage(ConnectionInterface $from, $msg)
{
echo sprintf(
"Connection %d sent message \"%s\" to WS server\n",
$from->resourceId,
$msg
);
/* #var $request \Guzzle\Http\Message\EntityEnclosingRequest */
$request = $from->WebSocket->request;
// ... do stuff
}
Now, I am listening to a couple of ports in ReactPHP, in order to differentiate between internet WebSocket requests, and private messages from other containers on the private Docker network. So, to detect the port, I do this (using the Guzzle object set up above):
$request->getPort();
However, that gets me 10002 (the internet port) rather than the internal container-side port (8081). I have a mapping device to look up the association for now, but can I obtain the container port directly?
I am running Ratchet 0.36.
My present answer to this is that it is not possible, at least with the version of Ratchet I am using. I have scanned $from, and this only seems to know about the port from an external perspective. The only references I can find in a print_r($from) to the internal port are ones that I have set in a manual mapping array, which does not help.
I idly pondered whether this information might be sitting in the Ratchet\Server\IoServer server instance, so I injected this into my listener before starting the loop, and examined it with print_r() again. Same result, I'm afraid; nothing pops out as looking useful.
Contrary to the original post, my port mappings are now set up in a docker-compose.yml file, so my solution for now will be to add these mappings in environment vars in the same file, so it is harder for them to come out of sync. I don't wish to hardwire these, as I am using different port mappings depending on environment.
It is possible that version 0.4 will support this, so I will revisit when I upgrade.
So, I am running a Ratchet (php) websocket server with multiple routes that connect do multiple Ratchet apps (MessageComponentInterfaces):
//loop
$loop = \React\EventLoop\Factory::create();
//websocket app
$app = new Ratchet\App('ws://www.websocketserver.com', 8080, '0.0.0.0', $loop);
/*
* load routes
*/
$routeOne = '/example/route';
$routeOneApp = new RouteOneApp();
$app->route($routeOne, $routeOneApp, array('*'));
$routeTwo = '/another/route';
$routeTwoApp = new AnotherApp();
$app->route($routeTwo, $routeTwoApp, array('*'));
From here I am binding a ZMQ socket, in order to be able to receive messages sent from php scripts run on the normal apache server.
// Listen for the web server to make a ZeroMQ push after an ajax request
$context = new \React\ZMQ\Context($loop);
$pull = $context->getSocket(\ZMQ::SOCKET_PULL);
$pull->bind('tcp://127.0.0.1:5050'); // Binding to 127.0.0.1 means the only client that can connect is itself
$pull->on('message', array($routeOneApp, 'onServerMessage'));
Finally, the server is started:
//run
$loop->run();
This works perfectly fine as long as i am binding only one of the ratchet apps to the ZMQ socket. However, i would like to be able to separately push messages to both of the Ratchet apps. For this purpose i thought of binding two ZMQ sockets to different routes like:
$pullOne->bind('tcp://127.0.0.1:5050' . $routeOne); // Binding to 127.0.0.1 means the only client that can connect is itself
$pullOne->on('message', array($routeOneApp, 'onServerMessage'));
and
$pullTwo->bind('tcp://127.0.0.1:5050' . $routeTwo); // Binding to 127.0.0.1 means the only client that can connect is itself
$pullTwo->on('message', array($routeTwoApp, 'onServerMessage'));
However, this leads to an error message from ZMQ when binding the second socket, saying the given address is already in use.
So the question is, is there any other way to use routes over a ZMQ socket?
Or should i use other means to distinguish between messages for the separate Ratchet apps, and if so, what would be a good solution?
I thought about binding to 2 different ports, but figured that would be a pretty ugly solution?!
In general in TCP packets are identified by the 4 tuple (sender ip, sender port, receiver ip, receiver port).
When a incoming packet reaches the network layer, it is forwarded to the appropriate application by looking at the receiver ip and port. If you use the same pair for both the apps, it will be impossible for the layer to decide whom to send it to when a connection comes in.
One solution would be to bind a single connection and the write a common handler that looks at the incoming content and then decides (I assume you have some logic) to differentiate the incoming connections to the different instances and then invokes the corresponding handler. The handler can get the connection object and can handle the connection hence forth.
If both your instances are identical and it doesn't matter who gets the request then you can just randomly forward the new connection to any of the handler.
Edit: I have tried to answer the question irrespective of the application type (Racket/ZMQ etc) because the issue you are trying to address is a fundamental one common to any network application.
For this case since you have two apps running and want to listen on the same port, you can have a common handler which can look at the request URL and forward the connection to the appropriate handler.
The request URL can be obtained using
$querystring = $conn->WebSocket->request->getQuery();
Now the clients can connect using
ws://localhost:5050/app1
and
ws://localhost:5050/app2
Your different apps can now handle these connections separately.
I am running a Ratchet Server
$app = new Ratchet\App( 'localhost', 8080 );
$app->route( '/api', new API );
$app->run();
It is very easy to connect via JavaScript (and send some messages to the server):
_connection = new WebSocket( "ws://localhost:8080" );
Now I want the same from PHP (connect and send messages), but I dont know how and I dont find any information in the documentation of Ratchet.
Is it somehow possible? I really need to send messages from PHP to the clients (JavaScript), when there is some event on the server.
Edit: I really just need to "fire&forget" some simple message. Nothing fancy.
It works a bit wrong.
After connecting to the server, you can send a message from the client to the server and from the server to the client.
In the Connection Interface is the "send" method.
And call onSubscribe or onPublish method you have $topic variable.
In the $topic is the "broadcasting" method.
You need call:
$conn->send($message); or $topic->brodcasting($message);
If you need send client message after action on site, use example on ratchet (we need install ZMQ)
It works like this:
You create server. This server recieve message from users and recieve message from ZMQ.
Your site publish on ZMQ message
Your server receive message from ZMQ and broadcasting on users
Regards Maxim