Results 1 to 9 of 9

Thread: [RESOLVED] Socket Programming

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Sep 2006
    Location
    UK / East Sussex
    Posts
    1,054

    Resolved [RESOLVED] Socket Programming

    Good Afternoon,

    I am building a client-server type applications and they are communicating over sockets. This is the first time I am doing such a program and I have been learning off a web example and thought I understood it all but it would appear I am missing something as it starts to work then errors. Here is what I have:

    VB Code:
    1. Dim serverSocket As New TcpListener(ListeningPort)
    2.             Dim clientSocket As TcpClient
    3.             Dim netStream As NetworkStream
    4.             Dim BytesFrom(1024) As Byte
    5.             Dim DataFromClient As String
    6.             Dim ServerResponse As String
    7.             Dim BytesTo As [Byte]()
    8.  
    9.             serverSocket.Start()
    10.  
    11.             clientSocket = serverSocket.AcceptTcpClient()
    12.  
    13.           netStream = clientSocket.GetStream
    14.  
    15.             'SEND TO CLIENT
    16.             ServerResponse = "IDENTIFY"
    17.             BytesTo = Encoding.ASCII.GetBytes(ServerResponse)
    18.             netStream.Write(BytesTo, 0, BytesTo.Length)
    19.             netStream.Flush()

    All of the above works and if I open up a telnet command window I can connect to my machine running this code on my specified port and in the command prompt window 'IDENTIFY' appears as it should from the above code.

    But it then gets to this block an errors on the second line:

    VB Code:
    1. 'RECEIVE FROM CLIENT
    2.             netStream = clientSocket.GetStream()
    3.             netStream.Read(BytesFrom, 0, CInt(clientSocket.ReceiveBufferSize))
    4.             DataFromClient = System.Text.Encoding.ASCII.GetString(BytesFrom)

    Now as far as I can tell it shouldn't be processing the first line [ netStream = clientSocket.GetStream() ]

    until I send something back from the client because then going on to the second line its trying to process something that isn't there yet.

    The actual error I get is:

    Specified argument was out of the range of valid values. Parameter name: size

    on the line:

    netStream.Read(BytesFrom, 0, CInt(clientSocket.ReceiveBufferSize))


    Am I missing something here?
    M.Carpenter

    Server Admin for Wurm Online
    Wurm Online - A very unique and original MMO from Code Club AB
    https://www.youtube.com/watch?v=YQlYar2uHWAWurm Trailor


  2. #2

    Re: Socket Programming

    How big is BytesFrom? You might be filling it too large because the default value for the ReceiveBufferSize is 8192.

    EDIT: Just noticed BytesFrom is declared with 1024 bytes. Declare it with a size equal to ReceiveBufferSize (8192) and it 'should' remove your error, OR what you can do is change this line:

    Code:
    netStream.Read(BytesFrom, 0, CInt(clientSocket.ReceiveBufferSize))
    to

    Code:
    netStream.Read(BytesFrom, 0, BytesFrom.Length))
    That should fix your error because now you have matching sizes (or at least you're now not going -over- the maximum size you have allocated).
    Last edited by formlesstree4; Aug 10th, 2011 at 10:28 AM. Reason: Found yer problem!

  3. #3

    Thread Starter
    Frenzied Member
    Join Date
    Sep 2006
    Location
    UK / East Sussex
    Posts
    1,054

    Re: Socket Programming

    Thanks its now waiting for an input however there is the next problem on that block of code. Its only receiving one byte.

    For instance if I am connected to the socket via telnet and I paste in the telnet:

    IDONTLIKETHIS

    Then my code is:

    netStream = clientSocket.GetStream()
    netStream.Read(BytesFrom, 0, BytesFrom.Length)
    DataFromClient = System.Text.Encoding.ASCII.GetString(BytesFrom, 0, BytesFrom.Length)
    MessageBox.Show(DataFromClient)

    I get a messagebox saying: I
    M.Carpenter

    Server Admin for Wurm Online
    Wurm Online - A very unique and original MMO from Code Club AB
    https://www.youtube.com/watch?v=YQlYar2uHWAWurm Trailor


  4. #4

    Re: Socket Programming

    Then you need to setup a loop that will continually loop until all the data is read:

    Code:
    While clientSocket.GetStream.DataAvailable
       'Do your stuff here
    End While

  5. #5

    Thread Starter
    Frenzied Member
    Join Date
    Sep 2006
    Location
    UK / East Sussex
    Posts
    1,054

    Re: Socket Programming

    Thanks Sorry for such stupid questions.. whole new section for me.
    I will give that ago when I am in the office.
    M.Carpenter

    Server Admin for Wurm Online
    Wurm Online - A very unique and original MMO from Code Club AB
    https://www.youtube.com/watch?v=YQlYar2uHWAWurm Trailor


  6. #6

    Thread Starter
    Frenzied Member
    Join Date
    Sep 2006
    Location
    UK / East Sussex
    Posts
    1,054

    Re: Socket Programming

    I am going f**** bonkers with this. no matter what I do I can only use the first byte sent from the client. Its receiving all the bytes but won't let me use them. Here is some sample code I knocked up and it does not work what so ever. Does it for anyone else?

    VB Code:
    1. Dim Datafromclient as string
    2. Dim tmp() as string
    3. ReDim tmp(6)
    4.  
    5.         For i = 0 To 5
    6.  
    7.             steamer = client.GetStream()
    8.             steamer.Read(BytesFrom, 0, CInt(client.ReceiveBufferSize))
    9.             DataFromClient = System.Text.Encoding.ASCII.GetString(BytesFrom, 0, BytesFrom.Length)
    10.             tmp(i) = DataFromClient
    11.  
    12.         Next
    13.  
    14.  
    15. 'AT THIS POINT THE WATCH POINTS STATE:
    16. 'Tmp(0) = A
    17. 'Tmp(1) = B
    18. 'Tmp(2) = C
    19. 'Tmp(3) = D
    20. 'Tmp(4) = E
    21. 'Tmp(5) = F
    22.  
    23.  
    24.  
    25.  
    26.         MessageBox.Show(tmp(0).ToString & tmp(1).ToString & tmp(2).ToString & tmp(3).ToString & tmp(4).ToString & tmp(5).ToString)
    27.  
    28. 'THIS PRODUCES A MESSAGE BOX WITH THE TEXT:     A
    29.  
    30. Dim MyLastChance As String = tmp(0) & tmp(1) & tmp(2) & tmp(3) & tmp(4) & tmp(5)
    31.  
    32. 'Watch point for MyLastChance here = A
    M.Carpenter

    Server Admin for Wurm Online
    Wurm Online - A very unique and original MMO from Code Club AB
    https://www.youtube.com/watch?v=YQlYar2uHWAWurm Trailor


  7. #7

    Re: Socket Programming

    Instead of writing directly to the network stream, try using the Send() method of the Socket Class instead of writing to the stream.

  8. #8

    Thread Starter
    Frenzied Member
    Join Date
    Sep 2006
    Location
    UK / East Sussex
    Posts
    1,054

    Re: Socket Programming

    I have had a quick glance at Send() but couldn't understand it however I have made a slight breakthrough.

    I was testing my server app by using a telnet client. I have now discovered this doesn't work. If I create a client and server app the whole string goes through which is fine.

    However there is still one thing missing and I am going to attempt to explain it.

    At present I have two applications the server and the client. The server has a richtext box that displays the last received message from the client. The client as a text box and a send button and when you hit send it sends the message through the socket.

    Now my first message from client to server I send:

    THIS IS WORKING WOOHOO!$

    and sure enough on the server it puts in the text box:

    THIS IS WORKING WOOHOO!$

    As expected. So I now send from the client BRILLIANT!$

    but the server then displays:

    BRILLIANT$RKING WOOHOO!$

    Here is my code for receiving from the client:

    VB Code:
    1. While Not DataFromClient.Contains("$")
    2.  
    3.             netStream = clientSocket.GetStream()
    4.             netStream.Read(BytesFrom, 0, CInt(clientSocket.ReceiveBufferSize))
    5.             DataFromClient = DataFromClient & System.Text.Encoding.ASCII.GetString(BytesFrom, 0, BytesFrom.Length)
    6.             netStream.Flush()
    7.  
    8.         End While
    9.  
    10.         messages = DataFromClient
    11.        
    12.         DataFromClient = ""
    13.         netstream = nothing

    I then go back to the while loop and start again. What am I missing? What needs to be cleared?
    M.Carpenter

    Server Admin for Wurm Online
    Wurm Online - A very unique and original MMO from Code Club AB
    https://www.youtube.com/watch?v=YQlYar2uHWAWurm Trailor


  9. #9

    Thread Starter
    Frenzied Member
    Join Date
    Sep 2006
    Location
    UK / East Sussex
    Posts
    1,054

    Re: Socket Programming

    I feel like a right prat now I just needed to clear the array BytesFrom. DUH
    M.Carpenter

    Server Admin for Wurm Online
    Wurm Online - A very unique and original MMO from Code Club AB
    https://www.youtube.com/watch?v=YQlYar2uHWAWurm Trailor


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