Welcome guest. Before posting on our computer help forum, you must register. Click here it's easy and free.

Author Topic: problem with simple chat program programmed with visual basic  (Read 7005 times)

0 Members and 1 Guest are viewing this topic.

stuartray

  • Guest
 :(Hi, I have an error that keeps coming up when I try to run a simple chat program from the Mastering Visual Basic series. this is the error I get: Run-time error 10054, the connection is reset by remote side. Here is the code that the debugger sends me to:
Private Sub Winsock1_DataArrival(ByVal bytesTotal As Long)
  Dim s As String
  If bytesTotal > 0 Then
    ' Data has arrived. Add it to the label control. Maintain
    ' only the last 2KB of text in the label control.
    Winsock1.GetData s, vbString
    lblReceivedData = s & vbCrLf & Left$(lblReceivedData, 2048)
  End If
End Sub
The complete program:
Option Explicit

Private Sub cmdBind_Click()
  ' Set the communication properties
  Winsock1.RemoteHost = txtPeerMachine
  Winsock1.RemotePort = txtPeerPort
  Winsock1.Bind txtLocalPort
End Sub

Private Sub cmdSend_Click()
  ' Send the data to the peer machine
  Winsock1.SendData txtSendData
  txtSendData = ""
End Sub

Private Sub txtLocalPort_Change()
  EnableBinding
End Sub

Private Sub txtPeerMachine_Change()
  EnableBinding
End Sub

Private Sub txtPeerPort_Change()
  EnableBinding
End Sub

Private Sub Winsock1_DataArrival(ByVal bytesTotal As Long)
  Dim s As String
  If bytesTotal > 0 Then
    ' Data has arrived. Add it to the label control. Maintain
    ' only the last 2KB of text in the label control.
    Winsock1.GetData s, vbString
    lblReceivedData = s & vbCrLf & Left$(lblReceivedData, 2048)
  End If
End Sub

Private Sub Winsock1_Error(ByVal Number As Integer, Description As String, ByVal Scode As Long, ByVal Source As String, ByVal HelpFile As String, ByVal HelpContext As Long, CancelDisplay As Boolean)
  MsgBox "A Winsock Error has occurred. " & vbCrLf & _
    "Error No. " & Number & " " & Description
End Sub

Private Sub EnableBinding()
  ' Enable the Bind button only if the peer machine,
  ' peer port, and local port have been specified.
  cmdBind.Enabled = Len(txtPeerMachine) > 0 And _
    Len(txtPeerPort) > 0 And Len(txtLocalPort) > 0
End Sub

The interface is a single from with text boxes for remote host, remote port,  local port and send data. Data is received in label. It has a bind and send buttons.
Thanks for any suggestions,
Stuart

contrex

  • Guest
Re: problem with simple chat program programmed with visual basic
« Reply #1 on: May 20, 2007, 05:40:51 AM »
This may help

http://support.microsoft.com/kb/260018

Quote
SYMPTOMS
A run-time error occurs when you use network communications on a Windows 2000-based computer with a Winsock Control that uses User Datagram Protocol (UDP), and the Protocol property of the control is set as sckUDPProtocol.

If a SendData request is sent to a RemoteHost that is not listening on the RemotePort, the DataArrival event fires and indicates that 1 byte of data is available. When you call the GetData method to retrieve that data, the following Microsoft Visual Basic run-time error occurs:
'10054' - "The connection is reset by remote side".
Normally, you can capture the error by using the Error event of the Winsock Control. However, even when the Error event is present for the control, the Error event never fires and a run-time error message box displays.

Code: [Select]
RESOLUTION
To work around this problem, use the Microsoft Visual Basic Standard Error Handling mechanism to catch the error. This workaround is illustrated in the following code snippet:

Private Sub Winsock1_DataArrival(ByVal bytesTotal As Long)
   Dim szData As String

   On Error Resume Next
   Winsock1.GetData szData
   If Err Then
      ' handle the error here
   End if
End Sub
            



stuartray

  • Guest
Re: problem with simple chat program programmed with visual basic
« Reply #2 on: May 21, 2007, 08:42:04 AM »
 :)Thanks for your help. I'll try that.