Verifying delivery of TCP messages - c#

I'm writing an application to send syslogs to a central server. The network connection to the server is expected to be unreliable. For this reason, I need to be able to check whether the messages have been received by the server, so that if the send is unsuccessful I can try sending them again later.
I have no control over the server, only the ability to form a TCP connection with it - most questions I've seen recommend writing custom ACK logic, but that's not an option for me. I need an entirely client-side approach. Is there any way I can access the TCP acknowledgements in order to mark a message as sent?

The TCP protocol guarantees that the data is properly delivered at the remote server. To quote from the original TCP specification in RFC 793:
An acknowledgment by TCP does not guarantee that the data has been
delivered to the end user, but only that the receiving TCP has taken
the responsibility to do so.
So if you do not get an error from your sending part, you have a confirm that the remove server TCP stack has properly received your data. TCP has built-in detection for damaged, lost, duplicated, or out of order delivered packets (using sequence numbers) and will retransmit packets when it detects problems.

As the other poster has mentioned, TCP is a reliable protocol. You can wrap this in some exception handling code to ensure that messages are re-sent when the connection is restored, for example:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Net.Sockets;
namespace TCPClientTest
{
class SyslogMessage
{
public Guid Guid = Guid.NewGuid();
public byte[] MessageData;
}
class Program
{
static readonly int TCP_PORT = 1337;
static byte[] SendTCPMessage(String hostname, int port, byte[] data)
{
using (var client = new TcpClient(hostname, port))
{
using (var stream = client.GetStream())
{
stream.Write(data, 0, data.Length);
var responseData = new byte[1024];
var byteCount = stream.Read(responseData, 0, responseData.Length);
return responseData.Take(byteCount).ToArray();
}
}
}
static void SendSyslog(String hostname, int port, SyslogMessage m)
{
SendTCPMessage(hostname, port, m.MessageData);
}
static Queue<SyslogMessage> sysLogQueue = new Queue<SyslogMessage>(new List<SyslogMessage>()
{
new SyslogMessage() { MessageData = Encoding.UTF8.GetBytes("Test data 1")},
new SyslogMessage() { MessageData = Encoding.UTF8.GetBytes("Test data 2")}
});
static void Main(string[] args)
{
System.Threading.Timer timer = new System.Threading.Timer(SendLogs, null, 1000, 5000);
Console.WriteLine("Press return to continue...");
Console.Read();
}
static void SendLogs(object state)
{
while (sysLogQueue.Count > 0)
{
try
{
var m = sysLogQueue.Peek();
SendSyslog("localhost", TCP_PORT, m);
Console.WriteLine("Sent sys log: " + Encoding.UTF8.GetString(m.MessageData));
// Remove from queue.
sysLogQueue.Dequeue();
}
catch (SocketException e)
{
// Leave in the queue.
Console.WriteLine("Failed to send log: Socket exception occurred: {0}", e);
// Break until next attempt.
break;
}
}
}
}
}
I've used port 1337 for this, just set this to whatever you need.

Related

Comparing a string received via TCP against another string

I obtained the following code from MSDN :
using System;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Text;
class MyTcpListener
{
public static void Main()
{
TcpListener server = null;
try
{
Int32 port = 13000; // Set the TcpListener on port 13000.
IPAddress localAddr = IPAddress.Parse("127.0.0.1");
server = new TcpListener(localAddr, port); // TcpListener server = new TcpListener(port);
server.Start(); // Start listening for client requests.
// Buffer for reading data
Byte[] bytes = new Byte[256];
String data = null;
// Enter the listening loop.
while (true)
{
Console.Write("Waiting for a connection... ");
// Perform a blocking call to accept requests.
// You could also user server.AcceptSocket() here.
TcpClient client = server.AcceptTcpClient();
Console.WriteLine("Connected!");
data = null;
NetworkStream stream = client.GetStream();// Get a stream object for reading and writing
int i;
while ((i = stream.Read(bytes, 0, bytes.Length)) != 0) // Loop to receive all the data sent by the client.
{
data = System.Text.Encoding.ASCII.GetString(bytes, 0, i); // Translate data bytes to a ASCII string.
Console.WriteLine("Received: {0}", data);
data = data.ToUpper();// Process the data sent by the client.
byte[] msg = System.Text.Encoding.ASCII.GetBytes(data);
stream.Write(msg, 0, msg.Length); // Send back a response.
Console.WriteLine("Sent: {0}", data);
if(data == "STOP")
{
Console.WriteLine("Stop command Received.");
Console.ReadKey();
Environment.Exit(0);
}
Console.WriteLine(data.Length);
}
client.Close(); // Shutdown and end connection
}
}
catch (SocketException e)
{
Console.WriteLine("SocketException: {0}", e);
}
finally
{
server.Stop();// Stop listening for new clients.
}
Console.WriteLine("\nHit enter to continue...");
Console.Read();
}
}
except the following lines which were inserted by me:
if(data == "STOP")
{
Console.WriteLine("Stop command Received.");
Console.ReadKey();
Environment.Exit(0);
}
I sent a string "STOP" via a tcp client. However, in the server, comparing the received string "STOP" against "STOP" in the 'if block' was of no use ie., nothing in that block got executed.
What is the actual error in this approach ? What changes should I make to compare the strings properly ?
If Ben's answer does not solve your problem there is a 2nd major problem with your code.
Your code has no guarantees that you won't get ST in one read and OP in the next read. A single send is allowed to be split in to multiple reads, and multiple sends are allowed to be combined in to a single read. So sending Test then STOP could show up as TestSTOP when you call the read function.
You need to add additional logic to the code to tell when one message stops and another starts on the receiving side. This is called Message Framing and for your additions to the program to work you will need to add that logic in to your program.
If you are sending the text via WriteLine (vs. Write), the string you are getting won't be "STOP" but rather "STOP\r\n", so you would want to Trim() the string to check for equality.
data.Trim() == "STOP"

C# Winform to Connect to Device Using IP

I need to create an application that allows the user to connect to a device (a piece of hardware that will be sent arguments/commands) via wifi (wifi is end goal but I am settling at the moment for any connection) and then send commands to said device. I know some C# and some sockets/IP stuff, but not sockets/IP using C#. The visual, GUI side of the program is not what I am struggling with. I cannot seem to get the socket up and running and make any real connection. I keep getting the "An Invalid Argument was Supplied" exception.
Any tips on this particular issue or help with C# networking/sockets/etc. are welcome.
I declare a new socket by:
Socket sck;
sck = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
The exception is thrown when it tries to process the "sck = new Socket(...);"
Code in Question:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Net.Sockets;
using System.Net;
using System.Threading;
using System.IO;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Text.RegularExpressions;
namespace InternetConnect
{
public partial class Form1 : Form
{
// Global Variables
int portNumber = 0;
string ipAddress = "";
TcpClient client;
Socket sck;
EndPoint epLocal, epRemote;
// Needs to be defined at some point
// These are just holding a place for now
string extraIP = "127.0.0.1";
string extraPort = "135";
public Form1()
{
InitializeComponent();
try
{
sck = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
sck.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
}
catch (SocketException ex)
{
MessageBox.Show(ex.Message);
}
}
private void connectButton_Click(object sender, EventArgs e)
{
try
{
epLocal = new IPEndPoint(IPAddress.Parse(ipAddressTextBox.Text), Convert.ToInt32(portNumberTextBox.Text));
MessageBox.Show("Before Bind");
sck.Bind(epLocal);
MessageBox.Show("After Bind");
epRemote = new IPEndPoint(IPAddress.Parse(extraIP), Convert.ToInt32(extraPort));
sck.Connect(epRemote);
MessageBox.Show("After Connect");
byte[] buffer = new byte[1500];
sck.BeginReceiveFrom(buffer, 0, buffer.Length, SocketFlags.None, ref epRemote, new AsyncCallback(MessageCallBack), buffer);
connectButton.Text = "Connected";
connectButton.Enabled = false;
sendButton.Enabled = true;
outgoingTextBox.Focus();
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
ipAddress = ipAddressTextBox.Text;
// To make sure there are no letters in the IP Address
int errorCounter = Regex.Matches(ipAddress, #"[a-zA-Z]").Count;
if (errorCounter == 0)
{
if (ipAddress != "")
{
// To make sure the port number is entered without letters
if (int.TryParse(portNumberTextBox.Text, out portNumber))
{
WriteToStatusBar("IP Address and Port Number Valid");
}
else
{
MessageBox.Show("Please enter a valid Port Number.");
}
}
else
{
MessageBox.Show("Please enter a valid IP Address.");
}
}
else
{
MessageBox.Show("Please enter a valid IP Address.");
}
#endregion
try
{
client = new TcpClient();
WriteToStatusBar("Connecting...");
client.Connect(ipAddress, portNumber);
WriteToStatusBar("Connected");
outgoingTextBox.Text = "Enter message to be sent to the device...";
client.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
As per request of the OP some information about using TcpClient and even TcpListener in case you need to create a server as well. Following link will help you get started with using TcpClient :https://msdn.microsoft.com/en-us/library/system.net.sockets.tcpclient%28v=vs.110%29.aspx?f=255&MSPPError=-2147217396
or this one in code project covers both client and server: http://www.codeproject.com/Articles/1415/Introduction-to-TCP-client-server-in-C
in that code (the first link) you'll find following statement : stream.Write(data, 0, data.Length);
You want to write multiple times to the socket then suppose you write the same data twice you can do simply :
stream.Write(data, 0, data.Length);
stream.Write(data, 0, data.Length);
At the receiver, you have to realise that because TCP is streaming, you might receive to 2 send 'messages' in one receive or scattered over multiple receives.
Further in the code you will find :
stream.Close();
client.Close();
Which ends the communication and closes the socket, sending after doing the close is not possible anymore.
Using sockets, but also tcpclient and tcplistener (because they are based on sockets), is something that I consider a bit advanced material. Without really understanding what streaming is, what IP addressing is, what TCP, UDP is, how to use sockets , some basic understanding of threads. It is easy to get lost. I am a professional programmer, and I did not touch this material after I read quite a lot of information about it.
The best thing to do is to do more investigation, based on examples on the internet & books. And ask very specific dedicated questions using this medium. Elaborate questions have a tendency to get closed without proper answer which will lead to frustration of course.

C# tcp stream - is it possible to run stream without listener?

I want to write a part of code in which I start sending packages using tcp stream, but without any part that receives it over the internet.
In the meantime I would also like to have another method that can connect to that particular stream at any time and starts receiving bytes from the moment it connects.
I wanted to use multicasting, but I see it's impossible to do it over the Internet
Is there a way to do it? I only found some info so far, that the tcp connection in C# uses point to point way, so my case sounds impossible to implement, because the listener has to be always active to even initialize the streamer, how can I bypass that?
Edit Added an example of a simply "broker" who republishes all messages it receives.
Either use UDP and broadcast your packets to an endpoint that may or maynot be listening at any point in time.
Or use a message queue such as MSMQ, RabbitMQ or 0MQ.
MSMQ may become a problem if the listening service is offline for to long as the messages queue on your dispatch system resulting in a backlog that may fill.
If you would like to create something using UDP here is some code.
Listener (server):
using System;
using System.Text;
using System.Net;
using System.Net.Sockets;
namespace UDPLISTENER
{
class Program
{
static void Main(string[] args)
{
var port = 8750;
var listener = new UdpClient(port);
var group = new IPEndPoint(IPAddress.Any, port);
Console.WriteLine("Listening for datagrams on port {0}", port);
while(true)
{
var data = listener.Receive(ref group);
Console.WriteLine("{0}: {1}", group.ToString(), Encoding.Default.GetString(data, 0, data.Length));
}
}
}
}
Broker (Server&Client):
sing System;
using System.Text;
using System.Net;
using System.Net.Sockets;
namespace UDPCLIENT
{
class Program
{
static void Main(string[] args)
{
int listeningPort = 8749, dispatchPort = 8750;
var listener = new UdpClient(listeningPort);
var group = new IPEndPoint(IPAddress.Any, listeningPort);
// Republish client
var sender = new UdpClient("127.0.0.1", dispatchPort);
Console.WriteLine("Listening for datagrams on port {0}", listeningPort);
while (true)
{
var data = listener.Receive(ref group);
Console.WriteLine("{0}: {1}", group.ToString(), Encoding.Default.GetString(data, 0, data.Length));
sender.Send(data, data.Length);
}
}
}
}
Sender (Client):
using System;
using System.Text;
using System.Net;
using System.Net.Sockets;
namespace UDPSENDER
{
class Program
{
static void Main(string[] args)
{
var sender = new UdpClient("127.0.0.1", 8749);
while (true)
{
Console.WriteLine("Message: ");
var data = Encoding.Default.GetBytes(Console.ReadLine());
sender.Send(data, data.Length);
}
}
}
}
Depending what you wish to archive I recommend message queues they give to the most flexibility.
But as you can see UDP works a bit differently to TCP. You don't need a handshake like TCP does, this means if no one is listening to your messages they vanish with no cost to the sender (there is still cost to the network). But if a listener pops up then they start consuming the messages right away. Remember no UDP packet is guaranteed delivery, this is both a blessing and a curse.
If you want messages to be guaranteed you need to implement your own solution, one method is a counter on the datagrams that the listener watches if there is a message missing then it request that message be resent.
You can create a new class derived from Stream, override the Write method, and manage the writing to the tcp clients here. You will be able to write to this stream from your code whether a client is connected or not. When no client is connected, the data will simply be ignored.

C# TCPClient/Socket writing not throwing exception

I have many printers I am trying to connect to over tcp connections. I am trying to verify that my TcpClient is still connected to update a GUI. I am trying to write to a socket to make sure its still connected. I get no exception even if the cable is unplugged I tried all of the suggestions here MSDN_Fourm
I am receiving the expected exception after I try to check the printer statuses
psudo-code
client is a TCPClient that has been connected previously
private bool FuntionPsudo(){
try{
if(client.Connected){
byte[] buf = new byte[1];
client.Client.Send(buf, 0,0);
client.GetStream().Write(buf,0,0);
if(client.Client.Receive(buf,SocketFlags.Peek)==0)
return false;
return true;
}
}
catch(Exception){
return false;
}
return false;
}
FuntionPsudo returns: true
cable unplugged
FuntionPsudo returns: true
FuntionPsudo returns: true
check printer status
FuntionPsudo returns: false
Thanks in advance for any help on why this might be happening and/or how to fix it
After several failed attempts I realised 'unplug-the-cable' type of connecting detection isn't that easy. At the same time I found that there are a couple of tricks you can do to check if the server has closed the connection, all without needing to send hearbeat kind of messages.
Here is what I came up with that I could say it works most of the time (especially with cable disconnects it's not easy to figure out if connection is still up)
static class SocketUtils
{
public static bool IsConnected(this Socket socket)
{
return IsSocketConnected(socket) && IsNetworkConnected(socket);
}
public static void KeepAlive(this Socket socket, int pollSeconds)
{
socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.KeepAlive, true);
SetIOControlKeepAlive(socket, (uint)(pollSeconds * 1000), 1);
}
static bool IsNetworkConnected(this Socket socket)
{
try
{
return socket.Send(new byte[0]) == 0;
}
catch (SocketException) { return false; }
}
static bool IsSocketConnected(this Socket socket)
{
try
{
return !(socket.Poll(1, SelectMode.SelectRead) && socket.Available == 0);
}
catch (SocketException) { return false; }
}
static void SetIOControlKeepAlive(Socket socket, uint time, uint interval)
{
var sizeOfUint = Marshal.SizeOf(time);
var inOptionValues = new byte[sizeOfUint * 3];
BitConverter.GetBytes((uint)(time == 0 ? 0UL : 1UL)).CopyTo(inOptionValues, 0);
BitConverter.GetBytes(time).CopyTo(inOptionValues, sizeOfUint);
BitConverter.GetBytes(interval).CopyTo(inOptionValues, sizeOfUint * 2);
socket.IOControl(IOControlCode.KeepAliveValues, inOptionValues, null);
}
}
Here is how you can use it:
var tcpClient = new TcpClient();
tcpClient.Connect("192.168.2.20", 3000);
// set this to a low value to detect cable disconnects early
tcpClient.Client.KeepAlive(30); // 30 seconds
Console.WriteLine("Connected..");
while (true)
{
Thread.Sleep(500);
Console.WriteLine(tcpClient.Client.IsConnected());
}
I must add that I shamelessly copied some code from Samuel's answer about checking client disconnects and Greg Dean's answer about setting keep-alive on the socket, so some credit should go to them as well ;)
You can only tell whether you are still connected or not by sending something and receiving something back. Just pushing bytes out into the network always works even if they go into a black hole. The Connected property is unreliable and almost all code using it is wrong.
Send something to the printer and receive a reply. Or, create a new connection (which internally will send and receive TCP control packets without data).
When dealing with transport layers like the TCP protocol you need to use it like a 'Walkie-Talkie'. You need to decide when and for how long to talk. In other words the communication breaks down when both parties talk or listen at the same time.
Here is an example from the book C# in a Nutshell:
using System;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Threading;
class TcpDemo
{
static void Main()
{
new Thread(Server).Start(); // Run server method concurrently.
Thread.Sleep(500); // Give server time to start.
Client();
}
static void Client()
{
using (TcpClient client = new TcpClient("localhost", 51111 ))
using(NetworkStream n = client.GetStream())
{
BinaryWriter w = new BinaryWriter(n);
w.Write("Hello");
w.Flush();
Console.WriteLine(new BinaryReader(n).ReadString());
}
}
static void Server()
{
TcpListener listener = new TcpListener(IPAddress.Any, 51111);
listener.Start();
using(TcpClient c = listener.AcceptTcpClient())
using(NetworkStream n = c.GetStream())
{
string msg = new BinaryReader(n).ReadString();
BinaryWriter w = new BinaryWriter(n);
w.Write(msg + " right back!");
w.Flush();
}
listener.Stop();
}
}
I have same this propblem for reconnect.
I write server in java and client in c# (unity)
java-java throw exception ok
java-c# : both of them throw exception in some case.
I have the best way for perfomance server
I resolve by the way : write jar client and use ikvm export to dll (copy jar to ikvm/bin). Create library in c# and reference dll + ikvm.core.dll + .../manage/unityEngine.dll ==> copy Cshapr/bin/Debug to UnityProJect/Asset
--> it run ok but speed over 37M to build 😭
If you want to have a small client --> network with no reconnect 😅

When to close a UDP socket

I have a client-server application that uses a UDP socket to send the data , the data only have to travel from client to server , and the server will always have the same IP. The only requirement is that I have to send messages about 10 messages per second
Currently I am doing it the following way :
public void SendData(byte[] packet)
{
IPEndPoint end_point = new IPEndPoint(serverIP, serverPort);
UdpClient udpChannel = new UdpClient(sourcePort);
udpChannel.Connect(end_point);
udpChannel.Send(packet, packet.Length);
udpChannel.Close();
}
The problem I have is that when I use the command "udpChannel.Close()" it takes 2-3 seconds to be performed when the server is not listening. (I've seen the same problem in: What is the drawback if I do not invoke the UdpClient.Close() method?)
My question would be, if I always send packets to the same IP address and port, is it necessary to connect the socket and close it after each send request?
The code I intend to use would be as follows:
UdpClient udpChannel;
public void SendData(byte[] packet)
{
udpChannel.Send(packet, packet.Length);
}
public void Initialize(IPAddress IP, int port)
{
IPEndPoint end_point = new IPEndPoint(serverIP, serverPort);
UdpClient udpChannel = new UdpClient(sourcePort);
udpChannel.Connect(end_point);
}
public void Exit()
{
udpChannel.Close();
}
Doing it this way, would it be necessary to do some checking in the "SendData" method before sending the data?
Is there any problem in the above code?
Thank you!
UDP is connectionless, calling udpChannel.Connect merely specifies a default host endpoint for use with the Send method. You do not need to close the client between sends, leaving it open will not leave any connections or listeners running between sends.
You shouldn't connect/close after each send request. When you start working - you connect to socket. And you can send data. You should close UdpClient when you do not want to send/recieve data, for example when you closing Form.
In your case you can check that udpClient != null when close/send client and you can use try/catch, for example:
try
{
udpClient.Send(sendBytes, sendBytes.Length);
}
catch (Exception exc)
{
// handle the error
}
Use try/catch when you connecting, because port may be busy or other problem with connection.
And look at UdpClient.SendAsync :)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Networking;
using System.Text;
using System.Net.Sockets;
using System;
using System.Net;
public class Server : MonoBehaviour
{
//int[] ports;
UdpClient udp; // Udp client
private void Start()
{
udp = new UdpClient(1234);
udp.BeginReceive(Receive, null);
}
void Send(string msg, IPEndPoint ipe)
{
UdpClient sC = new UdpClient(0);
byte[] m = Encoding.Unicode.GetBytes(msg);
sC.Send(m, msg.Length * sizeof(char), ipe);
Debug.Log("Sending: " + msg);
sC.Close();
}
void Receive(IAsyncResult ar)
{
IPEndPoint ipe = new IPEndPoint(IPAddress.Any, 0);
byte[] data = udp.EndReceive(ar, ref ipe);
string msg = Encoding.Unicode.GetString(data);
Debug.Log("Receiving: " + msg);
udp.BeginReceive(Receive, null);
}
}
At the Send() I use new UDP CLient and close it after every time. Its better, u can send and receive at the same time.

Categories