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
 
Ah gotcha,
Can you help me figure out how to code reading from the device? If they send a <ESC> H <CR><LF> i can show this on the console.writeline. That way i know that is what i received, then i can send the second and so on.
 
See the synchronous client/server sample code from Microsoft.
 
I'm able to connect and send the initial command, trying to send back via hercules but not showing in the console writeline. Any ideas?. The response is not writing to the console. just sending "Test" back.
C#:
 try
                {
                    // Establish the remote endpoint for the socket. 
                    // The name of the   
                    // remote device is "host.contoso.com". 
                    IPHostEntry ipHostInfo = Dns.GetHostEntry("192.168.4.15");
                    IPAddress ipAddress = ipHostInfo.AddressList[0];
                    IPEndPoint remoteEP = new IPEndPoint(ipAddress, port);

                    // Create a TCP/IP socket. 
                    Socket client = new Socket(ipAddress.AddressFamily,
                        SocketType.Stream, ProtocolType.Tcp);

                    // Connect to the remote endpoint. 
                    client.BeginConnect(remoteEP,
                        new AsyncCallback(ConnectCallback), client);
                    connectDone.WaitOne();

                    // Send test data to the remote device. 
                    Send(client, "Hello World<EOF>");
                    sendDone.WaitOne();

                    // Receive the response from the remote device. 
                    Receive(client);
                    receiveDone.WaitOne();

                    // Write the response to the console. 
                    Console.WriteLine("Response received : {0}", response);

                    // Release the socket. 
                    client.Shutdown(SocketShutdown.Both);
                    client.Close();

                }
                catch (Exception e)
                {
                    Console.WriteLine(e.ToString());
                }
            }
 
If i send the console to send a <CR><LF> do i need to do this as ASCII Hex chars? or will it work with <CR><LF> in the string???

Cheers
 
You need to send the binary values for those ASCII code points. How those binary values are represented in your code varies. In C#, the compiler will take: "\r\n" and convert them to the correct binary values.
 
Awesome, - I added in the \r\n and it's done it. For my reply i am expecting a <ESC>H<CR><LF> How can i add this to my if statement?
Im trying to go it against the byresRec but i know something is not right. Sorry i am a newb to socketing...


C#:
 try
                {
                    sender.Connect(remoteEndPoint);

                    Console.WriteLine("SVS Connected To Datalogic {0}",
                        sender.RemoteEndPoint.ToString());
                    Console.ReadLine();
                    // Encode the data string into a byte array. 
                    byte[] msg = Encoding.ASCII.GetBytes("Message Sent Test \r\n");
                    // Send the data through the socket. 
                    int bytesSent = sender.Send(msg);



                    // Receive the response from the remote device. 
                    int bytesRec = sender.Receive(bytes);
                    Console.WriteLine("{0}",
                        Encoding.ASCII.GetString(bytes, 0, bytesRec));
                    
                  
                    Console.ReadLine();
                    if (bytesRec = "   \r\n")
 
Thanks. It's the other program (external software)that has it in the reply command. I need to read the response and then send a second command depending if I get a reply.
 
Back
Top Bottom