Listen to Port

  • Thread starter Thread starter markgoldin
  • Start date Start date
M

markgoldin

I am looking for code to listen to a tcp port. Another (not .net) process
will be writing to that port on the same computer and I need c# code to be
able to get data, which is a simple string, from that port. I am not a c#
coder but can easy adapt working sample.
Thanks alot for help.
 
Hi,

thats really easy to do in .NET. Thats all you need:

//inside a console Main

byte[] byteReadStream = null; // holds the data in byte buffer
IPEndPoint ipe = new IPEndPoint(IPAddress.Parse("0.0.0.0"),
8888);//listen on all local addresses and 8888 port
TcpListener tcpl = new TcpListener(ipe);

while(true){ //infinite loop


tcpl.Start(); // block application until data and connection
is requested
TcpClient tcpc = tcpl.AcceptTcpClient(); //accept connection

byteReadStream = new byte[tcpc.Available]; //allocate space
for data
tcpc.GetStream().Read(byteReadStream, 0, tcpc.Available);
//read data into byte array
Console.WriteLine(Encoding.Default.GetString(byteReadStream)
+ "\n"); Write data to console buffer



}


But see the MSDN Library for more information
about the used Classes.


Regards

Kerem
 
What is a difference between listening to a socket or to a TCP port? Or it's
the same?

Thanks
 
markgoldin said:
What is a difference between listening to a socket or to a TCP port? Or
it's the same?

Thanks
A socket is basically a combination of an IP address and a port.
 
A socket is basically a combination of an IP address and a port.

ACK.

But i would describe a Socket more an connection endpoint
that is bound to an ip address and a corresponding port.

Regards

K.
 
Hi Pete,

i even didnt compile the code. I wrote it on the
fly inside #develop. It was just a first hint for the OP.
As statet he should read the docs in my reply,...


Regards

Kerem

--
-----------------------
Beste Grüsse / Best regards / Votre bien devoue
Kerem Gümrükcü
Microsoft Live Space: http://kerem-g.spaces.live.com/
Latest Open-Source Projects: http://entwicklung.junetz.de
-----------------------
"This reply is provided as is, without warranty express or implied."
Peter Duniho said:
[...]
byteReadStream = new byte[tcpc.Available]; //allocate
space
for data
tcpc.GetStream().Read(byteReadStream, 0, tcpc.Available);
//read data into byte array

Console.WriteLine(Encoding.Default.GetString(byteReadStream)
+ "\n"); Write data to console buffer

This is wrong, for at least a couple of reasons.

The most significant one is that TCP does not guarantee that all of the
data sent will be read in a single call to Read(). You _must_ iterate the
read in some way (loop, async i/o, whatever) until you know for sure that
you've read all of the data. This could be as simple as reading
everything until the end of the stream (connection is closed), or more
complicated as in defining a fixed-length for the data, sending a length
before the string, or using some sort of delimiter/terminator (e.g. null
character).

The other issue is that the Available property may change between the time
one allocates the buffer and the time one calls Read(). If it increases,
then the buffer length passed to Read() is larger than the buffer itself
actually is, which will produce an error. At the very least, one should
be passing "byteReadStream.Length" as the buffer length, not some other
value. Preferable would be to allocate the buffer as some fixed size,
rather than anticipating the size of the read. What size is appropriate
depends on your application protocol, but for larger transfers, a 4K or 8K
buffer would be appropriate.

IMHO it would be better for the OP to simply read the docs for
TcpListener, etc. and take advantage of the code samples there. If he
still has questions after looking at the documentation, then we can answer
those in a more specific, directed way.

Pete
 
Thanks for your code.
I am running the code, but it only shows data being sent to TCP port if I
debug program firstly stopping it at the line:
byteReadStream = new byte[tcpc.Available];

otherwise, if I just run it I dont see data shown in the Console.



Kerem Gümrükcü said:
Hi,

thats really easy to do in .NET. Thats all you need:

//inside a console Main

byte[] byteReadStream = null; // holds the data in byte buffer
IPEndPoint ipe = new IPEndPoint(IPAddress.Parse("0.0.0.0"),
8888);//listen on all local addresses and 8888 port
TcpListener tcpl = new TcpListener(ipe);

while(true){ //infinite loop


tcpl.Start(); // block application until data and
connection
is requested
TcpClient tcpc = tcpl.AcceptTcpClient(); //accept
connection

byteReadStream = new byte[tcpc.Available]; //allocate space
for data
tcpc.GetStream().Read(byteReadStream, 0, tcpc.Available);
//read data into byte array

Console.WriteLine(Encoding.Default.GetString(byteReadStream)
+ "\n"); Write data to console buffer



}


But see the MSDN Library for more information
about the used Classes.


Regards

Kerem


--
--
-----------------------
Beste Grüsse / Best regards / Votre bien devoue
Kerem Gümrükcü
Microsoft Live Space: http://kerem-g.spaces.live.com/
Latest Open-Source Projects: http://entwicklung.junetz.de
-----------------------
"This reply is provided as is, without warranty express or implied."

markgoldin said:
I am looking for code to listen to a tcp port. Another (not .net) process
will be writing to that port on the same computer and I need c# code to be
able to get data, which is a simple string, from that port. I am not a c#
coder but can easy adapt working sample.
Thanks alot for help.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top