-4

Simple Concept. I have a string on computer A, I want it moved to computer B, how to do it, why is it so complex :)

I am setting up a program I need to run on multiple PC's where the different nodes needs to communicate. What needs to be sent is a text string (a command object that has been serialized to a string). Some of these nodes might be "clients" behind a firewall so how do they initiate two-way communication so they can receive information back once connected up?

My expectations would be that seen outside this transfer part that you have an entry point and a pickup point. Following is how I see the pseudocode, but I simply do not understand network coding well enough to figure out how it actually works to get it right. So far my searching has left me with code I do not understand what does, or the idea behind. (see questions after code)

public void SendCommand(String commandObject, String targetService)
{
    <lookup service address>
    <connect to address>  || <connect to address if not already connected>
    <send string>
    <close connection>  || <keep connection open>
}

public List<String> GetCommands()
{
    <Grab Mutex on Outbox>
        <Copy Outbox to tmpList>
        <empty Outbox List>
    <release Mutex>
    <return tmpList>
}

??? how to handle this

    <accept connection>
    <receive string and move it to local buffer List<String> DropOff>
    <get mutex via (waitOne(0)) for access to List<String> Outbox
        <move DropOff entries to Outbox>
        <empty DropOff>
    <release Mutex>

Here my understanding fails.

*I do not understand how the Receive part of the network code is, and get the entry into the "DropOff" in a way that does not risk issues with multiple threads (hence idea of DropOff and Outbox in this code, is this already handled?)

*I do not understand how to setup a client to connect to this network layer in a way so twoway communication can occour. (Here I do understand its both computers having a listener and a way to send to the others sender, but if the listener on the Client computer is behind a firewall, how do I send replies and setup this listener once connection between the two computers have been established?)

I believe these are questions that have been asked quite a few times before, but I simply do not understand this well enough to know what to search for to get the required understanding. If you could direct me to this, and also help my understanding if my understanding of the pseudo code setup is wrong, please do so.

Taoh
  • 331
  • 2
  • 12
  • For the downvoter of this question. I have spent my due dilligence researching (and been quite unskilled at it). I truly hate having to ask this question and not figure this one out. But after having searcher for days, reading code from the Socket information on http://msdn.microsoft.com/en-us/library/kb5kfec7.aspx and still not being any wiser. I am going here for help. At least post a link or do something instead of just downvote. – Taoh Dec 12 '13 at 19:57
  • Why don't you just use a `TCPClient` and send the string? – KSdev Dec 12 '13 at 19:58
  • Got a link to it, and how to do it. This is what I am asking for. Transfer and receive a network string via Network. Looking at it, I am none the wiser, I do not understand how I set up a "receiver" nor how I return a reply to a client "behind a firewall". – Taoh Dec 12 '13 at 20:01
  • Would agree with the other comments to use a TCP server/client application. Your TCP client can listen on a port opened on your firewall. There are loads of tutorials on simple to advanced TCP applications on the internet and they are really not hard tto understand. – Andre Lombaard Dec 12 '13 at 20:14
  • If there is loads of guides (which I also state I expect there is), could you be so kind to direct me to a good one. It is a bit like telling a thirsty person, he should drink water, and there is plenty of water in the world. – Taoh Dec 12 '13 at 20:18
  • @Taoh How about going by step by step. It seems you know nothing about networking, protocols, Webservices, WCF etc... Either start to read some docs which will require some learning curve or hire someone who can do it. – L.B Dec 12 '13 at 20:18
  • You are right L.B. My knowledge level is very low, so low I do not know what to start looking for, and I am asking for help to guides that can help that, or keywords that can make me able to search for those guides. This is a hobby project, so "hiring" is not an option, also I want to learn and understand this. My knowledge level is I know of IP/Port, and that it is a TCP connection I want setup. I know I can not connect through a firewall unless a connection is initiated from the Client to the Server, and I need help to understand how to set that up. – Taoh Dec 12 '13 at 20:21
  • @Taoh I am not sure if it can help you but I commented [in this question](http://stackoverflow.com/questions/20552359/convert-web-api-to-use-self-hosting) one hour ago which contains self-contained answer. – L.B Dec 12 '13 at 20:25
  • Thanks L.B. Anything is a help. I need a "Network Coding in C# for dummies" guide :) – Taoh Dec 12 '13 at 20:27

1 Answers1

1

MSDN has a good section to review for TcpClient class. It is what I use to communicate simple strings.

Basic Concept Code:

TcpClient tcpClient = new TcpClient();
tcpClient.Connect(server, port);
Stream stream = tcpClient.GetStream();

//Send a Carriage Return
byte[] CR = Encoding.ASCII.GetBytes("\r\n");
stream.Write(CR, 0, CR.Length);

stream.Close();
tcpClient.Close();

That code would send a Carriage Return to Server at Port. Very Simple example, but the Link provided has greater depth.

PART 2: for Comment Below

// Create a TcpClient. 
TcpClient client = new TcpClient(server, port);

// Get a client stream for reading and writing. 
NetworkStream stream = client.GetStream();

// Read the message to the connected TcpServer.
Byte[] data = new Byte[5];
List<byte> dataBytes = new List<byte>();
stream.Read(data, 0, data.Length);

// Receive the Stream and Convert to Hex
foreach (byte a in data)
{
    dataBytes.Add(a);
}
string hexDatabytes = BitConverter.ToString(dataBytes.ToArray());
Console.WriteLine("Received: {0}", hexDatabytes);

This is some code I just grabbed from some old stuff I completed in the past.

KSdev
  • 621
  • 3
  • 12
  • and how do I setup the Server/Listener? – Taoh Dec 12 '13 at 20:10
  • @Taoh Added PART 2 to Answer. Also note, in my example I needed the information being received in Hex. I left it in as a guide for you to possible build off of. – KSdev Dec 12 '13 at 20:26
  • Thanks KSdev for trying, but I am just not getting this. I am missing some basic core understanding of the concept. I can see the tcpClient connnect to the server in the top part of the code. setup a Strem as the connection to the server, then you create a byte array containing the Carriage return, and you write it to the stream with a length of the message. – Taoh Dec 12 '13 at 20:42
  • then at the 2nd part I cannot figure out where the server get setup. Where is the "listener" for the connection? .... I see you read the Stream from the received message. (how do you know the Byte array, I take the Byte[5] is some kind of default? I see you now having a stream (again where did the connection occour?) read the bytes receive into a temp llst, then convert those bytes to a string. ... what if there is more communication comming, does it "clear a buffer" when you use the stream.Read? – Taoh Dec 12 '13 at 20:47
  • @Taoh Did you have a chance to review the Link I provided at the start of the Answer. Their example is probably a little more concise and better suited for what you wish to accomplish. They are converting directly to a String. It sounds like in your situation you would perform an act dependent upon the string received? And yes `Byte[5]` was specific to what I needed. – KSdev Dec 12 '13 at 20:50
  • I did review it, and its still quite confusing. Your answer has helped me a lot to understand what is going on better with the stream and such, and how to get that back out. Is a bit like the string part in my pseudocode, just with bytes instead, leaving me to think I will need some kind of "escape character" to seperate the different commands once the stream is setup. (both sides got the stream and can send data to it, and receive from it). Leaves me pretty much with the question of how do I setup the listener. I will try and dig deeper. – Taoh Dec 12 '13 at 20:57
  • @Taoh It is a lot to take in if you are unfamiliar but I am positive this is the approach you are looking for. I wish you the best of luck. – KSdev Dec 12 '13 at 21:14