基于Linux的客户端使用基于C和Windows的服务器使用C#(Windows窗体应用程序)使用TCP / IP协议

问题描述:

大家好,

我正在使用TCP / IP协议在C#中开发用户界面。

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

基于Linux的客户端将数据发送到基于Windows的服务器。 我面临的问题是我将用户界面程序连接到基于Linux的客户端。但是一段时间后我收到错误"输入字符串的格式不正确
格式
"。 

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". 

数据流我发送的是这种格式:  point1< tab> point2。

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

当我的用户界面服务器程序工作正常时,客户端也是基于Windows的C. 

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.

C#中的服务器程序窗体应用程序:

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("127.0.0.1"); 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; //缩放图像上的点位置
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;
}
}
}

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

 

SN25

当您发布错误时,如果您能提供整个错误,那将非常有用信息包括消息,行号和调用堆栈。没有它,我们猜测问题出在哪里。鉴于您的代码我假设它是Convert.ToDouble调用。

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.

您尝试使用\t拆分部件,但\ t可能与Linux客户端发送的内容不匹配。在Split调用上设置断点并验证它是否正确地拆分数据。如果不是那么你需要扩展你的拆分调用以包含Linux发送的char(s)
然后拆分所有这些。

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).