473,503 Members | 756 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Listen to Port

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
10 9324
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
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
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


"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
>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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
11398
by: Aquila Deus | last post by:
Hi all! I just tried to enable TCP/IP on SQL Server 2000 (trial, SP3a). The Server Network Utility shows it's already turned on, but after restarting the server doesn't listen to any TCP port,...
9
43075
by: mBird | last post by:
I wrote a service that listens for broadcast messages from my firewall (UDP snmp trap messages) and parses and puts the data in an database. I'd also like to write an app that is a simple form...
1
1698
by: HABJAN ®iga | last post by:
Hello, how can i bind a socket for listen on some port, and all addresses : In the sample i bind myself to localhost(127.0.0.1) on port 5500. It works fine when connecting to localhost, but if...
3
13662
by: D. André Dhondt | last post by:
In VB.NET 2003, is there a way to create a System.Net.Sockets.UDPClient to listen to any address AND any port? I can get it to listen to any address, but only if I specify a port (for example,...
1
2398
by: Marcelo | last post by:
_____ From: Marcelo Posted At: Friday, September 30, 2005 1:40 PM Posted To: microsoft.public.dotnet.languages.vb Conversation: Subject: Client and Server - Listen por packets in a specific...
1
3386
by: gregory_may | last post by:
I want to bind multiple UDPClients to the same port on the same machine. I want them to all listen for broadcast messages & respond accordingly. Multiple client bindings "under normal...
1
18098
by: ushachandran | last post by:
Hi Please let me know what is the error in the following code. I am doing a tracking project. My vehicle unit has a simcard with sms message stored on it. When the unit powers up it reads the sms...
7
3159
by: silverburgh.meryl | last post by:
Hi, I read the following code which open a server socket for client request. However, i would like to know how can I change it so that i just listen for client requestfor 10 seconds, after...
9
364
by: markgoldin | last post by:
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,...
0
7282
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
7339
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
7463
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...
1
5017
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...
0
4678
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...
0
3168
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3157
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1515
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 ...
1
738
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.