By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,564 Members | 1,081 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,564 IT Pros & Developers. It's quick & easy.

Listen to Port

P: n/a
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.

Jun 27 '08 #1
Share this Question
Share on Google+
10 Replies


P: n/a
read up on creating a socket listener
http://www.devarticles.com/c/a/C-Sha...g-in-C-Part-I/

Regards

John Timney (MVP)
http://www.johntimney.com
http://www.johntimney.com/blog
"markgoldin" <ma*************@yahoo.comwrote in message
news:03**********************************@microsof t.com...
>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.

Jun 27 '08 #2

P: n/a
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(byteR eadStream)
+ "\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" <ma*************@yahoo.comschrieb im Newsbeitrag
news:03**********************************@microsof t.com...
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.

Jun 27 '08 #3

P: n/a
What is a difference between listening to a socket or to a TCP port? Or it's
the same?

Thanks
"John Timney (MVP)" <xy******@timney.eclipse.co.ukwrote in message
news:Qd******************************@eclipse.net. uk...
read up on creating a socket listener
http://www.devarticles.com/c/a/C-Sha...g-in-C-Part-I/

Regards

John Timney (MVP)
http://www.johntimney.com
http://www.johntimney.com/blog
"markgoldin" <ma*************@yahoo.comwrote in message
news:03**********************************@microsof t.com...
>>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.

Jun 27 '08 #4

P: n/a


"markgoldin" <ma*************@yahoo.comwrote in message
news:05**********************************@microsof t.com...
What is a difference between listening to a socket or to a TCP port? Or
it's the same?

Thanks
"John Timney (MVP)" <xy******@timney.eclipse.co.ukwrote in message
news:Qd******************************@eclipse.net. uk...
>read up on creating a socket listener
http://www.devarticles.com/c/a/C-Sha...g-in-C-Part-I/

Regards

John Timney (MVP)
http://www.johntimney.com
http://www.johntimney.com/blog
"markgoldin" <ma*************@yahoo.comwrote in message
news:03**********************************@microso ft.com...
>>>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.

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

--

Joe Fawcett (MVP - XML)
http://joe.fawcett.name

Jun 27 '08 #5

P: n/a
>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.

--
--
-----------------------
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."

"Joe Fawcett" <jo********@newsgroup.nospamschrieb im Newsbeitrag
news:12**********************************@microsof t.com...
>

"markgoldin" <ma*************@yahoo.comwrote in message
news:05**********************************@microsof t.com...
What is a difference between listening to a socket or to a TCP port? Or
it's the same?

Thanks
"John Timney (MVP)" <xy******@timney.eclipse.co.ukwrote in message
news:Qd******************************@eclipse.net. uk...
read up on creating a socket listener
http://www.devarticles.com/c/a/C-Sha...g-in-C-Part-I/

Regards

John Timney (MVP)
http://www.johntimney.com
http://www.johntimney.com/blog
"markgoldin" <ma*************@yahoo.comwrote in message
news:03**********************************@microsof t.com...
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.

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

--

Joe Fawcett (MVP - XML)
http://joe.fawcett.name

Jun 27 '08 #6

P: n/a
On Sat, 31 May 2008 09:27:39 -0700, Kerem Gümrükcü <ka*******@hotmail.com>
wrote:
>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.
Well, as long as we're nitpicking... :)

For TCP over TCP/IP, a connected socket represents _two_ endpoints -- the
local endpoint and the remote endpoint -- each of which is defined by an
IP address and a port, and of course the fact that the connection is the
TCP protocol is represented as well. In other words, the connected socket
encapsulates five different pieces of information, and is uniquely
identified by those pieces of information.

Different sockets vary. A listening socket of course has only the
protocol (TCP), local IP and port, and the fact that it's listening. Just
local IP and port wouldn't be sufficient to distinguish it from connected
sockets on the same port. And of course UDP sockets are connectionless,
so the remote endpoint only affects individual datagrams, not the socket
itself.

And then, of course, the Socket class in .NET is a managed object that
provides a managed interface to all of the above. :)

Pete
Jun 27 '08 #7

P: n/a
On Sat, 31 May 2008 08:50:24 -0700, Kerem Gümrükcü <ka*******@hotmail.com>
wrote:
[...]
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(byteR eadStream)
+ "\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
Jun 27 '08 #8

P: n/a
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" <Np*********@nnowslpianmk.comschrieb im Newsbeitrag
news:op***************@petes-computer.local...
On Sat, 31 May 2008 08:50:24 -0700, Kerem Gümrükcü <ka*******@hotmail.com>
wrote:
>[...]
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(byte ReadStream)
+ "\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

Jun 27 '08 #9

P: n/a
ACK.
Cheers

K.

--
-----------------------
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" <Np*********@nnowslpianmk.comschrieb im Newsbeitrag
news:op***************@petes-computer.local...
On Sat, 31 May 2008 09:27:39 -0700, Kerem Gümrükcü <ka*******@hotmail.com>
wrote:
>>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.

Well, as long as we're nitpicking... :)

For TCP over TCP/IP, a connected socket represents _two_ endpoints -- the
local endpoint and the remote endpoint -- each of which is defined by an
IP address and a port, and of course the fact that the connection is the
TCP protocol is represented as well. In other words, the connected socket
encapsulates five different pieces of information, and is uniquely
identified by those pieces of information.

Different sockets vary. A listening socket of course has only the
protocol (TCP), local IP and port, and the fact that it's listening. Just
local IP and port wouldn't be sufficient to distinguish it from connected
sockets on the same port. And of course UDP sockets are connectionless,
so the remote endpoint only affects individual datagrams, not the socket
itself.

And then, of course, the Socket class in .NET is a managed object that
provides a managed interface to all of the above. :)

Pete

Jun 27 '08 #10

P: n/a
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ü" <ka*******@hotmail.comwrote in message
news:u5**************@TK2MSFTNGP06.phx.gbl...
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(byteR eadStream)
+ "\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" <ma*************@yahoo.comschrieb im Newsbeitrag
news:03**********************************@microsof t.com...
>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.


Jun 27 '08 #11

This discussion thread is closed

Replies have been disabled for this discussion.