tcpclient responds only once...

outcast1881

Active member
Joined
Aug 25, 2006
Messages
38
Programming Experience
Beginner
Hi everyone,
I am writing a client-server connection.The server is an rfid device.When I enter a query from textbox1,I get the respond in textbox2(the tag numbers).And when the conditions change for instance new tags come into the environment the query remains the same but how to change the respond automatically according to new data.Indeed the rfid reader continuously reads and saves the new data but how to get it in my textbox2....
sorry,I coulnd't express myself clearly,I hope someone had understood what I tried to explain and can help me as well.

Kindest regards,
Can

VB.NET:
Imports System.NET.Sockets
Imports System.Text
 
PublicClass Form1
Dim tagid AsString
Dim tcpClient AsNew System.Net.Sockets.TcpClient
Dim networkStream As NetworkStream
PrivateSub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) HandlesMyBase.Load
tcpClient.Connect("111.11.11.111", 8080)
networkStream = tcpClient.GetStream()
MessageBox.Show("connected")
EndSub
PrivateSub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
If networkStream.CanWrite And networkStream.CanRead Then
Dim sendBytes(TextBox1.Text.Length) AsByte
'Convert the contents of textbox1 to bytes for transmission(here I enter the query in the form)
sendBytes = Encoding.ASCII.GetBytes(TextBox1.Text)
'send the contents of textbox1 to address specified by networkstream (here I get the respond from the device)
networkStream.Write(sendBytes, 0, sendBytes.Length)
Dim bytes(tcpClient.ReceiveBufferSize) AsByte
networkStream.Read(bytes, 0, CInt(tcpClient.ReceiveBufferSize))
' Output the data received from the host to the textbox.
Dim returndata AsString = Encoding.ASCII.GetString(bytes)
TextBox2.Text = returndata
 
'attaining textbox value(query result) to tagid variable
tagid = TextBox2.Text.Substring(18, 6)
 
 
Else
IfNot networkStream.CanRead Then
Console.WriteLine("cannot not write data to this stream")
tcpClient.Close()
Else
IfNot networkStream.CanWrite Then
Console.WriteLine("cannot read data from this stream")
tcpClient.Close()
EndIf
EndIf
EndIf
EndSub
PrivateSub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
'a unique code used to quit the server
Dim sendBytes("!quit!".Length + 1) AsByte
'Convert the word !quit! to Bytes
sendBytes = Encoding.ASCII.GetBytes("!quit!")
'send the quit command to the server
networkStream.Write(sendBytes, 0, sendBytes.Length)
'close the connections
networkStream.Close()
tcpClient.Close()
'exit the program
Application.Exit()
EndSub
EndClass
 
Last edited by a moderator:
ok I solved it this way:
VB.NET:
Dim x AsInteger
x = 1
DoWhile x = 1
networkStream.Write(sendBytes, 0, sendBytes.Length)
Dim bytes(tcpClient.ReceiveBufferSize) AsByte
networkStream.Read(bytes, 0, CInt(tcpClient.ReceiveBufferSize))
' Output the data received from the host to the textbox.
Dim returndata AsString = Encoding.ASCII.GetString(bytes)
TextBox2.Text = returndata
tagid = TextBox2.Text
TextBox2.Refresh()
 
Loop
In case someone has thesame problem especially who's new in programming:)

Can
 
Last edited by a moderator:
GUI freezes while using TCPclient in VB.net

now the GUI freezes,I receive the values into textbox2 but I can't exit the programm,'cause I can't use any control on on the form....


Can
 
Problem in closing Tcpclient and Connecting again

HI everyone,

I have a program that connects to an external device by clicking button1,then I can send data through textbox1 and receive response through textbox2 and end the connection by clicking button2.
everything works OK.But when I click button1 in order to connect again,I can't connect and get this error message:

system.objectdisposedException
this object can not be accessed
"System.Net.Sockets.TcpClient"

my code for button1(connect):
VB.NET:
tcpClient.Connect("111.11.11.000", 8080)
networkStream = tcpClient.GetStream()
MessageBox.Show("connected")


my code for button2(quit):
VB.NET:
networkStream.Close()
tcpClient.Close()


I am new in programming so I maý not be realising a big mistake that I made...
Thanks&regards


Can
 
Last edited by a moderator:
I solved it...I just need to write this code for closing the connection,the other stuff was just trash...

networkStream.Close()
tcpClient.Close()

GUI is responding now.

Can
 
Hmmm.I got the problem:

VB.NET:
Dim tcpclient AsNew TcpClient [COLOR=darkgreen]'I had to add this line...[/COLOR]
tcpClient.Connect("111.11.11.000", 8080)
networkStream = tcpClient.GetStream()
MessageBox.Show("connected")


Now I can connect again,by clicking button1.
BUt now this lines gives me an error(nullreferenceException):


networkStream.Read(bytes, 0, CInt(tcpClient.ReceiveBufferSize))



I think I have to declare bytes as new bytes but I can't do it like
dim bytes as new byte...

Does anyone have an idea?

Thanks&regards

Can

Thnaks

 
Last edited by a moderator:
solution

Problem1: GUI becomes unresponsive==>>Solution using
Application.DoEvents() in the loop

Problem2: can not connect again after closing the tcpclient connection ==>>Solution defining tcpclient as:

tcpClient = New TcpClient

before

tcpClient.Connect


Sorry i realised that I had written too many questions but I was too desperate about finding the solutions and I couldn't realise correctly in the beginning which part of the code does what...Now I ahve less code and a running tcpclient program with an external device...

Regards,

Can
 
Back
Top