Communication Between Node Client and C# Server - c#

Im currently working on a NodeJS application that communicates with a c# server. The NodeJS web app receives a request and performs Get:
app.get('/api/retrieve', (req, res) => {
let client = new net.Socket();
client.connect(port, host, function() {
console.log("connected");
});
client.on('data', function(data){
console.log(data);
});
client.on('close', function(){
console.log("closed");
});
});
This successfully connects to my c# server:
public void StartListening()
{
TcpListener server = new TcpListener(IPAddress.Any, 5000);
server.Start();
while (true)
{
TcpClient client = server.AcceptTcpClient();
NetworkStream networkStream = client.GetStream();
while (client.Connected)
{
try
{
byte[] message = new byte[1024];
networkStream.Read(message, 0, message.Length);
var json = Encoding.Default.GetString(message);
if (json.Contains("GET /api/retrieve"))
{
string jsonData = JsonConvert.SerializeObject(_dal.Retrieve());
byte[] dataBytes = System.Text.Encoding.Unicode.GetBytes(jsonData);
client.GetStream().Write(dataBytes, 0, dataBytes.Length);
}
}
catch (Exception ex)
{
throw ex;
}
}
}
}
When debugging in the browser, I receive the following error:
Proxy error: Could not proxy request /api/retrieve from localhost:3000 to localhost:5000 (HPE_INVALID_CONSTANT).
In Visual Studio, the following exception is caught:
Unable to read data from the transport connection: An existing connection was forcibly closed by the remote host
Also, Visual Studio only catches the above exception after the line networkStream.Read(message, 0, message.Length); on the second iteration.
All in all, the .Net application should send a JSON string back to the client NodeJS.

Related

C# - Cannot connect my Socket Server

Good afternoon dear community,
I am currently working on a Arduino + Unity project where I am supposed to build an Arduino control through Unity. For that, I have set my Arduino and connected to wifi.
I built a Socket Server with C#. The problem is that: I am able to connect to this Socket Server through a Node.js Socket Client, however to check if my Arduino setup is broken, I have built a Node.js Socket Server and was able to connect my Node.js Socket server succesfully through Arduino. (The only reason I am using Node.js here is that because I am more fluent on that platform so I am able to check what is wrong, so final project does not have any Node.js.) So;
Node.js Client -> Unity C# Server (Works)
Arduino Client -> Node.js Server (Works) (Below is Arduino output)
AT+CIPSTART=3,"TCP","192.168.1.67",1234
OK
Linked
Arduino Client -> Unity C# Server (Does not work) (Below is Arduino output)
AT+CIPSTART=3,"TCP","192.168.1.67",1234
ERROR
Unlink
Below I am putting my all C#, Node.js and Arduino codes. I don`t think that there is an Arduino hardware issue here, but don·t understand why I cannot connect my
Unity Socket Server.
using System;
using System.Net.Sockets;
using System.Threading;
using System.Net;
using UnityEngine;
public class SocketScript : MonoBehaviour {
Thread tcpListenerThread;
void Start()
{
tcpListenerThread = new Thread(() => ListenForMessages(1234));
tcpListenerThread.Start();
}
public void ListenForMessages(int port)
{
TcpListener server = null;
try
{
IPAddress localAddr = IPAddress.Parse("192.168.1.67");
server = new TcpListener(localAddr, port);
server.Start();
Byte[] bytes = new byte[256];
String data = null;
while(true)
{
Debug.Log("Waiting for connection.");
using (TcpClient client = server.AcceptTcpClient())
{
Debug.Log("Connected!");
data = null;
NetworkStream stream = client.GetStream();
int i;
// As long as there is something in the stream:
while((i = stream.Read(bytes, 0, bytes.Length)) != 0)
{
// Translate data bytes to a ASCII string.
data = System.Text.Encoding.ASCII.GetString(bytes, 0, i);
Debug.Log(String.Format("Received: {0}", data));
byte[] msg = System.Text.Encoding.ASCII.GetBytes(data);
// Send back a response.
stream.Write(msg, 0, msg.Length);
Debug.Log(String.Format("Sent: {0}", data));
}
}
}
} catch (SocketException e)
{
Debug.LogError(String.Format("SocketException: {0}", e));
} finally
{
server.Stop();
}
}
}
Node.js Client:
var net = require('net');
var HOST = '192.168.1.67';
var PORT = 1234;
var client = new net.Socket();
client.connect(PORT, HOST, function() {
console.log('Client connected to: ' + HOST + ':' + PORT);
// Write a message to the socket as soon as the client is connected, the server will receive it as message from the client
client.write('Hello World!');
});
client.on('data', function(data) {
console.log('Client received: ' + data);
if (data.toString().endsWith('exit')) {
client.destroy();
}
});
// Add a 'close' event handler for the client socket
client.on('close', function() {
console.log('Client closed');
});
client.on('error', function(err) {
console.error(err);
});
Node.js Server:
var net = require('net');
// Configuration parameters
var HOST = '192.168.1.67';
var PORT = 1234;
// Create Server instance
var server = net.createServer(onClientConnected);
server.listen(PORT, HOST, function() {
console.log('server listening on %j', server.address());
});
function onClientConnected(sock) {
var remoteAddress = sock.remoteAddress + ':' + sock.remotePort;
console.log('new client connected: %s', remoteAddress);
sock.on('data', function(data) {
console.log('%s Says: %s', remoteAddress, data);
sock.write(data);
sock.write(' exit');
});
sock.on('close', function () {
console.log('connection from %s closed', remoteAddress);
});
sock.on('error', function (err) {
console.log('Connection %s error: %s', remoteAddress, err.message);
});
};

TCP Server only works in localhost scheme

I can establish a TCP connection between a TCP client and TCP server in localhost, however I can't repeat the same example for a connection with different computers into the same net range (sender Windows Server 2012 x64 R2 and receiver Windows 10 x64 Pro) . The TCP Server is a C# application and the TCP client is in node.js. I have disabled both Antivirus and Windows Firewall.
//SERVER C#
void Receive() {
//tcp_Listener = new TcpListener(IPAddress.Parse("192.168.1.62"), 212);
IPAddress localAddr = IPAddress.Parse("0.0.0.0");
tcp_Listener = new TcpListener(localAddr,212);
TcpClient clientSocket = default(TcpClient);
tcp_Listener.Start();
print("Server Start");
while (mRunning)
{
// check if new connections are pending, if not, be nice and sleep 100ms
if (!tcp_Listener.Pending()){
Thread.Sleep(10);
}
else {
clientSocket = tcp_Listener.AcceptTcpClient();
NetworkStream networkStream = clientSocket.GetStream();
byte[] bytesFrom = new byte[10025];
networkStream.Read(bytesFrom, 0, (int)clientSocket.ReceiveBufferSize);
string dataFromClient = System.Text.Encoding.ASCII.GetString(bytesFrom);
if (dataFromClient != "") {
print ("Data from client: " + dataFromClient);
} else {
print ("Client no data");
}
clientSocket.Close();
}
}
}
//CLIENT NodeJS
var net = require('net');
var HOST = '192.168.0.136';
var PORT = 212;
var client = new net.Socket();
client.connect(PORT, HOST, function() {
console.log('CONNECTED TO: ' + HOST + ':' + PORT);
// Write a message to the socket as soon as the client is connected, the server will receive it as message from the client
client.write('MSG SENT!');
});
// Add a 'data' event handler for the client socket
// data is what the server sent to this socket
client.on('data', function(data) {
console.log('DATA: ' + data);
// Close the client socket completely
client.destroy();
});
// Add a 'close' event handler for the client socket
client.on('close', function() {
console.log('Connection closed');
});
The wireshark log about this whole connection is the following:
This is the log from TCP Client:
I have replaced for networkStream.Read (bytesFrom, 0, bytesFrom.Length)

Java TCP Client and C# Server; Client Only Receives Message After Server Closes

I have written an asynchronous server in C# and a TCP client in Java for and an Android app. The client can send messages fine to the server and they are received when they are sent. However when I send a message from the server to the client, the client only displays the message after the server is shutdown (i.e. when the socket closes).
The strange thing is that I have written a client in C# as well and that receives messages as soon as they are sent.
The C# server and client both use the asynchronous begin* and end* methods and the Java client uses a stream reader/writer.
Can anyone please suggest why the Java client is behaving in this way and how to remedy this?
Thanks.
Client Code:
public void run() {
mRun = true;
try {
//here you must put your computer's IP address.
InetAddress serverAddr = InetAddress.getByName(SERVERIP);
Log.e("TCP Client", "C: Connecting...");
//create a socket to make the connection with the server
socket = new Socket(serverAddr, SERVERPORT);
try {
if (out != null)
{
//send the message to the server
out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())), true);
Log.e("TCP Client", "C: Sent.");
Log.e("TCP Client", "C: Done.");
}
//receive the message which the server sends back
in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
//in this while the client listens for the messages sent by the server
while (mRun) {
serverMessage = in.readLine();
if (serverMessage != null && mMessageListener != null) {
//call the method messageReceived from MyActivity class
mMessageListener.messageReceived(serverMessage);
serverMessage = null;
}
}
Log.e("RESPONSE FROM SERVER", "S: Received Message: '" + serverMessage + "'");
} catch (Exception e) {
Log.e("TCP", "S: Error", e);
} finally {
//the socket must be closed. It is not possible to reconnect to this socket
// after it is closed, which means a new socket instance has to be created.
socket.close();
}
} catch (Exception e) {
Log.e("TCP", "C: Error", e);
}
}
Server Code:
public void Send(String data)
{
// Convert the string data to byte data using ASCII encoding.
byte[] byteData = Encoding.ASCII.GetBytes(data);
// Begin sending the data to the remote device.
socket.BeginSend(byteData, 0, byteData.Length, 0, new AsyncCallback(SendCallback), socket);
}
private static void SendCallback(IAsyncResult ar)
{
try
{
//Retrieve the socket from the state object.
Socket clientSocket = (Socket)ar.AsyncState;
//send the data
int bytesSent = clientSocket.EndSend(ar);
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
}
My guess is that the data you send from the server doesn't end with an EOL sequence (\n or \r\n). So the readLine() method at client-side never returns, since it can only return when it's sure the line is terminated (i.e. when an EOL sequence is received, or the connection is closed).

Android socket client works on Java server but not C# server

I'm new to Android programming and I'm trying to send some text data to PC via TCP.
I built an example I found on the web, which has an Android Client and a Java Server.
The server runs ok.
When I run the Android Client on the emulator or on the phone, both works perfectly well.
The problem is that I need it to communicate to a C# application, so I built an TCP server in C#.
Now, if I run the Client on the emulator it works and the C# server receive the data (a little messed, but it's probably an text encoding problem which I think won't be hard to solve). But anyway, the data is arriving at the c# server.
If I try to run the same Client on the phone I can't even connect to the C# server. I get a timeout error when connecting.
Also I can ping the PC from phone and ping the phone from PC, so I don't think it's a network problem.
I have some experience in C# but not much on sockets and even less on Android. So I ask, is there any difference on TCP protocols used by Java and C#? Sorry if it's a dumb question, but I googled it for hours and haven't found a clue.
Any ideas of what may be causing it?
The Java server code is this:
public class Servidor {
private static boolean executando = true;
private static String mensagem;
private static final int PORTA = 1234;
public static void main(String[] args) {
try {
ServerSocket server = new ServerSocket(1234);
InetAddress addr = InetAddress.getLocalHost();
System.out.println("----------- SERVER CONNECTED "
+ addr.getHostAddress() + " PORT " + PORTA
+ " -----------");
System.out.println("Waiting connections.");
Socket socket = server.accept();
System.out.println("Server -> Connected Ip "
+ socket.getInetAddress().getHostAddress());
DataInputStream in = new DataInputStream(socket.getInputStream());
try {
while (executando) {
mensagem = in.readUTF();
System.out.println("Server-> Received Message: "
+ mensagem);
}
System.out.println("Servidor-> Finalizado.");
in.close();
socket.close();
server.close();
} catch (Exception e) {
System.err.println("Server -> Error: " + e.getMessage());
executando = false;
}
} catch (Exception e) {
e.printStackTrace();
}
} }
The C# Server code is this:
class Server
{
private TcpListener tcpListener;
private Thread listenThread;
public Server()
{
Console.WriteLine("\nStarting server...");
this.tcpListener = new TcpListener(IPAddress.Any, 1234);
this.listenThread = new Thread(new ThreadStart(ListenForClients));
this.listenThread.Start();
}
private void ListenForClients()
{
Console.WriteLine("\nWaiting for clients to connect...");
this.tcpListener.Start();
while (true)
{
blocks until a client has connected to the server
TcpClient client = this.tcpListener.AcceptTcpClient();
create a thread to handle communication
with connected client
Thread clientThread = new Thread(new ParameterizedThreadStart(HandleClientComm));
clientThread.Start(client);
}
}
private void HandleClientComm(object client)
{
Console.WriteLine("\nIncoming from client...");
TcpClient tcpClient = (TcpClient)client;
NetworkStream clientStream = tcpClient.GetStream();
byte[] message = new byte[4096];
int bytesRead;
while (true)
{
bytesRead = 0;
try
{
blocks until a client sends a message
bytesRead = clientStream.Read(message, 0, 4096);
}
catch
{
a socket error has occured
break;
}
if (bytesRead == 0)
{
the client has disconnected from the server
break;
}
message has successfully been received
ASCIIEncoding encoder = new ASCIIEncoding();
Console.WriteLine("\nReceived: \n\n" + encoder.GetString(message, 0, bytesRead));
}
tcpClient.Close();
}
}
If needed, the Android Client code is exactly the one on the following link:
http://www.portalandroid.org/comunidade/viewtopic.php?f=7&t=11077&p=127577

Using ThreadPool.QueueUserWorkItem to open a TcpClient connection and read data in ASP.NET and SignalR

I've read a couple of posts on SignalR and thought for a fun test project that I could create a web application to poll my onkyo receiver for status and display the results in a browser. For an initial test, I was successfully able to send the current time on the server back to the client by using this code in Application_Start:
ThreadPool.QueueUserWorkItem(_ =>
{
dynamic clients = Hub.GetClients<KudzuHub>();
while (true)
{
clients.addMessage(DateTime.Now.ToString());
Thread.Sleep(1000);
}
});
In the client javascript, i have the following code:
// Proxy created on the fly
var kHub = $.connection.kudzuHub;
// Declare a function on the hub so that the server can invoke it
kHub.addMessage = function (message) {
console.log('message added');
$('#messages').append('<li>' + message + '</li>');
};
// start the connection
$.connection.hub.start();
So all of that works fine. Every second, I get a new list item containing the current server date and time.
Now when I add this code to read data from the Onkyo receiver, it breaks: (still in Application_Start)
ThreadPool.QueueUserWorkItem(_ =>
{
dynamic clients = Hub.GetClients<KudzuHub>();
try
{
while (true)
{
string host = ConfigurationManager.AppSettings["receiverIP"].ToString();
int port = Convert.ToInt32(ConfigurationManager.AppSettings["receiverPort"]);
TcpClient tcpClient = new TcpClient(host, port);
NetworkStream clientSockStream = tcpClient.GetStream();
byte[] bytes = new byte[tcpClient.ReceiveBufferSize];
clientSockStream.Read(bytes, 0, (int)tcpClient.ReceiveBufferSize);
tcpClient.Close();
clients.addMessage(System.Text.Encoding.ASCII.GetString(bytes));
Thread.Sleep(50);
}
}
catch (SocketException ex)
{
// do something to handle the error
}
});
I set a break point and stepped through the code. It gets to this line and then returns.
clientSockStream.Read(bytes, 0, (int)tcpClient.ReceiveBufferSize);
It never finishes the rest of the code to send the message to the client. What am I doing wrong?
Thanks.
I would make some structural changes to your loop to allow the receiver time to respond, remove the overhead of retrieving the configuration every 50 milliseconds, and cleanup the open network stream:
ThreadPool.QueueUserWorkItem(_ =>
{
dynamic clients = Hub.GetClients<KudzuHub>();
TcpClient tcpClient = null;
NetworkStream clientSockStream = null;
try
{
string host = ConfigurationManager.AppSettings["receiverIP"].ToString();
int port = Convert.ToInt32(ConfigurationManager.AppSettings["receiverPort"]);
while (true)
{
if (tcpClient == null) {
tcpClient = new TcpClient(host, port);
clientSockStream = tcpClient.GetStream();
}
if (clientSockStream.CanRead) {
byte[] bytes = new byte[tcpClient.ReceiveBufferSize];
try {
clientSockStream.Read(bytes, 0, (int)tcpClient.ReceiveBufferSize);
} catch (Exception ex) {
// Add some debug code here to examine the exception that is thrown
}
tcpClient.Close();
// Closing the client does not automatically close the stream
clientSockStream.Close();
tcpClient = null;
clientSockStream = null;
clients.addMessage(System.Text.Encoding.ASCII.GetString(bytes));
}
Thread.Sleep(50);
}
}
catch (SocketException ex)
{
// do something to handle the error
} finally {
if (tcpClient != null) {
tcpClient.Close();
clientSockStream.Close();
}
}
});

Categories

Resources