Question:
What's wrong with my chat program code(Visual Basic)?
Demetris
2009-06-11 00:27:41 UTC
I'm trying to write a chat program in visual basic 2008 using winsock. When ever i use this it always gets stuck at Winsock.GetData(messagein) and does nothing. Could somebody tell me where my code has errors please.
Localhost is set to 255.255.255.255
local port and remote port are the same
and protocal is set to 1
Any help would be appreciated.
Thanks in advance.

Public Class Form1
Dim messagein As String
Dim messageout As String
Private Sub sendButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles sendButton.Click
messageout = sendTextBox.Text
Winsock.SendData(messageout)
End Sub

Private Sub Winsock_DataArrival(ByVal sender As Object, ByVal e As AxMSWinsockLib.DMSWinsockControlEvents_DataArrivalEvent) Handles Winsock.DataArrival
Winsock.GetData(messagein)
getTextBox.Text = messagein
End Sub

End Class
Three answers:
anonymous
2009-06-11 00:34:42 UTC
Hope this code helps you.



Winsock Component - Using with VB.NET







Retrieving web page source, using OstroSoft Winsock Component (oswinsck.dll)

Download project source code



Minimum requirements: Visual Basic.NET, oswinsck.dll*

* If you don't have OstroSoft Winsock Component, see installation instructions



1. In Visual Studio.NET create new Visual Basic Windows Application.

2. Click on Project menu and select "Add Reference".

3. In "Add Reference" dialog box click on COM tab and select OSWinsck. Click OK to save a new reference.

4. Enter the following code:

Public Class frmMain

Inherits System.Windows.Forms.Form



Dim sPage As String

Dim WithEvents wsTCP As New OSWINSCK.Winsock



#Region " Windows Form Designer generated code "



Private Sub cmdView_Click(ByVal sender As System.Object, _

ByVal e As System.EventArgs) Handles cmdView.Click

On Error GoTo ErrHandler

Dim sServer As String

Dim nPort As Long



nPort = 80

sServer = Trim(txtURL.Text)

If InStr(sServer, "://") > 0 Then _

sServer = Mid(sServer, InStr(sServer, "://") + 3)

If InStr(sServer, "/") > 0 Then

sPage = Mid(sServer, InStr(sServer, "/") + 1)

sServer = Strings.Left(sServer, InStr(sServer, "/") - 1)

End If

If InStr(sServer, ":") > 0 Then

nPort = Mid(sServer, InStr(sServer, ":") + 1)

sServer = Strings.Left(sServer, InStr(sServer, ":") - 1)

End If

If sServer = "" Then Err.Raise(12001, , "Invalid URL")



wsTCP.Connect(sServer, CInt(nPort))

Exit Sub



ErrHandler:

MsgBox("Error " & Err.Number & ": " & Err.Description)

End Sub



Private Sub wsTCP_OnClose() Handles wsTCP.OnClose

wsTCP.CloseWinsock()

End Sub



Private Sub wsTCP_OnConnect() Handles wsTCP.OnConnect

wsTCP.SendData("GET /" & sPage & " HTTP/1.0" & vbCrLf & vbCrLf)

End Sub



Private Sub wsTCP_OnDataArrival(ByVal bytesTotal As Integer) _

Handles wsTCP.OnDataArrival

Dim sBuffer As String

wsTCP.GetData(sBuffer)

txtSource.Text = txtSource.Text & sBuffer

End Sub



Private Sub wsTCP_OnError(ByVal Number As Short, _

ByRef Description As String, ByVal Scode As Integer, _

ByVal Source As String, ByVal HelpFile As String, _

ByVal HelpContext As Integer, ByRef CancelDisplay As Boolean) _

Handles wsTCP.OnError

MsgBox(Number & ": " & Description)

End Sub



Private Sub wsTCP_OnStatusChanged(ByVal Status As String) _

Handles wsTCP.OnStatusChanged

Console.WriteLine(Status)

End Sub

End Class
Fudge
2009-06-11 00:33:38 UTC
Try using a debugger to step into that line of code and check the variables ...
anonymous
2009-06-11 00:32:17 UTC
"What's wrong with my chat program code(Visual Basic)?"

"Visual Basic" part.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...