Results 1 to 8 of 8

Thread: Winsock Help!

  1. #1

    Thread Starter
    New Member
    Join Date
    Mar 2005
    Location
    London
    Posts
    7

    Resolved Winsock Help!

    Hi everyone, first i will explain my set up:

    Winsock Program as Client

    Connects to my embedded PIC program, which is C code with a tcp/ip stack.

    I can get them to connect but it will only work once.

    The second time i get the run time error stating that:

    socket not bound, invalid address or listen is not invoked prior to accept.

    I am guessing there is a problem with the socket still being open.

    I am unsure whether it is from my V Basic code or the c code?

    Here is my VB Code:

    VB Code:
    1. ' initialize variables
    2. Private Sub Form_Load()
    3.    
    4.    
    5.     txtRemoteIpAddress = "192.168.0.20"
    6.     txtRemotePort = "13"
    7.     txtSocketStatus = SocketStatus(tcpClient.State)
    8.     txtLocalPort = tcpClient.LocalPort
    9.     txtLocalIpAddress = tcpClient.LocalIP
    10.     txtLocalHostName = tcpClient.LocalHostName
    11.  
    12.        
    13. End Sub
    14.  
    15.  
    16.  
    17. ' operator wants to connect to the server
    18. Private Sub cmdConnect_Click()
    19.     If (tcpClient.State = sckClosed) Then
    20.         ' Invoke the Connect method to initiate a connection.
    21.      
    22.         tcpClient.RemotePort = txtRemotePort
    23.         tcpClient.RemoteHost = txtRemoteIpAddress
    24.         tcpClient.Connect
    25.         txtRemoteHostName = tcpClient.RemoteHost
    26.     Else
    27.         MsgBox "ERROR: socket is not closed.  Socket must be closed before opening a new connection"
    28.     End If
    29. End Sub
    30.  
    31.  
    32.  
    33. Private Sub cmdDisconnect_Click()
    34. 'Disconnect from ClientProcedure
    35.  
    36. ' operator wants to exit the program
    37.  
    38.     tcpClient.Close
    39.     End
    40. End Sub
    41.  
    42.  
    43.  
    44. ' the server disconnected the connection
    45. Private Sub tcpClient_Close()
    46.     tcpClient.Close
    47. End Sub
    48.  
    49. ' the server sent us data
    50. Private Sub tcpClient_DataArrival(ByVal bytesTotal As Long)
    51.     Dim strData As String
    52.    
    53.     tcpClient.GetData strData, vbString
    54.     txtOutput.Text = strData
    55. End Sub
    56.  
    57.  
    58. ' winsock control encountered an error
    59. Private Sub tcpClient_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)
    60.    Debug.Print "Server Error Number = " + Str(Number) + ",  Description = " + Description
    61.    txtSocketStatus = Description
    62. End Sub
    63.  
    64. ' this timer simply updates the status of the socket control on the display
    65. Private Sub Timer1_Timer()
    66.     txtSocketStatus = SocketStatus(tcpClient.State)
    67.     ' IF the socket is stuck in the closing or error states THEN close the socket to reset it
    68.     If ((tcpClient.State = sckClosing) Or (tcpClient.State = sckError)) Then
    69.         tcpClient.Close
    70.     End If
    71. End Sub


    Maybe i should have the vb code as the server and my embedded c code as the client?
    Last edited by stratty; Mar 23rd, 2005 at 12:15 PM.

  2. #2
    KING BODWAD XXI BodwadUK's Avatar
    Join Date
    Aug 2002
    Location
    Nottingham
    Posts
    2,176

    Re: Winsock Help!

    What line does the error occur on?

    You are sure the winsock is set for TCP/IP and not UDP see its properties
    If you dribble then you are as mad as me

    Lost World Creations Website (XBOX Indie games)
    Lene Marlin

  3. #3

    Thread Starter
    New Member
    Join Date
    Mar 2005
    Location
    London
    Posts
    7

    Re: Winsock Help!

    The error occurs in

    VB Code:
    1. tcpClient.Connect

    And yes the property is set to TCP

  4. #4
    Fanatic Member namrekka's Avatar
    Join Date
    Feb 2005
    Location
    Netherlands
    Posts
    639

    Re: Winsock Help!

    On the PC check with:

    "netstat -a"

    Then you can see if a port is still open or not.

    Success

  5. #5
    Fanatic Member Comintern's Avatar
    Join Date
    Nov 2004
    Location
    Lincoln, NE
    Posts
    826

    Re: Winsock Help!

    Quote Originally Posted by stratty
    I can get them to connect but it will only work once.

    The second time i get the run time error stating that:

    socket not bound, invalid address or listen is not invoked prior to accept
    From this error message, I would guess that your problem is with the server side, not the client side. It sounds like the server is not resetting itself after all of the active sockets are closed. Can you post some of the C code?

  6. #6
    KING BODWAD XXI BodwadUK's Avatar
    Join Date
    Aug 2002
    Location
    Nottingham
    Posts
    2,176

    Re: Winsock Help!

    Try setting the client port to 0 before connecting. This allows windows to select a free port. There is also nothing wrong with doing a close before a connect it infact prevents errors that somehow slip though
    If you dribble then you are as mad as me

    Lost World Creations Website (XBOX Indie games)
    Lene Marlin

  7. #7

    Thread Starter
    New Member
    Join Date
    Mar 2005
    Location
    London
    Posts
    7

    Re: Winsock Help!

    Quote Originally Posted by BodwadUK
    Try setting the client port to 0 before connecting. This allows windows to select a free port.

    Im not sure what u mean, because my server code is only sending data out if it gets a connection on port 13.

    Anyway here is my C code :

    Code:
    
    
     if (locport==PORT)
    
                    {                               // Recognised port?
                        rack = ((DWORD)concount << 16);
                        rack += 0xffff;
                        concount++;
                        tflags = TSYN+TACK;         // ..send SYN ACK
                    }
                    else                            // Unrecognised port?
                        tflags = TRST+TACK;         // ..send reset
                }
                else if (rflags & TFIN)             // Received FIN?
                    rseq += rpdlen+1;               // ..ack all incoming data + FIN
                else if (rflags & TACK)             // ACK received?
                {
                    if (rpdlen)                     // ..adjust Tx ack for Rx data
                        rseq += rpdlen;
                    else                            // If no data, don't send ack
                        tflags = 0;
    
                    if (locport==PORT && (WORD)rack==0)
                    {                               // Daytime request?
                        putstr(DAYMSG);
                        tflags = TFIN+TACK;         // Ack & close connection
                    }

  8. #8
    KING BODWAD XXI BodwadUK's Avatar
    Join Date
    Aug 2002
    Location
    Nottingham
    Posts
    2,176

    Re: Winsock Help!

    Only on the client in VB.

    Because the client doesnt need to be on a fixed local port (Its connecting out not in) you can have any port. It is therefore best to allow windows to allocate a free port than to try and take a fixed port that could be in use

    To do this just before any connect event add

    tcpClient.localport = 0
    tcpClient.Connect
    If you dribble then you are as mad as me

    Lost World Creations Website (XBOX Indie games)
    Lene Marlin

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