Results 1 to 6 of 6

Thread: [RESOLVED] Winsock problem with sending data

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jan 2006
    Posts
    108

    [RESOLVED] Winsock problem with sending data

    Hi, I've been browsing these forums (and some others) and I have yet to find a solution to my problem so I decided to sign up and post a thread. I'm really new to VB, been doing this for two days to be honest I'm using Visual Basic 2005 Express Edition to do the program.

    My program is for the moment just a really simple chatprogram (just trying to learn here you know), but in the end it will end up as (I hope) a program to connect to a device using telnet and be able to change and view the settings for the device. (The device currently has to be configure manually through telnet).

    I use "my" program and another one called SimpleChat by Dave Dunghill. The SimpleChat program had to be updated in order for VB to open and run it (dont know if it got messed up somewhere on the way).

    The problem is that neither program is sending or receiving data from the other. Even if I run two copies of the chatprogram by Dave no data is sent (doesn't show up in the textbox anyway). Both programs indicate that they are connected after I tried to connect them.

    I also tried to install the chatprogram by Dave onto my home computer to see if it would work (by using the Publish function and set it to be installed from CD/DVD). It downloaded and installed .NET Frameworks 2 on my computer but the program itself wont start. First time I tried it put itself in the Start-meny, but after several tries it din't even do that anymore.

    I'd be grateful if someone could please help me with this

    This is the program I've put together (copy/paste mostly )

    VB Code:
    1. Option Strict Off
    2. Option Explicit On
    3. Friend Class Form1
    4.     Inherits System.Windows.Forms.Form
    5.  
    6.     Private Sub buttonConnect_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles buttonConnect.Click
    7.  
    8.         ' reset the socket
    9.         Winsock.Close()
    10.  
    11.         ' set the new properties
    12.         Winsock.RemoteHost = "localhost" 'I put localhost here just now instead of my real IP
    13.         Winsock.RemotePort = "1234"
    14.  
    15.         ' initiate the connection
    16.         txtMain.Text = txtMain.Text & vbCrLf & "Connecting to " & comboDSLAM.Text & "..."
    17.         Winsock.Connect()
    18.  
    19.     End Sub
    20.  
    21.     Private Sub cmdSend_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles buttonSend.Click
    22.         ' make sure we're connected
    23.         If Winsock.CtlState = MSWinsockLib.StateConstants.sckConnected Then
    24.             ' send the data
    25.             Winsock.SendData(txtCommand.Text & vbCrLf)
    26.             System.Windows.Forms.Application.DoEvents()
    27.             ' log the data in the main window
    28.             LogData(txtCommand.Text)
    29.             ' clear the text ready for the next command
    30.             txtCommand.Text = ""
    31.         Else
    32.             txtMain.Text = txtMain.Text & vbCrLf & "You're not connected to anything"
    33.         End If
    34.     End Sub
    35.  
    36.     Private Sub Winsock_ConnectEvent(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles Winsock.ConnectEvent
    37.         ' alert the user that we're connected
    38.         txtMain.Text = txtMain.Text & vbCrLf & "Connection established to " & Winsock.RemoteHostIP
    39.     End Sub
    40.  
    41.     Private Sub Winsock_CloseEvent(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles Winsock.CloseEvent
    42.         ' alert the user when disconnected
    43.         txtMain.Text = txtMain.Text & vbCrLf & "The connection to " & comboDSLAM.Text & " (" & Winsock.RemoteHostIP & ") has been terminated"
    44.     End Sub
    45.  
    46.     Private Sub Winsock_DataArrival(ByVal eventSender As System.Object, ByVal eventArgs As AxMSWinsockLib.DMSWinsockControlEvents_DataArrivalEvent) Handles Winsock.DataArrival
    47.         Dim strData As String
    48.         ' get the data
    49.         Winsock.GetData(strData)
    50.         ' display it in the main textbox
    51.         LogData(strData)
    52.  
    53.     End Sub
    54.  
    55.     Private Sub Winsock_Error(ByVal eventSender As System.Object, ByVal eventArgs As AxMSWinsockLib.DMSWinsockControlEvents_ErrorEvent) Handles Winsock.Error
    56.         ' alert the user to the error
    57.         txtMain.Text = txtMain.Text & vbCrLf & "Error: " & eventArgs.description
    58.     End Sub
    59.  
    60.     Private Sub LogData(ByVal strData As String)
    61.         ' add string to the end of the textbox
    62.         txtMain.Text = txtMain.Text & vbCrLf & strData
    63.         ' scroll the textbox down
    64.         txtMain.SelectionStart = Len(txtMain.Text)
    65.     End Sub
    66.  
    67.     Private Sub Form_Unload(ByVal Cancel As Integer)
    68.         Winsock.Close()
    69.     End Sub
    70.  
    71. End Class
    Last edited by Darkstorm; Jan 5th, 2006 at 03:03 AM.

  2. #2

    Thread Starter
    Lively Member
    Join Date
    Jan 2006
    Posts
    108

    Re: Winsock problem with sending data

    I could only post 10000 characters so sorry for the double post

    This is the program I use to test the program I put together. Its a chat program made by Dunghill Dave.

    VB Code:
    1. Option Strict Off
    2. Option Explicit On
    3. Friend Class Form1
    4.     Inherits System.Windows.Forms.Form
    5.     Public Sub about_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles about.Click
    6.         MsgBox("This chat program was written to go with the tutorial at: [url]http://www.vbwm.com/forums/topic.asp?TOPIC_ID=1660[/url] and was written by Dunghill_Dave")
    7.         'as IFontDisp the user could forget my name :-)
    8.     End Sub
    9.    
    10.    
    11.     Private Sub cmdclear_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdclear.Click
    12.         Dim msgresult As String
    13.         msgresult = CStr(MsgBox("Are you sure you want to clear the chat box?", MsgBoxStyle.YesNoCancel))
    14.         If msgresult = CStr(MsgBoxResult.Yes) Then txtmain.Text = "" 'here we can clear the chat box if we have too much, just click it and everything goes!
    15.         'if they click anything else, we carry on...
    16.     End Sub
    17.    
    18.     Private Sub cmdconnect_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdconnect.Click
    19.         If txtIP.Text <> "" Then 'make sure the user has entered someting, IP or hostname
    20.             With Winsock 'what we are talking about
    21.                 .RemoteHost = txtIP.Text 'what we want to connect to
    22.                 .RemotePort = CInt(txtport.Text) 'port we are going to connect to
    23.                 .Connect() 'connect to the server!
    24.             End With 'stop talking about winsock
    25.             Call areconnected() 'make the buttons look right
    26.         Else
    27.             MsgBox("You must enter an IP address!") 'you cant connect without the ip address
    28.         End If
    29.     End Sub
    30.    
    31.     Private Sub cmddisconnect_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmddisconnect.Click
    32.         Winsock.Close() 'close the winsock control
    33.         Call aredisconnected() 'get the buttons looking right
    34.     End Sub
    35.    
    36.     Private Sub cmdhost_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdhost.Click
    37.         On Error Resume Next
    38.         Winsock.LocalPort = CInt(txtport.Text) 'tell the control port to use
    39.         Winsock.Listen() 'listen to the port above
    40.         Call areconnected() 'get the buttons looking right
    41.         MsgBox("Your IP address is: " & Winsock.LocalIP) 'just in case we want to tell the person we want to talk to what to connect to
    42.         txtIP.Text = Winsock.LocalIP 'and stick it in the ip box too
    43.     End Sub
    44.    
    45.     Private Sub areconnected()
    46.         'get the buttons looking right
    47.         cmdhost.Enabled = False 'so we cant host while connected
    48.         cmdconnect.Visible = False 'so we cant try to connect again
    49.         cmdsend.Enabled = True 'make sure we can send data when we're connected
    50.         With cmddisconnect 'we dont really need with here, but
    51.             .Enabled = True 'it saves a lot of typing when you
    52.             .Visible = True 'have a lot of properties to set
    53.         End With 'end it so it doesnt think we are still refering to the button
    54.         'get the text boxes locked so info cant be changed:
    55.         txtport.ReadOnly = True 'lock the port box, this isnt really important as you cant change the port the winsock is using after you are connected anyway,
    56.         txtIP.ReadOnly = True 'lock ip box, (isnt really important, as above
    57.         txtname.ReadOnly = True 'stop people changing their usernames half way through chat, not important now, but if you have more users...
    58.     End Sub
    59.     Private Sub aredisconnected()
    60.         'get the buttons looking right, (opposite to areconnected)
    61.         cmdsend.Enabled = False 'we cant send if we're not connected, so dont even try!
    62.         cmdhost.Enabled = True 'we can host, so show that button
    63.         cmdconnect.Visible = True 'we could connect too!
    64.         With cmddisconnect 'we dont really need with here, but
    65.             .Enabled = False 'it saves a lot of typing when you
    66.             .Visible = False 'have a lot of properties to set
    67.         End With 'end it so it doesnt think we are still refering to the button
    68.        
    69.         'get the text boxes locked so info cant be changed:
    70.         txtport.ReadOnly = False 'lock the port box, this isnt really important as you cant change the port the winsock is using after you are connected anyway,
    71.         txtIP.ReadOnly = False 'lock ip box, (isnt really important, as above
    72.         txtname.ReadOnly = False 'stop people changing their usernames half way through chat, not important now, but if you have more users...
    73.     End Sub
    74.    
    75.    
    76.     Private Sub cmdmore_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdmore.Click
    77.         MsgBox("We will use this button in a later tutorial: sending files")
    78.         'just in case you forget vbwm.com, and me of course!
    79.     End Sub
    80.    
    81.     Private Sub cmdsend_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdsend.Click
    82.         'UPGRADE_NOTE: State was upgraded to CtlState. Click for more: 'ms-help://MS.VSExpressCC.v80/dv_commoner/local/redirect.htm?keyword="A9E4979A-37FA-4718-9994-97DD76ED70A7"'
    83.         If Winsock.CtlState = MSWinsockLib.StateConstants.sckConnected Then 'chack the connection is alive
    84.             Winsock.SendData((txtname).Text & ": " & (txtchat).Text) 'send your chat name, a ":" and the info you typed
    85.             'NOTE: there is a space after the : so that it will appear chatname: typedinfo and not: chatname:typedinfo
    86.             System.Windows.Forms.Application.DoEvents() 'this makes sure all the data gets their
    87.             txtmain.Text = txtmain.Text & vbCrLf & (txtname).Text & ": " & (txtchat).Text 'puts the data you typed into your txtmain chat box too
    88.             txtmain.SelectionStart = Len(txtmain.Text) 'takes you to the end of the chat session so far
    89.             txtchat.Text = "" 'empty the box so you can type some more
    90.         End If
    91.         'If the winsock controll is not alive,
    92.         'nothing will happen when you press the send button
    93.     End Sub
    94.    
    95.     Public Sub elp_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles elp.Click
    96.         MsgBox("The tutorial which acompanies this is at: [url]http://www.vbwm.com/forums/topic.asp?TOPIC_ID=1660[/url] and was written by Dunghill_Dave.  Please post any questions there.")
    97.         'just in case you forget vbwm.com, and me of course!
    98.     End Sub
    99.    
    100.     Public Sub exit_Renamed_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles exit_Renamed.Click
    101.         Me.Close()
    102.         End 'exit the program of course
    103.     End Sub
    104.    
    105.     Private Sub Winsock_ConnectEvent(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles Winsock.ConnectEvent
    106.         MsgBox("Connected to server!") 'just to let you know we are connected
    107.     End Sub
    108.    
    109.     Private Sub Winsock_ConnectionRequest(ByVal eventSender As System.Object, ByVal eventArgs As AxMSWinsockLib.DMSWinsockControlEvents_ConnectionRequestEvent) Handles Winsock.ConnectionRequest
    110.         'this part is only used if serving the chat.
    111.         Winsock.Close() 'close the control as it can only do one thing at a time
    112.         Winsock.Accept(eventArgs.requestID) 'accept the incoming connection
    113.         MsgBox("Connection recieved from: " & Winsock.RemoteHostIP) 'find out who connected (their ip address at least!)
    114.     End Sub
    115.    
    116.     Private Sub Winsock_DataArrival(ByVal eventSender As System.Object, ByVal eventArgs As AxMSWinsockLib.DMSWinsockControlEvents_DataArrivalEvent) Handles Winsock.DataArrival
    117.         Dim strrecieved As String
    118.         Winsock.GetData(strrecieved) 'the incoming data will be set in a variable called strrecieved
    119.         txtmain.Text = (txtmain).Text & vbCrLf & strrecieved 'add the data to the main chat box, the first bit makes sure the old data remains, the next bit vbCrLf put the new data on a new line and the new data is written in the text box
    120.         txtmain.SelectionStart = Len(txtmain.Text) 'takes you to the end of the chat session
    121.     End Sub
    122.    
    123.     Private Sub Winsock_Error(ByVal eventSender As System.Object, ByVal eventArgs As AxMSWinsockLib.DMSWinsockControlEvents_ErrorEvent) Handles Winsock.Error
    124.         'if we get an error, we can get told the problem, and the
    125.         'program will not close itself.
    126.         MsgBox("Error: " & eventArgs.Description & "Connection Closed")
    127.         Winsock.Close() 'close the connection to save confusion (sometimes the errors close the connection, sometimes not, best close them to make sure.
    128.         Call aredisconnected() 'make sure all the buttons are correct
    129.     End Sub
    130. End Class

  3. #3
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: Winsock problem with sending data

    You are trying to jump from first to third. Like trying to fix your flat tire with a new transmission.

  4. #4

    Thread Starter
    Lively Member
    Join Date
    Jan 2006
    Posts
    108

    Re: Winsock problem with sending data

    Ok, what am I supposed to do then? I usually jump over reading books and just jump into making something

  5. #5
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: Winsock problem with sending data

    Press F9 to create a Breakpoint on a line, and then press F8 to step line thru line thru the code. Press F5 to resume the code.

  6. #6

    Thread Starter
    Lively Member
    Join Date
    Jan 2006
    Posts
    108

    Re: Winsock problem with sending data

    Thank you, I will try that

    Edit:

    That was great

    Ok, the code stops here:

    VB Code:
    1. Private Sub Winsock_DataArrival(ByVal eventSender As System.Object, ByVal eventArgs As AxMSWinsockLib.DMSWinsockControlEvents_DataArrivalEvent) Handles Winsock.DataArrival
    2.         Dim strData As String <-- It jumps over this
    3.         ' get the data
    4.         Winsock.GetData(strData) <-- This is the last thing it does
    5.         ' display it in the main textbox
    6.         LogData(strData)
    7.  
    8.     End Sub

    I tried putting in txtMain.Text = strData (instead of the LogData(strData)) so it would just delete everything in the textbox and put the data from strData there. That didnt work either...

    Why does it jump over Dim strData As String? Is that the problem?


    Edit Nr2: It works now! For some reason it couldnt handle strData because it had no value from the start so I gave it a value and now it works

    VB Code:
    1. Private Sub Winsock_DataArrival(ByVal eventSender As System.Object, ByVal eventArgs As AxMSWinsockLib.DMSWinsockControlEvents_DataArrivalEvent) Handles Winsock.DataArrival
    2.         Dim strData As String
    3.         strData = "whatever" <-- I added that
    4.         ' get the data
    5.         Winsock.GetData(strData)
    6.         ' display it in the main textbox
    7.         LogData(strData)
    8.  
    9.     End Sub
    Last edited by Darkstorm; Jan 5th, 2006 at 03:03 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width