Resolved Socket Programming

tdignan87

Well-known member
Joined
Jul 8, 2019
Messages
95
Programming Experience
Beginner
Hi
I am currently using socket programming to send commands to a device and it sends reply commands.
How can i get the console to read the reply thats returned?

So far i have

C#:
 m_socClient = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                System.Net.IPAddress remoteIPAddress = System.Net.IPAddress.Parse(DatalogicIP);
                System.Net.IPEndPoint remoteEndPoint = new System.Net.IPEndPoint(remoteIPAddress, DatalogicPort);
                m_socClient.Connect(remoteEndPoint);
                Console.WriteLine("Connection Successful");
                Console.ReadLine();

able to connect successfully and then im sending

C#:
string hostMode = "<ESC>[C";
C#:
 byte[] byData = System.Text.Encoding.ASCII.GetBytes(hostMode);
                m_socClient.Send(byData);

I should be receiving <ESC> H <CR><LF> back but how do i get console to listen, and show replies
Once i have the H back i want to be able to send another command after that

Appreciate any help (new to socketing)

Thanks
Tom
 
One more thing regarding the reset event : 5 Minutes Guide of ManualResetEvent - Read that regarding the comments I made on post 29. While there may be some other devs here who disagree with not following the default documentation, it should become clear while reading that article. Re this snipped why I do it a little differently, which I will briefly explain below :
In the above code, we initialize the ManualResetEvent with false value, that means all the threads which calls the WaitOne method will block until some thread calls the Set() method.

If we initialize ManualResetEvent with true value, all the threads which calls the WaitOne method will not block and free to proceed further.
When I am not writing test/development code, I make a habit of using the : INotifyPropertyChanged Interface (System.ComponentModel) and if my method is polling, I can call to update the get setter of the boolean for the ManualResetEvent with the value of true, and then let the property changed interface take care of initiating the reset procedure and you can ensure .set() is called upon the property changing to true; thus calls on the reset event which will free up that thread for the next polling call.

I'm not recommending for one minute that you call set(), to free up the thread. You'd be better to identify what is holding the thread hostage and identify the source of the blockage from your calling code first.

I hope you find this information useful and more importantly, that you understand what I am advising you to do.
 
Hi Sheepings thanks for taking the time to help me out mate. I am going to run through the dubug now, to see if i can identify the issue and go through the rest of your comments. I will post how i get on. Cheers man
 
So synchronous was of course best for what i was trying to do.
Managing to get it sending strings, reading strings.
Just in process now of building the HEX commands for the <ESC> <CR> <LF>
 
I need to send the data in HEX so for example.
I need to send 1b5b43 (<ESC>[C) i converted the string to bytes but its just showing as literally 1b5b43 on hercules. Anyone help?

C#:
 // string input = @"<ESC>[C";
            string input = @"1b5b43";
            byte[] encode = Encoding.UTF8.GetBytes(input);
            string result = ConnectServerAndGetResultByInput(input);
            if (string.Compare(@"<ESC>H<CR><LF>", result, true) == 0)
            {
 
A lot of your problems seems to stem from a lack of understanding of characters, strings, and encodings. 1b 4b 53 are already the hex values of the bytes you want to send.

For some reason, you made a string "1b4b53", and asked the framework to get the bytes representing that string. That would result in the hex bytes 31 62 34 62 35 33. It is these bytes that you ended up sending, and so the terminal obviously converted those bytes back to ASCII.

I suggest reviewing the ASCII table yet again like @Sheepings had told you to study last time.
 
Last edited:
Thanks for all your help. It's ok now, From reading skydivers comments i realised where i was going wrong. Appreciated
 
@Sheepings was actually directing you the right path. Maybe you just needed to hear the same thing phrased in a different way. Where as he is my good virtual twin and tell you what you need in a nice progression and with respect to the learning student, I tend to be the evil twin who will be snarky and/or sarcastic and just pump information out in a fire hose.

Most people respond better to his style as compared to mine. Something about flies, honey and vinegar... ? Sometimes though my style puts just the right amount of challenge, to make the student go "I'm smarter than that. I'll show that crazy Skydiver." Personally, I'd rather be a student in Sheepings class.
 
Maybe you just needed to hear the same thing phrased in a different way.
Or maybe they just wasn't paying attention to their own topic and what directions they were been given.
Personally, I'd rather be a student in Sheepings class.
That's very nice of you to say @Skydiver. I'm not very good at responding to posts like that about myself, lol so thank you. ?

I know this is where I am meant to say something nice back, but you know I've always had a problem with how expressive you are with your fire hose approach and when posting code examples. So when you said :
I tend to be the evil twin who will be snarky and/or sarcastic and just pump information out in a fire hose.
This is where we differ when it comes to presenting information on help forums. Were I write almost all of my own code for myself with everything conjoined (mostly with Linq and Lambda expressions) which makes it almost impossible for new beginners and intermediates to read, understand and find inline variables conjured up in methods using Lambda expressions. Which is why almost all of my code examples are written explicit and without the likes of Linq joinder and so forth -- Typically as Microsoft docs do with there examples.

The way I write code for users on a forum and how I answer questions is much more expressive than the way you do with your fire hose approach, which typically dumps the answers in-front of them but gives the user more to research.

We also differ in regards to how your code examples are often unexplained and most times without inline or external comments. So when you compare how we teach; I take into account how I started and what I found more helpful. I always found the more elaborate posts that explained a scenario or block of code to me to be much more helpful, than receiving a code dump with a block of code that I didn't understand how it worked or how it does what it does.

This often led me to ask more questions about the code I received, thus just makes you look like a help vampire when you really just want to learn more about what you've been given. So when I'm helping people on a board, I take into account how I felt when I started out many moons ago; and what types of answers I found more helpful. And on that note, I'd rather be in my class too! Lol No offence.

Anyway, as not to sidetrack despite being resolved :
Thanks for all your help. It's ok now, From reading skydivers comments i realised where i was going wrong. Appreciated

I'm still not sure what exactly was so different from what I said for you to do, that Skydiver explained clearer? Regardless, how did you resolve this issue and what was the cause if you truly did resolve it?

@tdignan87 - Please keep in mind, that I have a very high personal workload and I do try to be as diligent and explanatory in almost all of my replies on the forum. That's often hard to do with my time constraints. However I am here to help you. So if you find one of my replies doesn't quite explain or answer your topic. Tell me and I will try to elucidate on my previous replies as to try give you a better understanding on the directions I am giving you.
 
Back
Top Bottom