Linux based Client using C and Windows based server using C#( windows form application) using TCP/IP protocol RRS feed

  • Question

  • Hi Everyone,

    I am developing the user interface in C# which work as a server using TCP/IP protocol.

    The Linux based client sends data to the Windows based server. The Problem which I am facing that one I connect my user interface program to Linux based client. But after some time I received an error "The input string is not in a correct format". 

    the data stream which I am sending is in this format:  point1 <tab> point2.

    As my user interface server program is working fine when the client is also Windows based C. 

    I am sharing the block of my code here. Please Suggest me the solution I have worked a lot on it even searched too but couldn't find the way to solve this issue.

    Server Program in C# Window form Application:

    using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using System.Threading; using System.Net; using System.Net.Sockets; using System.Collections; namespace DEMO_APP { public partial class Form1 : Form { public Form1() { InitializeComponent(); pictureBox1.Image = new Bitmap("HCRC_Map_edited_final2.jpg"); g = pictureBox1.CreateGraphics(); path = Graphics.FromImage(pictureBox1.Image); } Thread listener; Int32 port = 3000; IPAddress ip = IPAddress.Parse(""); ArrayList nSockets; String[] part = null; Double w, x, y, z; private System.Drawing.Graphics g, path; private void Form1_Load(object sender, EventArgs e) { nSockets = new ArrayList(); label1.Text = "IP Address:" + ip; } private void button1_Click(object sender, EventArgs e) { listener = new Thread(listen); listener.Start(); } public void listen()//thread { TcpListener tcpListener = new TcpListener(ip, port); tcpListener.Start(); while (true) { Socket handlerSocket = tcpListener.AcceptSocket(); if (handlerSocket.Connected) { Control.CheckForIllegalCrossThreadCalls = false; label2.Text = "Connected"; lock (this) { nSockets.Add(handlerSocket); } ThreadStart thdstHandler1 = new ThreadStart(handlerThread1); Thread thdHandler1 = new Thread(thdstHandler1); thdHandler1.Start(); } } } public void handlerThread1() { Socket handlerSocket = (Socket)nSockets[nSockets.Count - 1]; NetworkStream networkStream = new NetworkStream(handlerSocket); Byte[] bytes = new Byte[1024]; String data = null; int k;

    lock (this) { while ((k = networkStream.Read(bytes, 0, bytes.Length)) != 0) { data = Encoding.ASCII.GetString(bytes, 0, k); part = data.Split('\t'); var sf = new StringFormat(); sf.Alignment = StringAlignment.Center; sf.LineAlignment = StringAlignment.Far; w = (Convert.ToDouble(part[1]) / 0.0347) + 67; //scaling the point position on image x = (Convert.ToDouble(part[2]) / 0.0335) + 437; textBox1.Text = ("X: " + part[2] + "\t" + "Y: " + part[1]); g.DrawRectangle(new Pen(Color.Blue, 3), Convert.ToInt32(x), Convert.ToInt32(w), 6, 6); g.DrawString("Tag1", SystemFonts.DialogFont, Brushes.Black, Convert.ToInt32(x) + 16 / 2, Convert.ToInt32(w), sf); Thread.Sleep(200); pictureBox1.Refresh(); byte[] msg = System.Text.Encoding.ASCII.GetBytes(data); networkStream.Write(msg, 0, msg.Length); } } handlerSocket = null; } } }

    Please Help



    • Edited by IB1515 Wednesday, December 20, 2017 12:53 AM
    Wednesday, December 20, 2017 12:50 AM

All replies

  • When you post errors it would be really useful if you could provide the entire error information including the message, line number and call stack. Without it we are guessing at where the problem is. Given your code I assume it is the Convert.ToDouble calls.

    You're trying to split the parts using \t but \t may not match what the Linux client is sending. Set a breakpoint on the Split call and verify it is splitting the data correctly. If it isn't then you'll need to expand your split call to include the char(s) that Linux is sending and then split on all of them.

    As additional error checking, after the split, you should confirm that you got both points. If not then that is an error (or you should handle it accordingly).

    Michael Taylor

    Wednesday, December 20, 2017 2:55 PM
  • The problem probably is that you are using the return value of networkStream.Read to determine the length of a null-terminated string. You are probably using an incorrect number of bytes and/or there is not a null to terminate the string. Or something like that related to length and null termination.

    I really doubt that Linux is sending the wrong value for tab; I think you can rely on the tab character to be the correct character.

    If you can debug by stepping through the code then you are likely to find the problem. If you are unable to step through the code then I would add lines to write data to the debug output window or someplace to log debugging information.

    Sam Hobbs

    Wednesday, December 20, 2017 10:05 PM