|
-
Aug 10th, 2011, 09:34 AM
#1
Thread Starter
Frenzied Member
[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?
-
Aug 10th, 2011, 10:25 AM
#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!
-
Aug 10th, 2011, 11:03 AM
#3
Thread Starter
Frenzied Member
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
-
Aug 10th, 2011, 11:30 AM
#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
-
Aug 11th, 2011, 01:38 AM
#5
Thread Starter
Frenzied Member
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.
-
Aug 11th, 2011, 05:36 AM
#6
Thread Starter
Frenzied Member
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:
Dim Datafromclient as string Dim tmp() as string ReDim tmp(6) For i = 0 To 5 steamer = client.GetStream() steamer.Read(BytesFrom, 0, CInt(client.ReceiveBufferSize)) DataFromClient = System.Text.Encoding.ASCII.GetString(BytesFrom, 0, BytesFrom.Length) tmp(i) = DataFromClient Next 'AT THIS POINT THE WATCH POINTS STATE: 'Tmp(0) = A 'Tmp(1) = B 'Tmp(2) = C 'Tmp(3) = D 'Tmp(4) = E 'Tmp(5) = F MessageBox.Show(tmp(0).ToString & tmp(1).ToString & tmp(2).ToString & tmp(3).ToString & tmp(4).ToString & tmp(5).ToString) 'THIS PRODUCES A MESSAGE BOX WITH THE TEXT: A Dim MyLastChance As String = tmp(0) & tmp(1) & tmp(2) & tmp(3) & tmp(4) & tmp(5) 'Watch point for MyLastChance here = A
-
Aug 11th, 2011, 11:32 AM
#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.
-
Aug 12th, 2011, 05:19 AM
#8
Thread Starter
Frenzied Member
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:
While Not DataFromClient.Contains("$")
netStream = clientSocket.GetStream()
netStream.Read(BytesFrom, 0, CInt(clientSocket.ReceiveBufferSize))
DataFromClient = DataFromClient & System.Text.Encoding.ASCII.GetString(BytesFrom, 0, BytesFrom.Length)
netStream.Flush()
End While
messages = DataFromClient
DataFromClient = ""
netstream = nothing
I then go back to the while loop and start again. What am I missing? What needs to be cleared?
-
Aug 12th, 2011, 05:22 AM
#9
Thread Starter
Frenzied Member
Re: Socket Programming
I feel like a right prat now I just needed to clear the array BytesFrom. DUH
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|