[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:
Dim serverSocket As New TcpListener(ListeningPort)
Dim clientSocket As TcpClient
Dim netStream As NetworkStream
Dim BytesFrom(1024) As Byte
Dim DataFromClient As String
Dim ServerResponse As String
Dim BytesTo As [Byte]()
serverSocket.Start()
clientSocket = serverSocket.AcceptTcpClient()
netStream = clientSocket.GetStream
'SEND TO CLIENT
ServerResponse = "IDENTIFY"
BytesTo = Encoding.ASCII.GetBytes(ServerResponse)
netStream.Write(BytesTo, 0, BytesTo.Length)
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:
'RECEIVE FROM CLIENT
netStream = clientSocket.GetStream()
netStream.Read(BytesFrom, 0, CInt(clientSocket.ReceiveBufferSize))
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?