tags:

views:

115

answers:

4

Hi,

I'm a bit of a network n00b, so please be gentle and explain things in a really, REALLY dummy way (it seems to me that every time it comes to network related stuff, people start talking a totally different language). I'm a fairly experienced C# programmer, but lacks some skill when it comes to communication between machines.

The scenario is this: I'm working with a product that communicates with other devices through tcp/ip. Is it possible to make a dummy program that acts like ta tcp/ip connection (locally on my machine), so I can hook up my other program to it by setting its IP address (and port), and then have it return whatever mocking/testing data I want?

A: 

Yes: you can setup a TCP connection to a program running on your own machine. The host name you should use is localhost, or the IP address is 127.0.0.1: see also Loopback (Virtual Network Interface).

ChrisW
A: 

Yep, entirely possible. Take a look at the System.Net.Sockets.TcpClient and System.Net.Sockets.TcpListener classes - they should be a good starting point.

Stuart Davies
+1  A: 

To be honest I don't see the point of 'mocking' anything here just use the proper TCP/IP connection, because obviously you can send the data over TCP/IP between 2 different applications on the same IP.

Check out this example:

http://www.codeproject.com/KB/IP/tcpclientserver.aspx

rochal
It may be that @erikric is not able to run a local instance of the remote system with which which he is trying to communicate, so for development purposes is basically looking to simulate ('mock') the responses he would expect to receive back from the devices he mentions.
Stuart Davies
true, but he precisely said he uses his local machine for development so I can't imagine scenario where setting a local "sender" and local "receiver" would be a problem. He could even modify hist `hosts` file to remap calls to the IP of real server into his 127.0.0.1 instance.
rochal
I'm not sure I follow @rochal, but I suspect we may have different interpretations of the question he was asking. I approached it from the direction that the OP was not considering mocking the connection itself, just the data returned to simulate a connection to a remote device, and his intention was to develop a server process to be run locally on the same machine as his client process. I could be wrong though :)
Stuart Davies
Great example, just what I needed :)
erikric
A: 

I have done this before. I have created a set of tools for Mocking various technologies that you can download at:

http://www.codeproject.com/KB/biztalk/Excellence.aspx

This allows you to setup a data to be sent and received. You might have to tweak it a bit but main stuff is already there.

I believe you are interested in TcpReceiveMiniServer.

Here is a mini-sample:

    static void Main(string[] args)
    {
        TcpReceiveMiniServer server = new TcpReceiveMiniServer(8789);
        server.Start();
        server.DataArrived += new TcpReceiveMiniServer.DataArrivedHandler(server_DataArrived);
        Console.Read();
    }

    static void server_DataArrived(object sender, DataArrivedEventArgs e)
    {
        // do something with e.Data
    }
Aliostad