Why would nanomsg hang? - c#

I just started doing nnanomsg programming with C#. For a simplistic client / server example, I'm starting with the following example where a client connects with a server to get the next number. For now, the example is for localhost, except I have my network IP address hardcoded just to make sure that works. There would be potentially many clients connecting to the server, however this example accesses the server far more aggressively than it would be in real life.
Client Code:
using NNanomsg.Protocols;
using System;
using System.Text;
using System.Threading;
namespace Client {
class Program {
static void Main(string[] args) {
string connect = "tcp://127.0.1.107:9595";
while (true) {
PairSocket s = new PairSocket();
s.Connect(connect);
s.Send(Encoding.UTF8.GetBytes("Hello!"));
Console.WriteLine("Sent");
byte[] ss = null;
while (ss == null) {
ss = s.ReceiveImmediate();
Thread.Sleep(250);
}
s.Dispose();
Console.WriteLine("Received: " + Encoding.UTF8.GetString(ss));
} } } }
Server code:
using System;
using System.Text;
using NNanomsg.Protocols;
namespace Server {
public class Server {
public static int ITER = 0;
public static int NEXT_ID = 10001;
public static string CONNECT;
public static int PORT = 9595;
public static string QUERY;
public static string TCP = "tcp://127.0.1.107";
static void Main(string[] args) {
string bind = TCP + ":" + PORT;
Console.WriteLine("server starting...");
PairSocket s = new PairSocket();
s.Bind(bind);
while (true) {
byte[] ss = null;
ss = s.Receive();
Console.WriteLine("Received from client: " + Encoding.UTF8.GetString(ss));
byte[] b = Encoding.UTF8.GetBytes(NEXT_ID.ToString());
Console.WriteLine("Sent next id " + NEXT_ID);
s.Send(b);
NEXT_ID++;
if (NEXT_ID > 65535) { NEXT_ID = 10001; }
} } } }
In general, this works swimmingly when there is a server and a single client. However, when I start a second client on the same physical machine, they work together for a while, for maybe 5 to 10 iterations, then one of the clients, usually the one which was most recently started, hangs. Adding delays (Thread.sleep(5000) for example) improves the situation slightly, however it will eventually hang after maybe 50 iterations. Currently, the client stops immediately after displaying "Sent". I haven't yet tried starting a client from another physical machine on the network.
Also, whether I wait in a loop with ReceiveImmediate() or just sit at a Receive() does not seem to matter.
What I expect is that every client would have a chance to send the server a request and have a reply. The exact order is not that important, as long as the server eventually responds to a client's request.
I would like to understand why it hangs. I have the latest version of the DLL, 1.1.5.
Thanks very much for every bit of assistance.

Simple solution: I switched over to ZeroMQ. Evidently ZeroMQ handles multiple threads far better.

Related

Detect SQL event with C# application

I have the code below (a literal copy/paste from MSDN website just to be clear), which seems to connect as it should (except for an "Access Denied" error which is okay because my security requests haven't gone through yet). What I need to do is detect when our sql server has executed an insert or update operation. Basically this application should be running 24/7 and perform certain functions when an operation as such comes across the listener. I'm not asking for the code to be laid out in front of me, but I am asking where to start. This is something I have no clue how to do at this point and am being told I have roughly a week to figure it out and get it done. Can someone point me in the right direction? Thank you in advance!
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
namespace Connect_Server
{
class Program
{
static string output = "";
static void Main(string[] args)
{
createListener();
}
static public void createListener()
{
// Create an instance of the TcpListener class.
TcpListener tcpListener = null;
IPAddress ipAddress = Dns.GetHostEntry("localhost").AddressList[0];
try
{
// Set the listener on the local IP address
// and specify the port.
tcpListener = new TcpListener(ipAddress, 80);
tcpListener.Start();
output = "Waiting for a connection...";
}
catch (Exception e)
{
output = "Error: " + e.ToString();
Console.Write(output);
}
while (true)
{
// Always use a Sleep call in a while(true) loop
// to avoid locking up your CPU.
Thread.Sleep(10);
// Create a TCP socket.
// If you ran this server on the desktop, you could use
// Socket socket = tcpListener.AcceptSocket()
// for greater flexibility.
TcpClient tcpClient = tcpListener.AcceptTcpClient();
// Read the data stream from the client.
byte[] bytes = new byte[256];
NetworkStream stream = tcpClient.GetStream();
stream.Read(bytes, 0, bytes.Length);
SocketHelper helper = new SocketHelper();
helper.processMsg(tcpClient, stream, bytes);
}
}
}
}
Check out SQL Server Notifications. This will send a signal to your app when some underlying dataset changes. I'm not sure how heavy this is for the server, so if you have a large number of clients waiting for a notification you should load-test it carefully....

Cannot figure out how to get Socket to receive data C#

So what I am specifically trying to do is get a serial proxy going to pipe an arduino into Unity3d to run on linux. System.IO.Ports just doesn't work on linux with unity.
So I have gotten a python based serial proxy script, I have that up and running just fine. I can netcat into that localhost and actually get output from the arduino.
nc localhost 8082
g'day from bitty 1.0 -- type 'logout' to disconnect
Connecting to /dev/tty.usbmodem5d11... connected.
HELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLO
I have the arduino only printing out HELLO on repeat.
But the serial proxy is up and running, and has data going over it.
Now I am trying to write the code in Unity to receive this data, and I just can't get it to work.
This is my code for that:
public Socket socket = new Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);
public byte[] buffer = new byte[256];
int pinRead = 0;
void Start ()
{
socket.Connect(IPAddress.Parse("127.0.0.1"),8082);
}
void Update ()
{
if (socket.IsBound)
{
try
{
int bytesRead;
bytesRead = socket.Receive(buffer);
string incommingdata = System.Text.Encoding.ASCII.GetString(buffer);
Debug.Log(bytesRead+" || "+incommingdata);
}
catch (System.Exception e)
{
}
}
bytesRead is ALWAYS 0 and incomingData just doesn't have anything. It connects, and isBound returns true. I just can't get the code right to receive the data and put it in a format that is usable.
Please help. I need to get this working and its far out of my area of expertise. How do I make this work?
So I got this to work using tcpclient objects and some snippets of code that were posted on a random unity blog....
Here it is if anyone wants to see. I would still be really curious to know how to get the Socket implementation functioning though.
using UnityEngine;
using System.Collections;
using System.Net;
using System.Net.Sockets;
using System;
using System.Text;
using System.IO;
using System.Threading;
public class ArduinoTest : MonoBehaviour
{
private bool socketReady = false;
private TcpClient mySocket;
private NetworkStream theStream;
private StreamWriter theWriter;
private StreamReader theReader;
private string Host = "localhost";
private Int32 Port = 9900;
private int pinRead = 0;
void Start ()
{
setupSocket();
}
void Update ()
{
string read = readSocket();
if (read != "")
{
int value = int.Parse(read);
if ( value > 53)
{
Debug.Log((value-54)+" DOWN");
}
else
{
Debug.Log(value+" UP");
}
}
}
void OnApplicationQuit()
{
writeSocket("logout");
closeSocket();
}
public void setupSocket()
{
try
{
mySocket = new TcpClient(Host, Port);
theStream = mySocket.GetStream();
theWriter = new StreamWriter(theStream);
theReader = new StreamReader(theStream);
socketReady = true;
}
catch (Exception e)
{
Debug.Log("Socket error:" + e);
}
}
public void writeSocket(string theLine)
{
if (!socketReady)
return;
String tmpString = theLine + "\r\n";
theWriter.Write(tmpString);
theWriter.Flush();
}
public string readSocket()
{
if (!socketReady)
return "";
if (theStream.DataAvailable)
return theReader.Read().ToString();
return "";
}
public void closeSocket()
{
if (!socketReady)
return;
theWriter.Close();
theReader.Close();
mySocket.Close();
socketReady = false;
}
public void maintainConnection()
{
if(!theStream.CanRead)
{
setupSocket();
}
}
}
(Sorry I can't post comments yet or would do so there)
BytesRead = 0 typically means the remote side disconnected.
For what it's worth, your code works fine when I run a test TCP server listening for connections on port 8082 and sending out some text messages. So I doubt the problem is on the C# client-side.
Beyond this simple example, "getting this to work" is not necessarily a simple question. Here are a couple things you should keep in mind:
You are making a blocking Receive call, which means you will need to call Update in a loop.
TCP is a streaming protocol, so you might get partial messages or many messages bundled together in the same Read call. Look up "TCP Framing" to get more details on this.
You can try using telnet on this socket, just to see if you can connect to it.
telnet 127.0.0.1 8082

Lost UDP 'connection' between client app and server app

I wrote two small applications (a client and a server) to test UDP communication and I found the 'connection' (yeah, I know, there's no real connection) between them gets lost frecuently for no reason.
I know UDP is an unreliable protocol, but the problem here does not seem the losing of packets, but the losing of the communication channel between the apps.
Here's client app code:
class ClientProgram
{
static void Main(string[] args)
{
var localEP = new IPEndPoint(GetIPAddress(), 0);
Socket sck = new UdpClient(localEP).Client;
sck.Connect(new IPEndPoint(IPAddress.Parse("[SERVER_IP_ADDRESS]"), 10005));
Console.WriteLine("Press any key to request a connection to the server.");
Console.ReadLine();
// This signals the server this clients wishes to receive data
SendData(sck);
while (true)
{
ReceiveData(sck);
}
}
private static void ReceiveData(Socket sck)
{
byte[] buff = new byte[8];
int cnt = sck.Receive(buff);
long ticks = BitConverter.ToInt64(buff, 0);
Console.WriteLine(cnt + " bytes received: " + new DateTime(ticks).TimeOfDay.ToString());
}
private static void SendData(Socket sck)
{
// Just some random data
sck.Send(new byte[] { 99, 99, 99, 99 });
}
private static IPAddress GetIPAddress()
{
IPHostEntry he = Dns.GetHostEntry(Dns.GetHostName());
if (he.AddressList.Length == 0)
return null;
return he.AddressList
.Where(ip => ip.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork && !IPAddress.IsLoopback(ip))
.FirstOrDefault();
}
}
Here's the server app code:
class ServerProgram
{
private static int SLEEP = 5;
static void Main(string[] args)
{
// This is a static IP address
var localEP = new IPEndPoint(GetIPAddress(), 10005);
Socket sck = new UdpClient(localEP).Client;
// When this methods returs, a client is ready to receive data
var remoteEP = ReceiveData(sck);
sck.Connect(remoteEP);
while (true)
{
SendData(sck);
System.Threading.Thread.Sleep( ServerProgram.SLEEP * 1000);
}
}
private static EndPoint ReceiveData(Socket sck)
{
byte[] buff = new byte[8];
EndPoint clientEP = new IPEndPoint(IPAddress.Any, 0);
int cnt = sck.ReceiveFrom(buff, ref clientEP);
Console.WriteLine(cnt + " bytes received from " + clientEP.ToString());
return (IPEndPoint)clientEP;
}
private static void SendData(Socket sck)
{
DateTime n = DateTime.Now;
byte[] b = BitConverter.GetBytes(n.Ticks);
Console.WriteLine("Sending " + b.Length + " bytes : " + n.TimeOfDay.ToString());
sck.Send(b);
}
private static IPAddress GetIPAddress()
{
// Same as client app...
}
}
(this is just test code, don't pay attention to the infinite loops or lack of data validation)
The problem is after a few messages sent, the client stops receiving them. The server keeps sending, but the client gets stuck at sck.Receive(buff). If I change SLEEP constant to a value higher than 5, the 'connection' gets lost after 3 or 4 messages most of the time.
I can confirm the client machine doesn´t receive any packet when the connection is lost since I use Wireshark to monitor the communication.
The server app runs on a server with direct connection to Internet, but the client is a machine in a local network, behind a router. None of them has a firewall running.
Does anyone have a clue what could be happening here?
Thank you!
EDIT - Additional data:
I tested the client app in several machines in the same network and the connection get lost always. I also tested the client app in other network behind other router and there is no problem there. My router is a Linksys RV042 and never had any problem with it, in fact, this app is the only one with problems.
PROBLEM SOLVED - SHORT ANSWER: It was a hardware problem.
I don't see any overt problems in the source code. If it is true that:
The server keeps sending packets (confirmed by WireShark on the server)
The client never receives the packets (confirmed by WireShark on the client)
..then the problem could be related to networking equipment between the two machines, which don't always handle UDP flows as expected especially when behind routers/firewalls.
I would recommend the following approach to troubleshooting:
Run client & server on the same system and use the loopback interface (confirm UDP code works on the loopback)
Run the client & server on two different systems that are plugged into the same Ethernet switch (confirm that UDP communication works switch-local)
If everything works switch-local you can be fairly sure you have a network configuration problem.

C# Client / Server Thread Connection

I am getting into trouble with this part of code.
In fact I want to set a Client/Server Application.
In the client part I launch a Thread which function is only to check everytime if it is connected to the server (if the connection to the server is still established)
TraceLog is a class that uses its Info() method to write in a file.
this is is the client code :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Sockets;
using System.Net;
using System.Threading;
using System.IO;
namespace ClientApp
{
class ClientOpenConnection
{
private static Thread threadConnect;
static TcpClient myClient = new TcpClient();
static String host = "";
static Int32 port = 0;
//Function that makes the client runs
public static void RunClient(String hostname, Int32 hostport)
{
host = hostname;
port = hostport;
int _tryAgain = 0;
while (!myClient.Connected) {
try
{ //I start the connection
myClient.Connect(host, port);
}
catch {
}
_tryAgain += 10;
if (_tryAgain == 1000)
break;
//_tryAgain allows me to define how long will the client try to connect to the server.
}
TraceLog.Info("Out of the while ", ""); // This is to know where am I
if (_tryAgain != 1000)
{ //If I get out because _tryAgain is less than 1000. It means that I am already connected to the server
//Here I start a Thread to be sure that I am always connected to the server
threadConnect = new Thread(isConnected);
threadConnect.Start();
TraceLog.Info("Launch the thread","");
}
//While threadConnect is executing parallely I continue my program
}
private static void isConnected() {
//I keep my eyes on the network connection
while (myClient.Connected) {
//Nothing is done
}
TraceLog.Info("The connection has been lost","");
RunClient(host,port);
}
}
}
The problem that I am having, when I start the client before the server I enter the first WHILE loop. it is OK at this level.
and when I start the server after, I launch the threadConnect but the problem is that if now I stop the server, normally i should have inside the log file "The connection has been lost" but I have nothing.
What is wrong with this part of code?
Have you already done something like this in the past?
I come with a modification but still having problem to obtain what I want, ie the client still get trying to contact the server eveytime even if the server is stopped .
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Sockets;
using System.Net;
using System.Threading;
using System.IO;
namespace ClientApp
{
class ClientOpenConnection
{
private static Thread threadConnect;
static TcpClient myClient = new TcpClient();
static String host = "";
static Int32 port = 0;
//Function that makes the client runs
public static void RunClient(String hostname, Int32 hostport)
{
host = hostname;
port = hostport;
TraceLog.Info(" -> "+myClient.Connected,"");
while (!myClient.Connected) {
try
{
myClient.Connect(host, port);
TraceLog.Info(" <-> " + myClient.Connected, "");
}
catch {
TraceLog.Info("Trying to contact the server","");
}
}
TraceLog.Info("I am connected ", "");
//Here I start a Thread to be sure that I am always connected to the server
threadConnect = new Thread(isConnected);
threadConnect.Start();
TraceLog.Info("Launch the thread to be sure I am constantly online","");
}
private static void isConnected() {
//I keep my eyes on the network connection
TraceLog.Info("->>"+myClient.Connected,"");
while (myClient.Connected) {
Thread.Sleep(500);
try
{
NetworkStream stream = myClient.GetStream();
ASCIIEncoding ascii = new ASCIIEncoding();
byte[] _incomingMsg = new byte[1024];
stream.Read(_incomingMsg, 0, _incomingMsg.Length);
String strToGet = System.Text.Encoding.ASCII.GetString(_incomingMsg);
strToGet = strToGet.Trim();
if (!strToGet.Equals("ONLINE"))
if (strToGet.Equals(""))
{
TraceLog.Info("The message receive is empty","");
break;
}
}
catch {
break;
}
}
TraceLog.Info("The connection has been lost", "");
RunClient(host, port);
}
}
}
But when I call the RunClient() in the isConnected() function it executes in the WHILE and output TraceLog.Info("Trying to contact the server",""); even if I start the server again, the client remains in the while loop and never connects at all.
From MSDN:
The Connected property gets the connection state of the Client socket
as of the last I/O operation. When it returns false, the Client socket
was either never connected, or is no longer connected.
Because the Connected property only reflects the state of the
connection as of the most recent operation, you should attempt to send
or receive a message to determine the current state. After the message
send fails, this property no longer returns true. Note that this
behavior is by design. You cannot reliably test the state of the
connection because, in the time between the test and a send/receive,
the connection could have been lost. Your code should assume the
socket is connected, and gracefully handle failed transmissions.
In other words, in order to check if you are still connected, you need to send or receive some data and then check the connection state.
Since your code doesn't send any packets after the connection is made, the connected property always returns true, and the loop never exits.

How can I use a threadpool to process each connection in a new thread

I have a simple server that waits for a client to connect, reads the incoming stream, and sends a message back. What I would like to do is have every connection handled by a separate thread. This is my first time working with sockets and threads in C#, and most of the examples I have found are really confusing, so any help or simple examples would be much appreciated.
Here is what I have right now.
using System;
using System.Collections.Generic;
using System.Net;
using System.Net.Sockets;
using System.Text;
[STAThread]
static void Main(string[] args)
{
TestServer ts = new TestServer();
ts.Start();
}
class TestServer
{
private readonly int port = 48888;
private readonly IPAddress ip = IPAddress.Parse("127.0.0.1");
private TcpListener listener;
public TestServer()
{
this.listener = new TcpListener(this.ip, this.port);
}
public void Start()
{
this.listener.Start();
Console.WriteLine("Server Running...");
Socket s;
Byte[] incomingBuffer;
int bytesRead;
Byte[] Message;
while (true)
{
s = this.listener.AcceptSocket();
incomingBuffer = new Byte[100];
bytesRead = s.Receive(incomingBuffer);
string message = "Hello from the server";
Message = Encoding.ASCII.GetBytes(message.ToCharArray());
s.Send(Message);
}
}
}
Try this:
public void Start()
{
this.listener.Start();
Console.WriteLine("Server running...");
while (true)
{
Socket s = this.listener.AcceptSocket();
ThreadPool.QueueUserWorkItem(this.WorkMethod, s);
}
}
private void WorkMethod(object state)
{
using (Socket s = (Socket)state)
{
byte[] buffer = new byte[100];
int count = s.Receive(buffer);
string message = "Hello from the server";
byte[] response = Encoding.ASCII.GetBytes(message);
s.Send(response);
}
}
Not answering your question directly, but ...
Thread pools are not about 'new thread per connection', they are about having some meaningful number of threads (with some relation to number of cores on the box) already running and waiting for work. This "work" is given to the pool by the producer thread (the one accepting the connections in your case) via one or more queues.
Please note that this is not always the best solution though. Take a look at C10K and at Hight Performance Server Architecture pages.
Use a threadpool. You can use instantiate threads manually but since you have might have a huge number of connections, a threadpoool is more efficient.
System.Threading.ThreadPool.QueueUserWorkItem(new System.Threading.WaitCallback(processMessage), socket);
processMessage is the method that will process the message. And there will be one thread per connection. Really simple actually.

Categories

Resources