Hi
I am creating an web application that connects to an tcp server.
When i give the command to connect to the server, he gives an response from the server and an textbox and Commandbutton are visible for command`s to the server.
When i am sending an command to the server then i get this error:
"The operation is not allowed on non-connected sockets."
This is my full cource:
Imports System.Net.Sockets
Class _Default
Inherits System.Web.UI.Page
Public client As New System.Net.Sockets.TcpClient()
Public NetworkStream As NetworkStream
Public bytes(client.ReceiveBufferSize) As Byte
Public returndata As String
Public SendBytes As Byte()
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
On Error GoTo fout
client.Connect("207.42.200.123", 81)
Label1.Text = "Connected to server"
Label1.ForeColor = Drawing.Color.Black
TextBox1.Visible = True
txt_send.Visible = True
cmd_send.Visible = True
NetworkStream = client.GetStream()
NetworkStream.Read(bytes, 0, CInt(client.ReceiveBufferSize))
returndata = Encoding.ASCII.GetString(bytes)
TextBox1.Text = returndata
Exit Sub
fout:
Button2.Visible = False
Label1.Text = "Error when trying to connect to server"
Label1.ForeColor = Drawing.Color.Red
End Sub
Protected Sub cmd_send_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles cmd_send.Click
SendBytes = Encoding.ASCII.GetBytes(txt_send.Text)
NetworkStream = client.GetStream()
NetworkStream.Write(SendBytes, 0, SendBytes.Length)
NetworkStream = client.GetStream()
NetworkStream.Read(bytes, 0, CInt(client.ReceiveBufferSize))
returndata = Encoding.ASCII.GetString(bytes)
TextBox1.Text = TextBox1.Text & vbCrLf & returndata
End Sub
Protected Sub Button2_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button2.Click
TextBox1.Visible = False
txt_send.Visible = False
cmd_send.Visible = False
client.Close()
End Sub
End Class
The error is in line 37 :
Line 35: Protected Sub cmd_send_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles cmd_send.Click
Line 36: SendBytes = Encoding.ASCII.GetBytes(txt_send.Text)
Line 37: NetworkStream = client.GetStream()
Line 38: NetworkStream.Write(SendBytes, 0, SendBytes.Length)
Line 39: NetworkStream = client.GetStream()
Can somebody help me

Error message TCPClient
Scott Currie MSFT
Thnx
jlkirkjr
In your code, the NetworkStream variable gets set in the code run by the connect button1. You have to run that before you run the code in the send button, or you will get the error you posted.
Tracy
sanjay_shinder
so i need a way that the connection always stays open on an session.
if you know what i mean
DexterDurai
Object reference not set to an instance of an object.
wizArdcn
There is no need to get the NetworkStream a second time in the cmd_send_Click().
You can use the network stream that you got originally to read and write.
The fact that a network stream is already opened on the tcpclient is the issue.
Granted we could have thrown a better exception, please fix your code and see if the problem goes away
Will Merydith
Tracy
acgentry
Tracy