-1

I am obviously new to TCP servers. The code below works just fine - It "only" echoes the messages it receives. But my question is "simple": How can I send responses to my client - other than simply echoing the request as I do below? For instance, if I wanted to send data back (specifically for me, "OFML" data in XML like form for criminal justice end-users).

But I'd settle for "Hello world!"!

All my attempts to do this result in my client crashing (the proprietary code of which I cannot share) - and some customized error messages like, "NO Packet Found".

Any suggestions would be most appreciated - or references to some clear documentation on how to accomplish this.

Oh - and I might add I am simply trying to create a simple "mock" server for local debugging of the client - i.e this will never be for "production". Thanks!

using System.Drawing;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Net.NetworkInformation;
using System.Threading;


namespace FoxTalkMOCK
{
    class Program
    {
        public static void Main()
        {
            TcpListener server = null;
            try
            {
                Int32 port = 8080;
                IPAddress localAddr = IPAddress.Parse("10.116.45.49");
 
                server = new TcpListener(localAddr, port);
 
                server.Start();

                // Buffer for reading data
                Byte[] bytes = new Byte[18];
                String data = null;

                // Enter the listening loop.
                while (true)
                {
                    Console.Write("Waiting for a connection... ");
 
                    TcpClient client = server.AcceptTcpClient();
                    Console.WriteLine("Connected!");
                    data = null;
                    // Get a stream object for reading and writing
                    NetworkStream stream = client.GetStream();                   
                    int i;                   
                    // Loop to receive all the data sent by the client.
                    try
                    {
                    while ((i = stream.Read(bytes, 0, bytes.Length)) != 0)
                    {
                            // Translate data bytes to a ASCII string.
                            data = System.Text.Encoding.ASCII.GetString(bytes, 0, i);
                        Console.WriteLine("Received: {0}", data);

                        // Process the data sent by the client.
                        data = data.ToUpper();

                            string bitString = BitConverter.ToString(bytes);
                            bitString = bitString.Replace("-", ", 0x");
                            bitString = "0x" + bitString;
                            Console.WriteLine(bitString);

                            // *******************Send response*********************
                            stream.Write(bytes, 0, bytes.Length);
                            Console.WriteLine("Sent: {0}", System.Text.Encoding.ASCII.GetString(bytes, 0, bytes.Length));
                        }
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine(e.Message);
                    }
                    // Shutdown and end connection
                    client.Close();
                }
            }
            catch (SocketException e)
            {
                Console.WriteLine("SocketException: {0}", e);
            }
            finally
            {
                // Stop listening for new clients.
                server.Stop();
            }
            Console.WriteLine("\nHit enter to continue...");
            Console.Read();
        }
    }
}```

user1836609
  • 75
  • 2
  • 10

2 Answers2

0

The example connects and receives data, then sends that data back and disconnects.

If you want to keep the socket, keep it without breaking.

Whenever you save it and write it again, you can manage whether the socket is maintained through exception handling.

HGSD
  • 1
  • 2
  • Actually the inner "while" keeps things going - with console showing things like, "Waiting for a connection... Connected! Received: ? ?U $ ☺CY ☺ Sent: ? ?U $ ☺CY ☺ Received: ?? ? NB64CRLFU? ? Sent: ?? ? NB64CRLFU? ?" Again - my biggest issue is how to send back something other than just echoing what I have received ... Thanks! – user1836609 Feb 28 '21 at 20:42
  • 1
    stream.Write(bytes, 0, bytes.Length); -> instead of bytes, input data you want. – HGSD Mar 01 '21 at 04:12
0

The answer to simply returning a "hello world" is as follows:

var strBuffer = "Hello World!";
byte[] array = new byte[strBuffer.Length];
array = System.Text.Encoding.ASCII.GetBytes(strBuffer);
stream.Write(array, 0, array.Length)
user1836609
  • 75
  • 2
  • 10