Sending message with Router-Dealer Proxy using c# and python - c#

I am able to have c# (client) and python (server) talk to each other by using a simple request-reply. However, I want my web application built on c# asp.net to be stable and need more clients and servers, so I tried connecting c# and python using the Router-Dealer Proxy with python.
I tried running the proxy python script first, then running c# (client), then python (server). However, when I run the python (server), it gives me an "Address in use" error message.
Am I running them in a wrong order OR is there something wrong with the proxy python script (shown below)?
5602 = c# client
5603 = python server
def main():
context = zmq.Context()
# Socket facing clients
frontend = context.socket(zmq.ROUTER)
frontend.bind("tcp://*:5602")
# Socket facing services
backend = context.socket(zmq.DEALER)
backend.bind("tcp://*:5603")
zmq.proxy(frontend, backend)
# We never get hereā€¦
frontend.close()
backend.close()
context.term()
if __name__ == "__main__":
main()

I'm assuming your servers use bind, so the proxy should connect to them rather than also using bind.
Note: in zeromq the order of application startup doesn't matter so you can tell your proxy to connect to a server that doesn't yet exist, when the server is started the connection will be made.

Related

nodejs Net socket client communication with c# .net server application

I have a C# .net server service application which listens on port 11111 for a connection. I have tested it with another c# test application which can send and recieve communication for and from the server.
Now the production scenario is that this C# .net server application is to recieve connection from, amongst others, a nodejs client which is encapsulated in Electron. So i create this function in NodeJS
const net = require( "net");
const socket = new net.Socket();
socket.connect('11111','127.0.0.1');
exports.socket = socket;
I import it in the IPC layer of Electron.
const { socket } = require("./socketInit.js");
and when doing that i get this error
A JavaScript error occurred in the browser process
Error: connect ECONNREFUSED 127.0.0.1:11111
at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1146:16)
It wont connect to the server socket.
Im not even using the socket yet, but i understand that the connection attempt is doing the handshake and that must be where the error happens.
Does anyone have any idea what could be wrong ?
TY for your time!
Im quite sure it is because the code is in a renderpass which runs several times, and then the code in reality tries to open a connection on a port where a connection is already open each time there is a renderpass except from the first.
I destroyed the connection after sending and now i dont get the error anymore. But im not getting any data send to the server, but that is for another question.
I'm integrating this into an existing codebase, which i don't fully understand as im a total newb to NodeJS, Javascript and Electron, so im not sure how it all works :)

Stomp WebSocket Client for a Spring based broker built in Java is failing

I have a Websocket Stomp server built in Java (Spring based). My JavaScript client is able to talk to the server using Stomp over Web Socket - no issues.
I'm trying to use the WebSocket4Net library to build the C# client. Can anyone please tell me how can I build the Stomp message frames in bytes[] and send them to the server?
In the OnOpen() method, I'm doing the following:
String stompMsg = "CONNECT\n";
stompMsg += "accept-version:1.1,1.2\n\n";
stompMsg += "^#";
Byte[] bytesToSend = Encoding.UTF8.GetBytes(stompMsg);
websocketObj.Send(bytesToSend, 0, bytesToSend.Length);
I get the following message from server:
Failed to parse BinaryMessage payload - java.nio.HeapByteBuffer.
Sending STOMP ERROR to client.
java.lang.IllegalArgumentException: Object of class [org.springframework.web.socket.BinaryMessage] must be an instance of class org.springframework.web.socket.TextMessage
at org.springframework.util.Assert.isInstanceOf(Assert.java:339)
at org.springframework.util.Assert.isInstanceOf(Assert.java:319)
at org.springframework.web.socket.messaging.StompSubProtocolHandler.handleMessageFromClient(StompSubProtocolHandler.java:189)
at org.springframework.web.socket.messaging.SubProtocolWebSocketHandler.handleMessage(SubProtocolWebSocketHandler.java:307)
at org.springframework.web.socket.handler.WebSocketHandlerDecorator.handleMessage(WebSocketHandlerDecorator.java:75)
at org.springframework.web.socket.handler.LoggingWebSocketHandlerDecorator.handleMessage(LoggingWebSocketHandlerDecorator.java:55)
at org.springframework.web.socket.handler.ExceptionWebSocketHandlerDecorator.handleMessage(ExceptionWebSocketHandlerDecorator.java:71)
at org.springframework.web.socket.adapter.standard.StandardWebSocketHandlerAdapter.handleBinaryMessage(StandardWebSocketHandlerAdapter.java:122)
at org.springframework.web.socket.adapter.standard.StandardWebSocketHandlerAdapter.access$100(StandardWebSocketHandlerAdapter.java:42)
at org.springframework.web.socket.adapter.standard.StandardWebSocketHandlerAdapter$4.onMessage(StandardWebSocketHandlerAdapter.java:88)
at org.springframework.web.socket.adapter.standard.StandardWebSocketHandlerAdapter$4.onMessage(StandardWebSocketHandlerAdapter.java:85)
at org.apache.tomcat.websocket.WsFrameBase.sendMessageBinary(WsFrameBase.java:549)
at org.apache.tomcat.websocket.WsFrameBase.processDataBinary(WsFrameBase.java:514)
at org.apache.tomcat.websocket.WsFrameBase.processData(WsFrameBase.java:274)
at org.apache.tomcat.websocket.WsFrameBase.processInputBuffer(WsFrameBase.java:116)
at org.apache.tomcat.websocket.server.WsFrameServer.onDataAvailable(WsFrameServer.java:54)
at org.apache.tomcat.websocket.server.WsHttpUpgradeHandler$WsReadListener.onDataAvailable(WsHttpUpgradeHandler.java:192)
at org.apache.coyote.http11.upgrade.AbstractServletInputStream.onDataAvailable(AbstractServletInputStream.java:178)
at org.apache.coyote.http11.upgrade.AbstractProcessor.upgradeDispatch(AbstractProcessor.java:92)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:601)
at org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.doRun(AprEndpoint.java:2430)
at org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.run(AprEndpoint.java:2419)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at java.lang.Thread.run(Thread.java:744)
Can anyone please suggest if you have successfully created / used any C# client library with a spring based STOMP over Web Socket server?
#Artem - Thank you SO much. The \0 indeed did the trick.
websocket.Send("SUBSCRIBE\nid:sub-0\ndestination:/topic/mytopic\n\n\0");

How to ping a WebSocket server using a WampSharp Client?

I have created a WebSocket server in python using the Wamp WS. I am connecting a DotNet application containing the WampSharp client with the above mentioned WebSocket server using the following code:
DefaultWampChannelFactory channelFactory = new DefaultWampChannelFactory();
channel = channelFactory.CreateChannel(serverAddress);
channel.Open();
Now I need to ping my server from the client. When I had a look at the Wamp WS client created in Python it consisted of the sendPing(self,payload) function which would ping the server as follows:
WampClientProtocol.sendPing(self, payload)
and at server side there is a onPing function which handles the ping sent as follows:
def onPing(self, payload):
print "Recieved ping message successfully"
Thus, I would like to know whether there is any way I could ping the server from my WampSharp client?
WebSocket Server started at: 127.0.0.1:8000
Thanks in advance
There is no supported way to do this currently.
Note that WampSharp wraps WebSocket4Net's WebSocket class, which by default automatically sends a ping message every 60 seconds.
I could not find any support for manual ping send in WebSocket4Net from the WebSocket class, but maybe it exists and I missed it.
If you want, you can find a different WebSocket client library that supports manual ping send, and implement a IControlledWampConnection that wraps it (see WebSocket4NetConnection), and use the DefaultWampChannelFactory overload that specifies the IControlledWampConnection to use (see WebSocket4Net extension methods).

Interaction between MQL5 (or C++) and C# via Named Pipes

I am trying to send some data via Named Pipes. I created Named Pipe Server in C# and client in MQL5 (it is just a C++ wrapper). Server works fine and can be reached from Named Pipe Client written in C# so communication C# <-> C# works fine. Also i tried utility PipeList and it also shows that my pipe server is visible and available.
The only problem is with client written in MQL5 (C++) - it does not find the path to the pipe server so communication MQL <-> C# is failing.
Could anybody suggest :
what am i doing wrong?
how to check that both C# and MQL are accessing the same physical
path and the same location?
Server :
NamedPipeServerStream pipeStream = new NamedPipeServerStream("MQL5", PipeDirection.In, 1, PipeTransmissionMode.Byte)
I also tried full path \\\\.\\pipe\\MQL5 with no success
Client :
CFilePipe iPipe;
while(IsStopped() == false)
{
Print("This loop is infinite because there is no connection");
if (iPipe.Open("\\\\.\\pipe\\MQL5", FILE_READ | FILE_WRITE | FILE_BIN) != INVALID_HANDLE) break;
Sleep(250);
}
Thanks.
Answer is found. Seems that was simply my own mistake or this is how Pipes work in MQL - channel always needs to be Duplex so line in C# needs to be replaced with the following :
NamedPipeServerStream pipeStream = new NamedPipeServerStream(name, PipeDirection.InOut, 1, PipeTransmissionMode.Byte)
Parameter PipeDirection.InOut says pipe to be two-way.
P.S. Though it is a little weird anyway because conjunction C# Server <-> C# Client can work in both modes (In / Out or one of them)
Server: C# / Client: MetaTrader
I had two other problems:
The client and server needed to be run as Administrator
I needed to set buffer size for input and ouput (Default is zero =>
dynamically calculated => there was an error on this).

Using WinSocks in C to send data to a c# application on same machine, "target host refusing connectiong(c# part)

I am trying to get data from my interface, written in c, to another application, in c#.
Now, I'm not sure if WinSocks is pure c, but I'm using visual studio and the rest of my interface is 100% pure C.
Here is my "client" written in c#
http://pastebin.com/X9SNcVqn
here is my "server" written in c - loops waiting for a connection, this builds AND RUNS without issues
NOTE: DEFAULT_PORT is 18042, used the same port for client and server side.
I've downloaded wireshark and used the command "tcp.port eq "
http://pastebin.com/FHZyre2V
I also tried going through my windows firewall and NORTON to allow this connection, I couldn't figure out what to do. Most of the tuts I saw where outdated and tabs and options are changed in WINDOWS 7
I chose a port that wasn't being used, I tried using wireshark to see the connections, no luck BUT I scanned the port I used with nmap, before AND after I ran the "server", so it must of atleast have been created
In your C# code you are mixing TcpClient and Socket objects. You don't need both, only the TcpClient. (The Socket code is using the wrong port as well). Once the TcpClient object is connected, call the GetStream method to get a NetworkStream object that you can read and write to to send and receive data to the server process.
See the example code in the documentation, here: http://msdn.microsoft.com/en-us/library/system.net.sockets.tcpclient.aspx
Your client code contains:
IPEndPoint endPoint = new IPEndPoint(IPAddress.Parse("192.168.1.4"), 18041);
I would not necessarily expect the IP address bound to a network card to necessarily work for localhost-to-localhost connections. I'd recommend changing your client to use 127.0.0.1 or another suitable loopback address.
First,check if the IP adress is correct and if the corresponding port is listeing.
netstat -an | find "port number"
and I think, in the server side code
local.sin_port = (unsigned short)DEFAULT_PORT;
Should be:
local.sin_port = htons((unsigned short)DEFAULT_PORT);

Categories

Resources