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

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 :)

Related

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

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.

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");

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);

Channel TCP is already registered but with Infinite Loop

I have created TCP Client and Server using .Net remoting . When I call server from client i.e. at the line RemotingConfiguration.Configure("App.config", true);, I am getting error as
Remoting configuration failed with the exception 'System.Runtime.Remoting.RemotingException: The channel 'tcp' is already registered.
I have made all the settings in config file. To resolve this issue, I gave unique name to the client application. Now, I am not getting error but when I send any data from client to server,nothing happens at server or client end. If I closed the server, I get error in client application as remoting server closes the connection , which is proper behaviour. So the issue is why nothing happnes at client or server end.
RemotingConfiguration.Configure("App.config", true); line is not giving me any error, but server.Send("Hi") does not give any response back to the client. Nothing displays on server.

C# Client-Server application problem

I run my application on a network and in some cases the client lost connection to the server. After this time, when I wanted to send a message to the server I receive the following error: Operation not allowed on non-connected sockets (something like this).
I thought to create an event for object type TcpClient and when tcp_obj.Connected = false to call a function to discontinue execution of the current code. How could I do this?
Or giving me other suggestios.
Thanks.
I know at least from socket programming in Java that when a client loses connection to the server, the server does not and can not know about it. You need a heartbeat of some sort to detect the early disconnection.
We often use a heartbeat in our client/server applications to detect early disconnections and log them on the server. This way the server can close the associated socket and release the connection back to the pool.
Simply send a command to the client periodically and wait for a response. If no response is garnered within a timeout assume disconnect and close streams.
I would simply first check your connection object to ensure you are connected, prior to attempting to send the message. Also make sure that you are putting your send-logic inside of a try-catch, so that if you do happen to get disconnected mid transmission, you'll be able to resume without blowing your application apart.
Psuedo-Code:
private void SendMessage(string message, Socket socket)
{
if(socket.connectionState = States.Connected)
{
try{
// Attempt to Send
}
catch(SocketException Ex)
{
// Disconenct, Additional Cleanup Etc.
}
}
}
If you are in C#, prior to your connection state changing, you will have a socket disconnected event fire, prior to your connection state changing. Make sure you tie this event up as soon as your socket connects.
Can we know why you use TCP sockets? Is for calling a tcp device o server code?
I recommend you if is for calling a .net server app use Windows Communication Foudation. It is simple to expose services by net.tcp, http, etc.
Regards,
Actually this is a very old problem,
If I understand your question correctly you need a way to know whether you're application is still connected to the server or vice versa.
If so then a workaround is to have a UDP connection just to check the connectivity (overhead I know, but its much better then polling on Connected state), you could check just before you send you're data.
Since UDP is not Connection oriented you don't need to be connected when you send the data

Categories

Resources