I'm trying to convert a C# socket connector to nodejs but I can't figure out how to read the stream from the socket. Basically I'm trying to convert this bit of code from C# to nodejs:
private TcpClient client = new TcpClient();
client.Connect(host, port);
client.NoDelay = true;
this.NetworkStream = client.GetStream();
Thanks for the help!
The net.connect() function returns a net.Socket instance, which is a stream, i.e. it implements a duplex Stream interface.
See:
https://nodejs.org/api/net.html#net_net_connect_options_connectlistener
https://nodejs.org/api/net.html#net_class_net_socket
https://nodejs.org/api/stream.html#stream_class_stream_readable
https://nodejs.org/api/stream.html#stream_class_stream_writable
Related
I have a robotic system that can be interacted with over TCP/IP. I have been able to control the system in Matlab using the following code:
AT = tcpip('cim-up',8000);
fopen(AT);
fprintf(AT, '$global[1] = 33');
I need to emulate the same command in C#. I have tried the following code:
// Connect to Robot using TCPIP
string IP = "cim-up";
TcpClient tcpclnt = new TcpClient();
Console.WriteLine("Connecting.....");
try
{
tcpclnt.Connect(IP, 8000);
Console.WriteLine("Connected");
}
catch
{
Console.WriteLine("Failed");
}
StreamWriter AT_writer = new StreamWriter(tcpclnt.GetStream(), Encoding.ASCII);
AT_writer.Write("$global[1]=33");
This code will connect to the TCP/IP address but the server does not respond to the $global[1]=33 command.
I have also tried the following:
Byte[] data = System.Text.Encoding.ASCII.GetBytes("$global[1]=33");
// Get a client stream for reading and writing.
NetworkStream stream = tcpclnt.GetStream();
// Send the message to the connected TcpServer.
stream.Write(data, 0, data.Length);
Does anyone have any suggestions as I have a successful Matlab implementation?
Thanks
I am trying to write/read to sslstream on 443 port.
I can able write data to that sslstream but while reading from that sslstream, it is waiting to read, forever.
Sample code:
var add= "IPGoesHere";
var port = 443;
var remoteIpAdd= IPaddress.
Parse("IPGoesHere");
var socket = new Socket(remoteIpAdd.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
socket.Connect(new IPEndPoint(remoteIpAdd,port));
var netstream = new NetworkStream(
socket);
var sslstream = new SslStream(
netstream, false, callback1,
callback2);
sslstream.AuthenticateAsClient
(add,null, SslProtocols.Tls12, false);
var bytes = Encoding.Ascii.GetBytes("something");
sslstream.write( bytes,0,bytes.Length);
sslstream.Flush();
sslstream.Read(new byte[9],0,9); //Here it is waiting indefinitely..
Callback1 is a RemoteCertificateValidationCallback method and I am returning true here.
Callback2 is a Local certificate selection callback method and returning null here.
I am thinking like, how it can not able to read when it can able to write.
Could someone share some input on this issue.
PS: I typed entire code in mobile app. Regret for alignment.
I am trying to send a stream through UDP socket. The "SendTo" takes byte[] buffer argument. Not sure how to do this if I have a stream object (buffer). Please help! Thanks. The ByteBufferOutputStream does not seem to have a funciton to convert the stream to bytes.
ByteBufferOutputStream buffer = new ByteBufferOutputStream();
Avro.IO.Encoder ej = new BinaryEncoder(buffer);
ej.WriteInt(Convert.ToInt32(testEvent["schemaId"]));
var dwrd = new DefaultWriter(schema);
dwrd.Write<GenericRecord>(testEvent, ej);
buffer.Flush();
Socket clientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Udp);
IPAddress serverAddr = IPAddress.Parse("192.168.1.1");
IPEndPoint endPoint = new IPEndPoint(serverAddr, 2190);
clientSocket.SendTo(buffer, endPoint);
Actually it does. ByteBufferOutputStream has a method named GetBufferList, which returns an
IEnumerable of System.IO.MemoryStream. You can take these MemoryStreams, and concatenate them to a single buffer, with an integer header specifying how many streams are there (denoted by X), followed by X more integers specifying the length of the ordered streams, followed by the streams themselves.
You can send that entire buffer via UDP to your server, where it will reconstruct the streams, and
use ByteBufferInputStream (from Avro) which has a constructor that accepts them.
I'm making an application in PhoneGap, and I'm using HTML5 Websockets to send some info to another device (read: sending data to another user who has the app). Users are registered with ID and Username.
For this, I read that I have to make an TCP/Socket connection. I've tried to use an old socket-chat I once made in school with C#.
I'm connecting to the socket this way from the app:
var ws = new WebSocket("ws://127.0.0.1:7001");
ws.onopen = function()
{
// Web Socket is connected, send data using send()
ws.send("message");
};
And the server side looks like this (of course isn't the entire code, but the relevant parts)
private NetworkStream stream;
private IFormatter formatter = new BinaryFormatter();
private TcpListener server;
public TcpClient client;
private string username { get; set; }
private void StartServer()
{
int port = Convert.ToInt32(portUser);
server = new TcpListener(IPAddress.Parse(IP), port);
server.Start();
client = server.AcceptTcpClient();
stream = client.GetStream();
username = formatter.Deserialize(stream).ToString();
}
The last deserialize-line writes:
The input stream is not a valid binary format. The starting contents (in bytes) are: 47-45-54-20-2F-20-48-54-54-50-2F-31-2E-31-0D-0A-48 ...
Any other way to deserialize the stream? Or am I using a completely wrong way?
Edit: working C# way I used to send data between clients in the chatter
stream = client.GetStream();
formatter.Serialize(stream, messageobj);
stream.Flush();
I'm working on a biometry system with my C# application.
Sdk provices a connection via TCP/IP on port 2100, and works by receiving and sending strings to communicate .
My class:
class Biometry
{
private System.Net.Sockets.TcpClient _clientSocket = new System.Net.Sockets.TcpClient();
public Biometry() {
//connect to socket
_clientSocket.Connect("127.0.0.1", 2100);
_clientSocket.ReceiveTimeout = 9000;
}
public String identify(String msg) {
//get network stream
NetworkStream _serverStream = _clientSocket.GetStream();
//send an array of bites that represents a string(encoded)
System.Text.ASCIIEncoding ASCII = new System.Text.ASCIIEncoding();
byte[] outStream = System.Text.Encoding.ASCII.GetBytes(msg);
_serverStream.Write(outStream, 0, outStream.Length);
//reads the response from networkStream
byte[] inStream = new byte[10025];
_serverStream.Read(inStream, 0, (int)_clientSocket.ReceiveBufferSize);
string returndata = System.Text.Encoding.ASCII.GetString(inStream);
_serverStream.Close();
return returndata;
}
}
The problem is:
It is not working!! The biometry only works(SDK only understand my request) when I close the application(connection is closed).
You might need to flush the stream before your start reading using _serverStream.Flush().
Another problem might be that in your question you say you need to connect to port 21000, but in your code you connect to 2100, which might be a typo in either place, but should be fixed ;-)
In addition to flushing the stream, your server might also be waiting for an "end of message" indicator?