Results 1 to 10 of 10

Thread: Socket Problems [Resolved]

Threaded View

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Aug 2002
    Location
    Belfast, N. Ireland
    Posts
    167

    Socket Problems [Resolved]

    Hi all,

    Sorry to post a big chunk of code like this and ask 'what’s wrong', it seems lazy, but I've read as much as I can find and I still can't figure out how to fix this... I'm stuck!

    I'm trying to write a client program which will send data to a server program, waits for a reply and then receives the data sent from the server.

    Here's my code, it works if I step through it slowly in debugging mode, but not if I run it normally.



    VB Code:
    1. Const READ_BUFFER_SIZE As Integer = 255
    2. Const PORT_NUM As Integer = 2100
    3.  
    4. Private client As TcpClient
    5. Private readBuffer(READ_BUFFER_SIZE) As Byte
    6.  
    7. Private Sub SendData(ByVal data As String)
    8.  
    9.         client = New TcpClient("localhost", PORT_NUM)
    10.         client.ReceiveTimeout = 500
    11.        
    12.         ' Send request to server
    13.  
    14.         Dim writer As New IO.StreamWriter(client.GetStream)
    15.         writer.Write(data & vbCr)
    16.         writer.Flush()
    17.        
    18.         ' Receive reply and display in messagebox
    19.  
    20.         Dim networkStream As NetworkStream = client.GetStream()
    21.         Dim bytes(client.ReceiveBufferSize) As Byte
    22.         Dim returndata As String
    23.  
    24.         While networkStream.DataAvailable
    25.                 networkStream.Read(bytes, 0, CInt(client.ReceiveBufferSize))
    26.                 returndata = returndata & Encoding.ASCII.GetString(bytes)
    27.         End While
    28.  
    29.         networkStream.Close()
    30.  
    31.         MsgBox(returndata)
    32.  
    33.         client.Close()
    34.         client = Nothing
    35.  
    36. End Sub

    Hopefully someone out there has some expertise with socket programming and can help me out.

    Thanks!

    Justin.

    Last edited by Justy; Aug 26th, 2003 at 03:21 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