473,409 Members | 1,983 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,409 software developers and data experts.

TcpListener only works first time

I've implemented the code from MS at:
http://msdn.microsoft.com/library/de...ClassTopic.asp
and am able to send data but it only works once. Nothing happens the second
time, not even an error.

Here's what my client function looks like:

Function SendData(ByVal Data As String) As String

Dim port As Int32 = 13000
Dim localAddr As IPAddress = IPAddress.Parse("127.0.0.1")

Dim client As New TcpClient
client.Connect(localAddr, port)

Dim stream As NetworkStream = client.GetStream()

Dim msg As [Byte]() = System.Text.Encoding.ASCII.GetBytes(Data)
stream.Write(msg, 0, msg.Length)

client.Close()

End Function

Anyone know what the problem is?
Nov 20 '05 #1
5 2840
Yes Greg,

Every time you call this function you are initializing a new instance of the
client.
You should only initialize it once, outside somewhere, and assign bytes to
the stream every time you want to send data.
Also, although the documentation says it is not necessary, flush the stream
just to make sure everything goes as planned (stream.flush())

Good Luck.

"Greg" <no****@nospam.com> wrote in message
news:Os**************@TK2MSFTNGP09.phx.gbl...
I've implemented the code from MS at:
http://msdn.microsoft.com/library/de...ClassTopic.asp and am able to send data but it only works once. Nothing happens the second time, not even an error.

Here's what my client function looks like:

Function SendData(ByVal Data As String) As String

Dim port As Int32 = 13000
Dim localAddr As IPAddress = IPAddress.Parse("127.0.0.1")

Dim client As New TcpClient
client.Connect(localAddr, port)

Dim stream As NetworkStream = client.GetStream()

Dim msg As [Byte]() = System.Text.Encoding.ASCII.GetBytes(Data)
stream.Write(msg, 0, msg.Length)

client.Close()

End Function

Anyone know what the problem is?

Nov 20 '05 #2
"Juan Romero" <ju*********@bowne.com> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...
Yes Greg,

Every time you call this function you are initializing a new instance of the client.
You should only initialize it once, outside somewhere, and assign bytes to
the stream every time you want to send data.
Also, although the documentation says it is not necessary, flush the stream just to make sure everything goes as planned (stream.flush())

Good Luck.


Thanks for the reply. Stream.Flush() seems to have fixed the problem. I
can now repeatedly send data without any problem.
Nov 20 '05 #3
How could .Flush() possibly have fixed the problem???

The NetworkStream.Flush() method does nothing. The NetworkStream object represents an UNBUFFERED string. There is no buffer to .Flush(). The documentation even states this very plainly. .Flush() doesn't throw any errors, it simply does nothing

Also, in response to Juan's post - of course the code reinitializes the TcpClient each time. That's the nature of the program and that is correct. Once a request has been filled, the connection is closed and TcpListener waits for another request at which point the program calls SendData() and creates a new Client

I am having a similar problem with the NetworkStream .Write() method and have not yet found a solution. Any other (knowledgable) suggestions will be much appreciated

Nov 20 '05 #4
How could .Flush() possibly have fixed the problem???

The NetworkStream.Flush() method does nothing. The NetworkStream object represents an UNBUFFERED string. There is no buffer to .Flush(). The documentation even states this very plainly. .Flush() doesn't throw any errors, it simply does nothing

Also, in response to Juan's post - of course the code reinitializes the TcpClient each time. That's the nature of the program and that is correct. Once a request has been filled, the connection is closed and TcpListener waits for another request at which point the program calls SendData() and creates a new Client

I am having a similar problem with the NetworkStream .Write() method and have not yet found a solution. Any other (knowledgable) suggestions will be much appreciated

Nov 20 '05 #5
How could .Flush() possibly have fixed the problem???

The NetworkStream.Flush() method does nothing. The NetworkStream object represents an UNBUFFERED string. There is no buffer to .Flush(). The documentation even states this very plainly. .Flush() doesn't throw any errors, it simply does nothing

Also, in response to Juan's post - of course the code reinitializes the TcpClient each time. That's the nature of the program and that is correct. Once a request has been filled, the connection is closed and TcpListener waits for another request at which point the program calls SendData() and creates a new Client

I am having a similar problem with the NetworkStream .Write() method and have not yet found a solution. Any other (knowledgable) suggestions will be much appreciated

Nov 20 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Doug Wyatt | last post by:
So I'll preface this with the fact that I'm a UNIX developer by training and have just recently gotten in to C# development on Windows. I'm basically running in to a problem whereby I suspect...
4
by: Rob White | last post by:
OK, so I have a TcpListener that is waiting for sockets, this piece of code: IPAddress localAddress = Dns.GetHostByName(Dns.GetHostName()).AddressList; IPEndPoint localEP = new...
2
by: MuZZy | last post by:
Hi, The app i develop supose to have a remote control/reporting feature, so that if a client connects to it, he can obtain information about application state and to send some commands. I...
2
by: Dave Coate | last post by:
Hi, I am working on a client-server app. I can get two applications to talk to each other on the same machine using 127.0.0.1, but as soon as I try it using a computer name or actual IP address...
2
by: Terry Olsen | last post by:
Hoping someone can help me here. I've got this code written, and it works fine for the first connection. But if I connect another client (while the first is still connected), I get connected but...
3
by: Bjørn Eliasen | last post by:
Hi, I have an application running on all pc's in our company. Basically it is a TCPListener awaiting for sockets to connect and on connection performs the required tasks. The app works fine, but...
2
by: =?Utf-8?B?Sm9uIEphY29icw==?= | last post by:
I use in vb (VS2005) Server = New TcpListener(PortNumber) Which is simple and straightforward. But I get Public Sub New(port As Integer) is obsolete: This method has been deprecated. Please...
4
by: =?Utf-8?B?Sm9uIEphY29icw==?= | last post by:
(Note: the TCP Client is on another machine, and attempts communication every minute.) Both apps use the same assembly The service app starts thusly, creating the tcp object and starting the...
1
by: Darwin | last post by:
Setting a server to listen on 8080 for incoming connections. Written in VS2005 on a Multi-homed machine. I get the warning: Warning 1 'System.Net.Sockets.TcpListener.TcpListener(int)' is obsolete:...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.