Sunday, August 2, 2009

Is there a tutorial about how I can program a service that listens for queries on a specific port in C#?

I'm thinking something like a database service, which listens to queries on its port, then returns the results of these queries to its client. It would be best if the tutorial (or your answer) showed only how to receive queries, and send responses. No need for the client code - I've built one on Linux and now I want to establish a communication with the server on Windows - although I won't mind if there is a client as well.





So, a simple "hello world" server would suffice - just to receive some data (request) and maybe put it in a text box or something, and send something (response) back. Nothing too fancy.





Thanks.

Is there a tutorial about how I can program a service that listens for queries on a specific port in C#?
use the TcpListener object ..





System.Net.Sockets.TcpListener server = new System.Net.Sockets.TcpListener(2048); // port number ..





server.Start();





TcpClient client = server.AcceptTcpClient();





Byte[] sendBytes = Encoding.ASCII.GetBytes("Hello World!");





client.GetStream().Write(sendBytes, 0, sendBytes.Length);





....

floral

No comments:

Post a Comment