473,788 Members | 2,706 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.GetStrea m()

Dim msg As [Byte]() = System.Text.Enc oding.ASCII.Get Bytes(Data)
stream.Write(ms g, 0, msg.Length)

client.Close()

End Function

Anyone know what the problem is?
Nov 20 '05 #1
5 2853
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******** ******@TK2MSFTN GP09.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.GetStrea m()

Dim msg As [Byte]() = System.Text.Enc oding.ASCII.Get Bytes(Data)
stream.Write(ms g, 0, msg.Length)

client.Close()

End Function

Anyone know what the problem is?

Nov 20 '05 #2
"Juan Romero" <ju*********@bo wne.com> wrote in message
news:%2******** ********@TK2MSF TNGP10.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.F lush() 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.F lush() 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.F lush() 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
1818
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 something to do with process groups or threads and closeOnExec semantics (to speak in POSIX terms) is causing me a problem. I've got a windows service (let's call it "myService") that, among other things, does : onStart creates a TcpListener on a...
4
4402
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 IPEndPoint(localAddress, 9000); TcpListener tcpListen = new TcpListener(localEP); tcpListen.Start(); Socket skt = tcpListen.AcceptSocket(); .... do some socket stuff skt.Close();
2
7720
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 started with using TcpListener in a separate thread of the app. Something like this: // ============================ CODE BEGIN ======================= public void ListenerThreadFunction()
2
4663
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 I get a SocketException: "No connection could be made because the target machine actively refused it". On the server I have a listener: Private m_Listener As TcpListener m_Listener = New TcpListener(IPAddress.Parse("127.0.0.1"), 8000)
2
4061
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 nothing else, no data exchange. If I disconnect the 2nd session, I get a System.IO.IOException. If I disconnect the first session with the 2nd session still connected, then all the data I typed in the 2nd session gets returned to me as if it was...
3
4375
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 while the listener is awaiting for socket to connect several other applications can't start, and even installation of new applications migth hang. In order to finalise the other apps or installations the tcp listener is killed resulting in the other...
2
3995
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 use TcpListener(IPAddress localaddr, int port) instead. It also gives a link that returns "page not found."
4
3418
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 timer in the OnStart event. The timer calls OpenServer (one time): Public Class TCPService Private tcp As SimpleTCP Private Timer1 As Timer
1
8641
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: 'This method has been deprecated. Please use TcpListener(IPAddress localaddr, int port) instead. on the code: TcpListener tcpListener = new TcpListener(8080); Do I really have to specify every IP address that I want to listen on?
0
9656
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9498
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10370
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9969
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8995
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6750
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
4074
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3677
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2896
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.