Windows 10 app C# tcp client - c#

I've created a Arduino + windows 10 app. The microcontroller connects with a Wi-Fi module (ESP8266-ESP01), tcp/ip protocol. The module is the server and app is the client. The module works fine and I tested it on another program ("SocketTest v3.0"). It sends and receives data. But my app can only write data, the socket.InputStream doesn't work. When I read data, it throws an error.
What am I doing wrong?
Maybe there is an example like this program:
.
public sealed partial class MainPage : Page
{
StreamSocket _socket;
HostName _hostName;
DataWriter writer;
public MainPage()
{
this.InitializeComponent();
_socket = new StreamSocket();
}
private async void connectButton_Click(object sender, RoutedEventArgs e)
{
#region _check_parameters
if (String.IsNullOrEmpty(hostName_Box.Text))
{
_statusBar.Text += "[ERROR] Set the Host Name !!!\n";
return;
}
if (String.IsNullOrEmpty(serviceName_Box.Text))
{
_statusBar.Text += "[ERROR] Set the Service Name !!!\n";
return;
}
try
{
_hostName = new HostName(hostName_Box.Text);
}
catch (Exception)
{
_statusBar.Text += "[ERROR] Invalid Host Name !!!\n";
return;
}
#endregion
// If necessary, tweak the socket's control options before carrying out the connect operation.
// Refer to the StreamSocketControl class' MSDN documentation for the full list of control options.
_socket.Control.KeepAlive = false;
try
{
_statusBar.Text += "Connecting...\n";
await _socket.ConnectAsync(_hostName, serviceName_Box.Text);
_statusBar.Text += "Connected\n";
// go to send mode
connectButton.Content = "Send";
connectButton.Click -= connectButton_Click;
connectButton.Click += send_Data;
hostName_Box.PlaceholderText = "data to send";
hostName_Box.Text = "";
writer = new DataWriter(_socket.OutputStream);
}
catch(Exception exception)
{
// If this is an unknown status it means that the error is fatal and retry will likely fail.
if (SocketError.GetStatus(exception.HResult) == SocketErrorStatus.Unknown)
{
throw;
}
_statusBar.Text += "[ERROR] Connect failed with error:\n" + exception.Message + "\n";
}
}
private async void read_data()
{
DataReader dataReader = new DataReader(_socket.InputStream);
dataReader.InputStreamOptions = InputStreamOptions.Partial;
try
{
await dataReader.LoadAsync(dataReader.UnconsumedBufferLength);
string s;
uint length;
length = dataReader.ReadUInt32();
s = dataReader.ReadString(length);
_statusBar.Text += "Read successful\n" + s + "\n";
}
catch (Exception exception)
{
_statusBar.Text += "[ERROR] Fail to load dada !!!\n" +
exception.Message + "\n";
}
}
private async void send_Data(object sender, RoutedEventArgs e)
{
if (String.IsNullOrEmpty(hostName_Box.Text))
{
_statusBar.Text += "Write anything\n";
return;
}
if(hostName_Box.Text == "read")
{
try
{
hostName_Box.Text = "";
read_data();
return;
}
catch (Exception ex)
{
_statusBar.Text += "[ERROR] Reading data failed:\n" + ex.Message + "\n";
}
}
writer.WriteUInt32(writer.MeasureString(hostName_Box.Text));
writer.WriteString(hostName_Box.Text);
try
{
await writer.StoreAsync();
_statusBar.Text += "\"" + hostName_Box.Text + "\" sent successfully\n";
hostName_Box.Text = "";
}
catch(Exception ex)
{
if (SocketError.GetStatus(ex.HResult) == SocketErrorStatus.Unknown)
{
throw;
}
_statusBar.Text += "[ERROR] Send failed with error:\n" + ex.Message + "\n";
}
}
}
Maybe my reading function is not so good, but the error is that in every situation
dataReader.UnconsumedBufferLength == 0;
and when I use:
int length = dataReader.ReadInt32();
it throws an exception.

The first error is that the dataReader.UnconsumedBufferLength == 0. And the function .LoadAsync(...) throws the error.
DataReader.UnconsumedBufferLength is used to get the size of the buffer that has not been read. You haven't loaded any data from the input stream to the intermediate buffer before you called dataReader.UnconsumedBufferLength, so the value is 0. And the parameter of DataReader.LoadAsync cannot be zero, so you got a System.ArgumentException: The parameter is incorrect.
If I send a string "qwerty" and do dataReader.LoadAsync(6) it throws --> "The operation attempted to acces data outside the valid range"
This exception is thrown by the code s = dataReader.ReadString(length);, you need to call await dataReader.LoadAsync(length); to load the string data into the buffer before you can read it.
Following is the sample code I have verified:
private async void read_data()
{
DataReader dataReader = new DataReader(_socket.InputStream);
dataReader.InputStreamOptions = InputStreamOptions.Partial;
try
{
//load some data to the buffer first
await dataReader.LoadAsync(4);
//read int data
uint length = dataReader.ReadUInt32();
//load data to the buffer before reading it.
uint acturalStringLength = await dataReader.LoadAsync(length);
//read string data
string s = dataReader.ReadString(acturalStringLength);
_statusBar.Text += "Read successful\n" + s + "\n";
}
catch (Exception exception)
{
_statusBar.Text += "[ERROR] Fail to load dada !!!\n" +
exception.Message + "\n";
}
}

Related

TcpClient hangs

I am working on a simple Client to send data to a server at my office. I tested the code locally on my computer using a server called TCPServer, I connect, send data, receive reply, disconnect, send again, rinse and repeat, it all works perfectly, but when I connect to office and do the same thing it connects fine, I can connect/disconnect forever, but when I send data it just hangs. Nothing is received in the log at the office. I can't send a single byte there.
Seems like a firewall issue doesn't it.
But I can run an older program I wrote years ago in Delphi (pascal), and it connects and sends the same data over without issue, same port, everything, so the problem is not a firewall issue. Thoughts on this? Here is the basic code layout.
Connect Button
Disconnect Button
Send Button
At the top of the class I declare the TcpClient Variable
public TcpClient m_client = new TcpClient();
In the _Click for Connect Button and Disconnect Button I have code to connect to server and set some indicators etc
private void ConnectButton_Click(object sender, EventArgs e)
{
string address = FixIP(IPAddressMaskedTextBox.Text);
int Port = Convert.ToInt32(PortNumberMaskedTextBox.Text);
Control control = (Control)sender;
String name = control.Name;
try
{
switch (name)
{
case ("ConnectButton"):
//Connect to server
connect(address, Port);
if (m_client.Connected)
{
SingleConnectionRichTextBox.Clear();
ConnectedLightButton.BackColor = Color.Lime;
SingleConnectionRichTextBox.Text += "Connected at IP " + address + " and Port " + Port.ToString() + "\r\n";
}
break;
case ("DisconnectButton"):
if (m_client.Connected)
{
SingleConnectionRichTextBox.Text += "Connection Terminated\r\n";
ConnectedLightButton.BackColor = Color.Red;
m_client.Client.Disconnect(false);
m_client = new TcpClient();
}
break;
}
}
catch (Exception err)
{
MessageBox.Show(err.ToString());
}
}
public void connect(string address, int port)
{
try
{
if (!m_client.Connected)
{
ConnectedLightButton.BackColor = Color.Yellow;
SingleConnectionRichTextBox.Text += "Attempting to Connect...\r\n";
m_client.Connect(address, port);
}
else
{
SingleConnectionRichTextBox.Text += "Connection Failed...\r\n";
ConnectedLightButton.BackColor = Color.Red;
throw new Exception("Connect: Already connected\r\n");
}
}
catch (Exception err)
{
MessageBox.Show(err.ToString());
}
}
The Send button has it's own event, mostly because when connecting to office it can take a minute for sockets to be created etc.
private void SendButton_Click(object sender, EventArgs e)
{
try
{
if (m_client.Connected)
{
string completeString = "";
for (int cnt = 0; cnt < SingleSendRichTextBox.Lines.Count() - 1; cnt++)
{
string aLine = Regex.Replace(SingleSendRichTextBox.Lines[cnt], #"\e\[(\d+;)*(\d+)?[ABCDHJKfmsu]", "");
if (cnt == 0)
{
//First line gets Start Block, plus a CR on end
aLine = (char)0x0B + aLine + (char)0x0D;
}
else if (cnt == (SingleSendRichTextBox.Lines.Count() -1))
{
//Last line gets CR + End Block + CR on end
aLine += (char)0x0D + (char)0x1C + (char)0x0D;
}
else
{
//All middle lines get CR on end
aLine += (char)0x0D;
}
//MessageBox.Show("Sending line# " + cnt.ToString() + " = " + aLine);
completeString += aLine;
}
Byte[] data = Encoding.ASCII.GetBytes(completeString);
WriteBytes(data);
ReadAllBytes();
}
else
{
MessageBox.Show("Nothing is connected currently...");
}
}
catch (Exception err)
{
MessageBox.Show(err.ToString());
}
}
public void WriteBytes(Byte[] data)
{
try
{
if ((m_client.Connected)&&(data.Count() > 0))
{
// Get access to network stream
NetworkStream stm = m_client.GetStream();
stm.Write(data, 0, data.Length);
stm.Flush();
//MessageBox.Show("Data Sent, length = " + data.Length.ToString());
}
else
{
MessageBox.Show("Either we are not connected, or there is no data to send!!");
}
}
catch (Exception err)
{
MessageBox.Show(err.ToString());
}
}
public void ReadAllBytes()
{
try
{
// Buffer to store the response bytes.
Byte[] readdata = new Byte[256];
// String to store the response ASCII representation.
String responseData = String.Empty;
NetworkStream stm = m_client.GetStream();
// Read the first batch of the TcpServer response bytes.
Int32 bytes = stm.Read(readdata, 0, readdata.Length);
responseData = Encoding.ASCII.GetString(readdata, 0, bytes);
SingleReplyRichTextBox.Text += responseData + "\r\n";
}
catch (Exception err)
{
MessageBox.Show(err.ToString());
}
}
Does anything in here raise a red flag that is obvious? I tried Stream VS NetworkStream. I tried turning off the Reply listener. I took this code and combined it all into one function, and moved the TcpClient creation over to a different Class as static so I could create it in each function, but while all of these worked fine locally, nothing works connecting to office. It won't send a single byte. I set m_client as static at the top too, works fine locally, not to office.
Is GetStream failing maybe? or its sending the data on a different socket?
use a Task and cancellation token to cancel the task. Don't init a "new" TCPclient. In the background the TCPclient will be not closed (socket-timeout of .net)
Use try catches to see the exception and add the log to the conversation to make it more clear pls

How to fix VS2013: Error: type or namespace 'Windows' could not be found?

I want to create a service for Wifi Direct. If I try to add Reference, I don't see core->windows option in VS2013. I have updated the winSDK.
How do I add the Windows.Devices.WifiDirect api ?
you can use
public sealed class WiFiDirectDevice : IDisposable
this is a sample code to handle connections
Windows.Devices.WiFiDirect.WiFiDirectDevice wfdDevice;
private async System.Threading.Tasks.Task<String> Connect(string deviceId)
{
string result = "";
try
{
// No device Id specified.
if (String.IsNullOrEmpty(deviceId)) { return "Please specify a Wi- Fi Direct device Id."; }
// Connect to the selected Wi-Fi Direct device.
wfdDevice = await Windows.Devices.WiFiDirect.WiFiDirectDevice.FromIdAsync(deviceId);
if (wfdDevice == null)
{
result = "Connection to " + deviceId + " failed.";
}
// Register for connection status change notification.
wfdDevice.ConnectionStatusChanged += new TypedEventHandler<Windows.Devices.WiFiDirect.WiFiDirectDevice, object>(OnConnectionChanged);
// Get the EndpointPair information.
var EndpointPairCollection = wfdDevice.GetConnectionEndpointPairs();
if (EndpointPairCollection.Count > 0)
{
var endpointPair = EndpointPairCollection[0];
result = "Local IP address " + endpointPair.LocalHostName.ToString() +
" connected to remote IP address " + endpointPair.RemoteHostName.ToString();
}
else
{
result = "Connection to " + deviceId + " failed.";
}
}
catch (Exception err)
{
// Handle error.
result = "Error occurred: " + err.Message;
}
return result;
}
private void OnConnectionChanged(object sender, object arg)
{
Windows.Devices.WiFiDirect.WiFiDirectConnectionStatus status =
(Windows.Devices.WiFiDirect.WiFiDirectConnectionStatus)arg;
if (status == Windows.Devices.WiFiDirect.WiFiDirectConnectionStatus.Connected)
{
// Connection successful.
}
else
{
// Disconnected.
Disconnect();
}
}
private void Disconnect()
{
if (wfdDevice != null)
{
wfdDevice.Dispose();
}
}

C# Serial Port communication issue

I have a problem with a small C# application.
The application has to connect through a serial port to a bar code scanner which reads a Data Matrix code. The Data Matrix code represents an array of bytes which is a zip archive. I read a lot about the way SerialPort.DataReceived work but I can't find an elegant solution to my problem. And the application should work with different bar code scanners so i can't make it scanner specific. Here is some of my code:
using System;
using System.IO;
using System.IO.Ports;
using System.Windows.Forms;
using Ionic.Zip;
namespace SIUI_PE
{
public partial class Form1 : Form
{
SerialPort _serialPort;
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
try
{
_serialPort = new SerialPort("COM1", 9600, Parity.None, 8, StopBits.One);
}
catch (Exception ex)
{
MessageBox.Show("Error:" + ex.ToString());
return;
}
_serialPort.Handshake = Handshake.None;
_serialPort.ReadBufferSize = 10000;
_serialPort.DataReceived += new SerialDataReceivedEventHandler(comPort_DataReceived);
_serialPort.Open();
}
void comPort_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
byte[] data = new byte[10000];
_serialPort.Read(data, 0, 10000);
File.WriteAllBytes(Directory.GetCurrentDirectory() + "/temp/fis.zip", data);
try
{
using (ZipFile zip = ZipFile.Read(Directory.GetCurrentDirectory() + "/temp/fis.zip"))
{
foreach (ZipEntry ZE in zip)
{
ZE.Extract(Directory.GetCurrentDirectory() + "/temp");
}
}
File.Delete(Directory.GetCurrentDirectory() + "/temp/fis.zip");
}
catch (Exception ex1)
{
MessageBox.Show("Corrupt Archive: " + ex1.ToString());
}
}
}
}
So my question is: How can I know that I read all the bytes the scanner sent?
The code I've got for reading barcode data, which has been working flawlessly in production for several years looks like this:
Note, my app has to read standard UPC barcodes as well as GS1 DataBar, so there's a bit of code you may not need...
The key line in this is:
string ScanData = ScannerPort.ReadExisting();
which is found in the DoScan section, and simply reads the scan data as a string. It bypasses the need to know how many bytes are sent, and makes the rest of the code easier to deal with.
// This snippet is in the Form_Load event, and it initializes teh scanner
InitializeScanner();
ScannerPort.ReadExisting();
System.Threading.Thread.Sleep(1000);
// ens snippet from Form_Load.
this.ScannerPort.DataReceived += new SerialDataReceivedEventHandler(ScannerPort_DataReceived);
delegate void DoScanCallback(); // used for updating the form UI
void DoScan()
{
if (this.txtCouponCount.InvokeRequired)
{
DoScanCallback d = new DoScanCallback(DoScan);
this.Invoke(d);
return;
}
System.Threading.Thread.Sleep(100);
string ScanData = ScannerPort.ReadExisting();
if (isInScanMode)
{
try
{
HandleScanData(ScanData);
}
catch (Exception ex)
{
System.Media.SystemSounds.Beep.Play();
MessageBox.Show("Invalid Scan");
}
}
}
void ScannerPort_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
{
// this call to sleep allows the scanner to receive the entire scan.
// without this sleep, we've found that we get only a partial scan.
try
{
DoScan();
}
catch (Exception ex)
{
System.Media.SystemSounds.Beep.Play();
MessageBox.Show("Unable to handle scan event in ScannerPort_DataReceived." + System.Environment.NewLine + ex.ToString());
}
}
void Port_ErrorReceived(object sender, System.IO.Ports.SerialErrorReceivedEventArgs e)
{
System.Media.SystemSounds.Beep.Play();
MessageBox.Show(e.EventType.ToString());
}
private void HandleScanData(string ScanData)
{
//MessageBox.Show(ScanData + System.Environment.NewLine + ScanData.Length.ToString());
//Determine which type of barcode has been scanned, and handle appropriately.
if (ScanData.StartsWith("A") && ScanData.Length == 14)
{
try
{
ProcessUpcCoupon(ScanData);
}
catch (Exception ex)
{
System.Media.SystemSounds.Beep.Play();
MessageBox.Show("Unable to process UPC coupon data" + System.Environment.NewLine + ex.ToString());
}
}
else if (ScanData.StartsWith("8110"))
{
try
{
ProcessDataBarCoupon(ScanData);
}
catch (Exception ex)
{
System.Media.SystemSounds.Beep.Play();
MessageBox.Show("Unable to process DataBar coupon data" + System.Environment.NewLine + ex.ToString());
}
}
else
{
System.Media.SystemSounds.Beep.Play();
MessageBox.Show("Invalid Scan" + System.Environment.NewLine + ScanData);
}
}
private void InitializeScanner()
{
try
{
ScannerPort.PortName = Properties.Settings.Default.ScannerPort;
ScannerPort.ReadBufferSize = Properties.Settings.Default.ScannerReadBufferSize;
ScannerPort.Open();
ScannerPort.BaudRate = Properties.Settings.Default.ScannerBaudRate;
ScannerPort.DataBits = Properties.Settings.Default.ScannerDataBit;
ScannerPort.StopBits = Properties.Settings.Default.ScannerStopBit;
ScannerPort.Parity = Properties.Settings.Default.ScannerParity;
ScannerPort.ReadTimeout = Properties.Settings.Default.ScannerReadTimeout;
ScannerPort.DtrEnable = Properties.Settings.Default.ScannerDtrEnable;
ScannerPort.RtsEnable = Properties.Settings.Default.ScannerRtsEnable;
}
catch (Exception ex)
{
MessageBox.Show("Unable to initialize scanner. The error message received will be shown next. You should close this program and try again. If the problem persists, please contact support.", "Error initializing scanner");
MessageBox.Show(ex.Message);
Application.Exit();
}
}
As stated in the doc for SerialPort.DataReceived, "Use the BytesToRead property to determine how much data is left to be read in the buffer."
here is the doc for SerialPort.BytesToRead
http://msdn.microsoft.com/en-us/library/system.io.ports.serialport.bytestoread.aspx

Instead of scanning multiple ports I'm scanning only one and when port is closed my app closes

I'm trying to scan multiple ports at once using asynchronymous scanning. The problem is that I can only display the first working port and then waiting like 20 seconds my app is closing with out telling me that the port is closed.
What could be wrong with this code?
private void btnStart_Click(object sender, EventArgs e)
{
for (int port = 80; port < 100; port++)
{
ScanPort(port);
}
}
private void ScanPort(int port)
{
var client = new TcpClient();
try
{
client.BeginConnect(IPAddress.Parse("74.125.226.84"), port, new AsyncCallback(CallBack), client);
}
catch (SocketException)
{
client.Close();
}
}
private void CallBack(IAsyncResult result)
{
var client = (TcpClient)result.AsyncState;
client.EndConnect(result);
if (client.Connected)
{
this.Invoke((MethodInvoker)delegate
{
txtDisplay.Text += "open2" + Environment.NewLine;
});
}
else
{
this.Invoke((MethodInvoker)delegate
{
txtDisplay.Text += "closed2" + Environment.NewLine;
});
}
}
In your callback method, I would make sure close the connection and dispose of the TcpClient. Also TcpClient.EndConnect(IAsyncResult) can also throw exceptions. I also do not see where capturing the port number for display to the user. I would write the callback something like this.
Edit: I didn't actually compile or execute my code (sorry). I also found this other article that shows how to create a port scanner in C#, http://www.dijksterhuis.org/building-a-simple-portscanner-in-c/ There is a comment in this post stating,
There is a gotcha here : The .NET implementation of TCPClient.Close() function does not actually close the connection properly. So we need to do the additional steps of obtaining the stream representing the connection and closing this as well before calling TCPClient.Close.
private void CallBack(IAsyncResult result)
{
var client = (TcpClient)result.AsyncState;
bool connected = false;
try
{
client.EndConnect(result);
connected = client.Connected;
}
catch (SocketException)
{
}
catch (ObjectDisposedException)
{
}
finally
{
if (client.Connected)
{
client.Close();
}
client.Dispose();
}
if (connected)
{
this.Invoke((MethodInvoker)delegate
{
txtDisplay.Text += "open2" + Environment.NewLine;
});
}
else
{
this.Invoke((MethodInvoker)delegate
{
txtDisplay.Text += "closed2" + Environment.NewLine;
});
}
}

telnet connection to a c# socket doesn't close

I have a c# socket based server which serves TCP clients.
I use telnet based monitoring to see if the server is listening.
to versify clients are up i use server keep alive (sending a random string data), and if socket expection raises-i remove the client from a client's dictionary.
the probelm is: the telnet connections to the socket doesn't get removed from the list of connections and the number of clients is rising up slowly but surely.
i tried a telnet manually to the server through command line and than closing the telnet connection-nothing.the server just keep sending the keep alive to the telnet connection with no execptions.
this is my read call back:
protected void ReadCallback(IAsyncResult ar)
{
String content = String.Empty;
// Retrieve the state object and the handler socket
// from the async state object.
StateObject state = (StateObject)ar.AsyncState;
Socket handler = state.socket;
if (state.socket == null)
return;
if (!state.socket.Connected)
return;
int id = state.id;
try
{
// Read data from the client socket.
int bytesRead = handler.EndReceive(ar);
if (bytesRead > 0)
{
string newAddedStr = string.Empty;
newAddedStr = Encoding.UTF8.GetString(state.buffer, 0, bytesRead);
//cut the new message and add it
if (state.sb == null)
state.sb = new StringBuilder();
state.sb.Append(newAddedStr);
// There might be more data, so store the data received so far.
//add data until end of XML
content = state.sb.ToString();
//IF GOT FULL MESSAGE FROM SOCKET
if ((content.Length > 0) /*&& (content.IndexOf("\0") > -1)*/)
{
String strh = String.Format("Client # {0} data: ", id);
strh += content.Replace("\0", "");
if (!strh.Contains("keepalive"))
LogWriter.Trace(strh, "");
// l(writeToGetTextBoxMsg), new object[] { strh });
if (state != null)
{
if (state.sb == null)
state.sb = new StringBuilder();
state.sb.Length = 0;
}
//add the rest of the xml
string objData = content.Replace("\0", "");
string xmlData = objData.ToString();
try
{
if (xmlData.Contains("Unsubscribe"))
{
RemoveSubscriber(xmlData);
}
else
{
if (xmlData.Contains("Subscribe"))
{
if (!state.IsInitiated)
{
state.Init();
state.socketClient.OnSocketError += new SocketErrorHandler(socketClient_OnSocketError);
state.socketClient.clientSocket = handler;
}
AddSubscriber(xmlData, state);
}
}
xmlData = null;
objData = null;
content = null;
}
catch (ArgumentOutOfRangeException ex)
{
LogWriter.Trace(newAddedStr,"ArgumentOutOfRangeException in ReadCallback");
}
catch (Exception ex)
{
LogWriter.TraceError(ex.Message + " " + ex.StackTrace + " " + newAddedStr);
}
#region oldCode
#endregion
}
handler.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0, new AsyncCallback(this.ReadCallback), state);
}
}
catch (System.Net.Sockets.SocketException es)
{
closeSocket(state, false, "Execption:" + es.Message + "," + es.StackTrace);
if (es.ErrorCode != 64)
{
LogWriter.Trace(string.Format("Socket Exception: {0}, {1}.", es.ErrorCode, es.ToString()), "");
}
}
catch (Exception e)
{
closeSocket(state, false,"Execption:"+e.Message+","+e.StackTrace);
if (e.GetType().FullName != "System.ObjectDisposedException")
{
Console.WriteLine("Exception: " + e.StackTrace);
LogWriter.Trace("Exception Message: " + e.ToString() + e.StackTrace, "");
Console.WriteLine("Exception Message: " + e.ToString());
LogWriter.Trace("ReadCallback:" + e.Message + " " + e.StackTrace, "ERROR");
}
}
}
any ideas?
When you do a read of a socket and 0 is returned you know that the other side of the connection has closed. Are you doing that?

Categories