473,385 Members | 2,180 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,385 software developers and data experts.

knowing when data sent so i can close the streamwriter and networkstream

i would like to konw when the data sent so that i can close the streamwriter
and networkstream is there some sort of call backs/events i have to
implement for this to work? if so how? can i just open neworkstream and
streamwriter, send data and then close it syncrhronously or do i have to
implement some callbacks/events to do this like in vb6?

TcpClient myclient;

myclient = new TcpClient("localhost", 1234);

NetworkStream networkStream ;

networkStream = myclient.GetStream();

StreamWriter streamWriter ;

streamWriter = new StreamWriter(networkStream);
streamWriter.WriteLine("<ratpack1324><ratpack1324> <ratpack1324><ratpack1324>
<ratpack1324><ratpack1324><ratpack1324><ratpack132 4><ratpack1324><ratpack132
4><ratpack1324><ratpack1324><ratpack1324><ratpack1 324><ratpack1324><ratpack1
324><ratpack1324><ratpack1324><ratpack1324><ratpac k1324><ratpack1324>v<ratpa
ck1324><ratpack1324><ratpack1324><ratpack1324><rat pack1324><ratpack1324><rat
pack1324><ratpack1324><ratpack1324><ratpack1324><r atpack1324><ratpack1324><r
atpack1324><ratpack1324><ratpack1324><ratpack1324> <ratpack1324><ratpack1324>
<ratpack1324><ratpack1324><ratpack1324>");

streamWriter.Flush();

streamWriter.Close() ;

networkStream.Close();

Jul 21 '05 #1
1 1779
Daniel <so*******************@yahoo.com> wrote:
i would like to konw when the data sent so that i can close the streamwriter
and networkstream is there some sort of call backs/events i have to
implement for this to work? if so how? can i just open neworkstream and
streamwriter, send data and then close it syncrhronously or do i have to
implement some callbacks/events to do this like in vb6?


You should just be able to write with no problems - closing will flush,
basically.

Note that it's worth using a "using" statement to make the code close
the connection even if an exception is thrown. You can also simplify
things a bit by not having a separate variable for the network stream:

using (TcpClient myClient = new TcpClient ("localhost", 1234))
{
using (StreamWriter streamWriter = new StreamWriter
(myClient.GetStream())
{
streamWriter.WriteLine ("...");
}
}

(Admittedly you *could* turn it into one using statement, with

using (StreamWriter writer = new StreamWriter (new TcpClient
("localhost", 1234).GetStream())
{
....
}

but I think that's going a bit far.)

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Jul 21 '05 #2

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

Similar topics

1
by: Daniel | last post by:
after opening socket, sending data then closing socket 3000 times i get "Only one usage of each socket address" what am i doing wrong? is there some thing else i need to do to free up the socket...
3
by: Adie | last post by:
I'm using TcpClient in blocking mode with NetworkStreams and would like to be able to give notification that data was sent - I presume this must be possible? But anyone know how?
4
by: yaron | last post by:
Hi, I have a problem when sending data over TCP socket from c# client to java server. the connection established ok, but i can't send data from c# client to java server. it's work ok with...
6
by: Ryan | last post by:
Hi, I am confused with how NetworkStream works. My application needs to handle heavy requests sent through TCP socket connection. I use NetworkStream.Read method to get the stream...
1
by: Daniel | last post by:
i would like to konw when the data sent so that i can close the streamwriter and networkstream is there some sort of call backs/events i have to implement for this to work? if so how? can i just...
3
by: TulasiKumar | last post by:
hi all, My requirment is i want to listen one perticular TCPIP port for one IPAddress,Wheneverver the data in coming to this port that data i will be save one text file..For...
0
by: Al Wilkerson | last post by:
Hey, Has anyone ever got a "Unable to read data from transport connected" message after reading data from a streamreader composed of a networkstream. For example: Server TcpListener...
10
by: Uma - Chellasoft | last post by:
Hai, I am new to VB.Net programming, directly doing socket programming. In C, I will be able to map the message arrived in a socket directly to a structure. Is this possible in VB.Net. Can...
0
by: Buddy Home | last post by:
There is two examples of code. Example 1. Send and Receive within the same process. Put this code in a console app called SendAndReceive and run the code. using System; using...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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...

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.