How can I make a TCP-IP communication between 2 Visual C# apps?
I just need to know how can I send a message from a server to a client, if the communication could be bidirectional it would be per开发者_如何学Pythonfect, but it is not necessary.
One easy way would be to use Sockets to accomplish this. A good reference for this is:
http://msdn.microsoft.com/en-us/library/system.net.sockets.socket.aspx
It has little overhead and can be configured much more simply than Remoting or other types of communication.
Consider using .net remoting over tcp/ip. This makes bi-directional communication a breeze. You'll need a TcpServerChannel on one side, a TcpClientChannel on the other side. The server side will have an object that extends MarshalByRefObject.
public class MyServerClass : MarshalByRefObject
{
public override void InitializeLifetimeService ()
{
return null;
}
public string SendAndRecieve (string message)
{
return message + message;
}
}
- WCF nettcpbinding from .net 3.5 .Net Remouting for .net 2
- TcpListener
- Sockets
Try to google for examples, there are many examples with this
Another approach would be to use the System.Messaging namespace and exchange messages over MSMQ. Not sure of your requirements, so that may not be the best approach, but it is another way to quickly get messages between 2 .Net processes.
For VS 2005/.NET 2.0 try .NET Remoting
Edit
Unless you enjoy the exercise I would advise against sockets. .NET Remoting allows for TCP, UDP, IPC communication and to your code it just looks like you are calling a method/property on an object. Also, any data structure that is serializable can be passed over the wire which lets you use rich data representation, rather than packaging/parsing byte streams at the socket level.
精彩评论