473,407 Members | 2,629 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,407 software developers and data experts.

Telnet Client, Sockets trouble!

17
Hello Everyone!

I have so nearly finished a mud client for a customer to the point where I was about to send them the source code. Out of luck I took the project home to try it on my personal PC just to make sure.

At work on my corporate network everything works perfectly but it seems once anyone uses the client on a direct high speed connection to the internet the incoming text seems to all get jumbled up!

This is using system.net.sockets namespace i've left out a fair bit of code or this post will probably be too long.

My thinking is that on faster connections the dataarrival event is fired off BEFORE the current text has been displayed leading to the text getting mixed up?

If anyone can offer some suggestions here it would be massively appreciated as once this issue is resolved the project works perfectly, its also intended that the client will be provided free of charge to everyone so by helping me your helping people get free software!

Heres the source below...
Expand|Select|Wrap|Line Numbers
  1. Dim evtDataArrival As New AsyncCallback(AddressOf  Datarrive)
  2. Public tcpClient As New System.Net.Sockets.TcpClient()
  3. Dim buffer(1024) As Byte
  4. Dim stream As NetworkStream
  5. stream = tcpClient.GetStream
  6.  
  7.  
  8.  
  9. Private Function dataArrive(ByVal dr As IAsyncResult)
  10.         Dim textin As String
  11.  
  12.         numberOfBytes = stream.EndRead(dr)
  13.         ' At this instant DataBuffer variable is filled with the received data
  14.         stream.Flush()
  15.         textin = textin & Encoding.ASCII.GetString(buffer, 0, numberOfBytes)
  16.  
  17.         While stream.DataAvailable
  18.             stream.BeginRead(buffer, 0, 1024, New AsyncCallback(AddressOf dataArrive), stream)
  19.         End While
  20.  
  21.         If Not stream.DataAvailable Or Asc(Strings.Right(textin, 1)) = Val(Chr(32)) Then
  22.             param = textin
  23.             Settext(textin)
  24.             words.Add(textin)
  25.             param = ""
  26.  
  27.             If checktrig.IsAlive = False Then
  28.                 checktrig.Interrupt()
  29.             End If
  30.  
  31.             stream.BeginRead(buffer, 0, 1024, evtDataArrival, Nothing)
  32.  
  33.         Else
  34.             stream.BeginRead(buffer, 0, 1024, evtDataArrival, Nothing)
  35.         End If
  36.  
  37.     End Function
  38.  
  39.  Private Function Settext(ByVal param As String) As String
  40.         Dim networkStream As NetworkStream = tcpClient.GetStream()
  41.         Dim pos As Integer
  42.         Dim pos2
  43.         Dim esccode As String
  44.         Dim i As Integer
  45.         Dim parse As String
  46.         Dim myregex As New Regex("(?s)(?:\e\[(?:(\d+);?)*([A-Za-z])(.*?))")
  47.         Dim myresults As MatchCollection
  48.         '(?=\e\[|\z)
  49.  
  50.         If Me.RichTextBox1.InvokeRequired Then
  51.             Dim d As New Stt(AddressOf Settext)
  52.             RichTextBox1.Invoke(d, param)
  53.         Else
  54.             i = 0
  55.             pos = 1
  56.             myresults = myregex.Matches(Trim(param))
  57.  
  58.             If myresults.Count > 0 Then
  59.                 Do Until pos = param.Length
  60.  
  61.                     If i <> myresults.Count Then
  62.                         esccode = myresults(i).Value
  63.                         parse = Strings.Mid(param, pos, IIf(myresults(i).Index <= pos, (myresults(i).Index - (pos - 1)), (myresults(i).Index - pos)) + 1)
  64.                     Else
  65.                         parse = Strings.Mid(param, pos, param.Length)
  66.                         pos = param.Length
  67.  
  68.                     End If
  69.  
  70.                     If Not Strings.Left(parse, 1) = Chr(27) Then
  71.                         Me.RichTextBox1.SelectionColor = vcolour
  72.                         RichTextBox1.AppendText(parse)
  73.                         SendMessage(RichTextBox1.Handle, WM_VSCROLL, SB_BOTTOM, 0)
  74.                     End If
  75.  
  76.                     If esccode <> "" Then
  77.                         Select Case esccode
  78.                             Case Chr(27) & "[1m"
  79.                                 RichTextBox1.SelectionFont = New Font(RichTextBox1.Font.ToString, RichTextBox1.Font.Size, FontStyle.Bold)
  80.                                 Exit Select
  81.                             Case Chr(27) & "[0m"
  82.                                 RichTextBox1.SelectionFont = New Font(RichTextBox1.Font.ToString, RichTextBox1.Font.Size, FontStyle.Regular)
  83.                                 vcolour = Color.Lime
  84.                                 Exit Select
  85.                             Case Chr(27) & "[33m"
  86.                                 vcolour = Color.Yellow
  87.                                 Exit Select
  88.                             Case Chr(27) & "[35m"
  89.                                 vcolour = Color.Magenta
  90.                                 Exit Select
  91.                             Case Chr(27) & "[36m"
  92.                                 vcolour = Color.Cyan
  93.                                 Exit Select
  94.                         End Select
  95.                     End If
  96.  
  97.                     If i <> myresults.Count Then
  98.                         pos = (myresults(i).Index + Len(esccode)) + 1
  99.                         esccode = ""
  100.                         i = i + 1
  101.                     End If
  102.                 Loop
  103.             Else
  104.                 Me.RichTextBox1.SelectionColor = vcolour
  105.                 RichTextBox1.AppendText(param)
  106.                 SendMessage(RichTextBox1.Handle, WM_VSCROLL, SB_BOTTOM, 0)
  107.             End If
  108.         End If
  109.     End Function
  110.  
Jul 11 '08 #1
2 1709
marcf
17
As an update i've confirmed its he event firing multiple times while its still trying to display, any ideas on how to make the second firing of the event wait until all previous ones have finished?
Jul 11 '08 #2
Plater
7,872 Expert 4TB
Perhaps buffer a collection of bytes before displaying them.
Like either wait till X amount of bytes have come in or 0.5seconds have passed with no data arrival?

I would also recomend cleaning up your parsing code. It's almost all plain VB and not VB.NET, it might make a difference.
Jul 11 '08 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

6
by: Richard Bird CCNP, CCDP, MCSE, etc. | last post by:
I need some help adding multithreading to an existing python script. The script was developed to telnet and make changes on a list of cisco routers. I figure that by adding multithreading, I'd be...
0
by: Fu Chen | last post by:
Hi pals! One of my client have a old UNIX application (but very important), it use telnet to operate. I am designing a web interface to this client. one of the work is building the UNIX...
5
by: Greg Martz | last post by:
I'd like to do the following in C# and prefer using tcpclient rather than raw sockets... Connect to a unix box Login run date +%H%M%S retrieve the response. That's it, nothing more. This...
7
by: Rex Winn | last post by:
I've Googled until my eyes hurt looking for a way to issue Telnet commands from C# and cannot find anything but $300 libraries that encapsulate it for you. I don't want to be able to create a...
2
by: b00x | last post by:
Hi, I'm trying to develop a script that I can reuse to run remote commands on multiple UNIX servers via telnet. I've tried various php scripts after googling for a good 5 or so hours, but found...
3
by: jtagpgmr | last post by:
I need help coming up with a way to create a c# program to help me Telnet to a hostIP.. Any suggestions.. or a way to start..
0
by: goroth | last post by:
I am trying to create a telnet client that will connect to several of my network devices on campus and change settings on the devices. So far I can connect with the code below, but I can't seem to...
3
by: =?Utf-8?B?cGlja2VkYW5hbWU=?= | last post by:
Hi, I am trying to establish a telnet session to do screen scraping. This device returns some custom escape sequences, which I can ignore, and some standard escape seq's. At your suggestion, I...
6
by: =?Utf-8?B?U2hhcmllZg==?= | last post by:
Dear All, I must write a client program in C# which will communicate with a switch throught telnet. When I create a socket connection on port 22, the switch responds with some text and at the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.