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.
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 have a socat TCP listener running on Linux (SLES 12). Any client that connects will pass a string to the socket and socat will execute a script which does some work with based on the string. The script echos some output which is passed back to client.
socat TCP-LISTEN:9996,fork EXEC:/home/abhishek/hello.sh
Below is the hello.sh script.
#!/bin/bash
read str
echo "[Hello] $str" | tee -a test.txt
When I run ncat client this works as expected. ncat is able to get the data back and output it.
echo abhishek | ncat 192.168.1.12 9996
[Hello] abhishek
Now I want to connect to socat through a .NET client written in C#. Below is the raw socket based code I have come up with.
IPAddress address = IPAddress.Parse("192.168.1.12");
IPEndPoint ipe = new IPEndPoint(address, 9996);
Socket client = new Socket(ipe.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
client.Connect(ipe);
byte[] msg = Encoding.ASCII.GetBytes("abhishek");
// Send the data through the socket.
int bytesSent = client.Send(msg);
Console.WriteLine("Sent {0} bytes.", bytesSent);
byte[] bytes = new byte[128];
int bytesRec = client.Receive(bytes); ;
Console.WriteLine("Response text = {0}", Encoding.ASCII.GetString(bytes, 0, bytesRec));
client.Shutdown(SocketShutdown.Both);
client.Close();
I get the line "Sent 8 bytes" but after that client hangs on Receive. The client data is received by hello.sh becaused test.txt contains new entry. When I kill the client (Ctrl+C) socat prints
2018/02/03 10:12:03 socat[21656] E write(4, 0xe530f0, 17): Broken pipe
How should I read the reply from socat in C#?
Thanks
I was able to use TcpClient to establish communication with socat and get the output from script. Below is the main part of code I implemented.
TcpClient client = new TcpClient("192.168.1.12", 9999);
NetworkStream stream = client.GetStream();
StreamWriter writer = new StreamWriter(stream) { AutoFlush = true };
StreamReader reader = new StreamReader(stream);
//Send message to socat.
writer.WriteLine(message);
//Receive reply from socat.
string reply = reader.ReadLine();
stream.close();
client.close();
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
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 using C# .NET to upload numerous, large, files from client to server using SslStream.
It's twice as slow as un-encrypted upload. How much delay is normal?
Any suggestions on how to improve the performance of encryption framework? Would lowering the encryption strength of pfx/pvk help? If so how to do it? All tips are welcome.
Edit:
In each client session 1 or multiple files are uploaded. In my test there were 1250. Client uses same SslStream for a session...
Server code:
private void OnAcceptTcpClient(IAsyncResult result)
{
var client = _listener.EndAcceptTcpClient(result);
Stream stream = client.GetStream();
if (_cert != null)
{
var ssl = new SslStream(stream, false);
ssl.AuthenticateAsServer(_cert, false, SslProtocols.Tls, false);
stream = ssl;
}
// Processing block
...
}
Client Code:
public IAsyncResult BeginSend(string host, int port, bool bTls, AsyncCallback callback, object state)
{
_client = new TcpClient(host, port) { NoDelay = NoDelay };
Stream stream = _client.GetStream();
if (bTls)
{
var ssl = new SslStream(stream, false, new RemoteCertificateValidationCallback((o, c, ch, er) => true));
ssl.AuthenticateAsClient(host);
stream = ssl;
}
// Send all files
return BeginSend(stream, callback, state);
}
Thank you very much.
Are you using the X509 Certificate to cryptography all files sent to server? I think one solution could be using the X509 Certificate just in the beginning in order to exchange a random generated symmetric cryptography key and then use it to cryptography the files to be sent. Just like https does.
Another tip is to see if the cryptographed data is getting too much bigger.