|
|
|
![]() |
|
|||||||
|
||||||||
![]() |
|
|
Thread Tools | Rate Thread | Display Modes |
|
|
|
#1
|
||||
|
||||
|
TCP problems in C#
I've got a small LV TCP server program writing a string to the stream and then on the client side (written in C#), when I read the stream every 20ms, it comes back jumbled after the first read.
For instance, if I wrote a string "a b c" to the stream, it would read like something this for the first four iterations (each line is what is read at 20ms): Quote:
Here's the server code, really simple LV server, went off a youtube tutorial ![]() Here's the C# code: Code:
private void timUpdate_Tick(object sender, EventArgs e)
{
//update revolutions X and Y and the Gyro Angle every 20ms from the server string value
//and make sure the connection is solid, otherwise update connection
Byte[] info = new Byte[Client.ReceiveBufferSize];
try
{
int rData = stream.Read(info, 0, System.Convert.ToInt32(Client.ReceiveBufferSize));
RobotData = System.Convert.ToString(rData);
}
catch (System.IO.IOException)
{
lblConnectInd.BackColor = System.Drawing.Color.Red;
Log("The robot ended the stream.");
stream.Close();
Client.Close();
}
//split string into X, Y and angle
strRevX = RobotData.Substring(0, 2);
strRevY = RobotData.Substring(5, 2);
strAngle = RobotData.Substring(8, 2);
//update values recieved in GUI
lblX.Text = strRevX;
lblY.Text = strRevY;
lblAngle.Text = strAngle;
//convert separated strings into doubles
Double.TryParse(strRevX, out dblRevolutionsX);
Double.TryParse(strRevX, out dblRevolutionsY);
Double.TryParse(strAngle, out dblAngle);
//make sure we're still connected
if (Client.Connected == true)
{
lblConnectInd.BackColor = System.Drawing.Color.Green;
}
else
{
lblConnectInd.BackColor = System.Drawing.Color.Red;
timUpdate.Stop();
}
}
|
|
#2
|
|||||
|
|||||
|
Re: TCP problems in C#
I'm having a hard time understanding what you want to do. What you're calling the "server" is apparently sending data continuously, and what you're calling the "client" is apparently reading the accumulated data once every 20 milliseconds. Is that the intent?
Normally, I'd expect the client to send a packet of data either on a fixed schedule or whenever the data is available, and the server would wait for data to appear before reading it and processing it immediately. |
|
#3
|
||||||
|
||||||
|
Re: TCP problems in C#
The first thing you should do is use a program such as wireshark to capture the TCP/IP packets and determine if they are being sent as discrete packets. TCP is a stream protocol and guarantees that things will be received in the correct order, but they may be packetized differently then you expect. You can learn more by searching for nagle algorithm.
Why are you sending data with no delay in LabVIEW, but reading data every 20ms? You'll probably fill up the TCP/IP buffers fairly quickly. Last edited by Joe Ross : 19-11-2013 at 12:36. |
|
#4
|
||||
|
||||
|
Re: TCP problems in C#
Quote:
Code:
RobotData = System.Text.Encoding.Default.GetString(rData); |
|
#5
|
||||
|
||||
|
Re: TCP problems in C#
Thank you guys for the replies!
I have to admit, I'm just learning TCP so please excuse any noob-ish mistakes I've made:/ Quote:
Quote:
. Would it just be a ms timer in the loop?Quote:
Do you guys think the problem is on the LV side or the C# side? Last edited by Invictus3593 : 20-11-2013 at 09:20. |
|
#6
|
||||
|
||||
|
Re: TCP problems in C#
Did that fix the problem? What's it outputting now? That modification will completely change the results you get
Also, how were you getting "a b c" - the code you had should have only given you numbers since you were just processing the number of bytes read. Were a b c placeholders? It might be helpful if you could give us some concrete examples |
|
#7
|
|||||
|
|||||
|
Re: TCP problems in C#
Quote:
Since your application involves a fixed amount of data for each communication, I don't think the stream nature of TCP is helping you any. I suggest that you should be using UDP instead. That lends itself to a very simple structure where your C code can listen for a UDP packet and process the whole thing at once when it arrives. |
|
#8
|
||||||
|
||||||
|
Re: TCP problems in C#
Quote:
I agree with Alan that UDP is a better choice if all you want to do is blindly read a packet of data. |
|
#9
|
||||
|
||||
|
Re: TCP problems in C#
Quote:
Quote:
Quote:
Sorry for all the questions in a row but I would like to know as much about this as possible so that when try it out, I can troubleshoot. EDIT: One more thought; would using Network Tables help here? Maybe just getting the string from the LV Dashboard? If you guys have any extra tips or resources I can check out about the two different protocols, PM me because I'm still learning! Last edited by Invictus3593 : 20-11-2013 at 14:17. |
![]() |
| Thread Tools | |
| Display Modes | Rate This Thread |
|
|