Click here to Skip to main content
15,887,746 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I'd like to ask how to code when i want to send request to the server from the client and than server sends me answer. I've been trying send message from the client e.g. "Hi" and the server receives it and than the server sends the same message to the client. Do you know how to do it?

What I have tried:

Server

C#
IPAddress ip = Dns.GetHostEntry("localhost").AddressList[0];            
            TcpListener server = new TcpListener(ip, 8080);
            TcpClient client = default(TcpClient);

            try
            {
                server.Start();
                Console.WriteLine("Server started...");
                //Console.Read();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
                //Console.Read();
            }            

            //while (true)
            //{
                client = server.AcceptTcpClient();

                byte[] buffer = new byte[100];
                NetworkStream stream = client.GetStream();

                stream.Read(buffer, 0, buffer.Length);

                string msg = Encoding.Default.GetString(buffer);

                msg += "Ano";

            //StringBuilder msg = new StringBuilder();

            //foreach (byte b in buffer)
            //{
            //    if (b.Equals(00))
            //    {
            //        break;
            //    }
            //    else
            //        msg.Append(Convert.ToChar(b).ToString());
            //}


            IPEndPoint ipClient = new IPEndPoint(IPAddress.Any, 8080);
            Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

            socket.Bind(ipClient);
            socket.Listen(10);
            Console.WriteLine("Waiting for a client...");
            Socket clientClient = socket.Accept();
            IPEndPoint clientep = (IPEndPoint)clientClient.RemoteEndPoint;
            Console.WriteLine("Connected with {0} at port {1}", clientep.Address, clientep.Port);

            string welcome = "Welcome";
            byte[] data = new byte[1024];
            data = Encoding.ASCII.GetBytes(welcome);
            clientClient.Send(data, data.Length, SocketFlags.None);

            Console.WriteLine("Disconnected from {0}", clientep.Address);
            client.Close();
            socket.Close();


Client

C#
TcpClient client = new TcpClient(serverIP, port);

            int byteCount = Encoding.ASCII.GetByteCount(textBoxMessage.Text);

            byte[] sendData = new byte[byteCount];

            sendData = Encoding.ASCII.GetBytes(textBoxMessage.Text);

            NetworkStream stream = client.GetStream();

            stream.Write(sendData, 0, sendData.Length);

            IPEndPoint ip = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 9999);

            Socket server = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

            try
            {
                server.Connect(ip);
            }
            catch (SocketException ex)
            {
               
                return;
            }

            byte[] data = new byte[1024];
            int receivedDataLength = server.Receive(data);
            string stringData = Encoding.ASCII.GetString(data, 0, receivedDataLength);
            label1.Text = stringData;

            stream.Close();
            client.Close();
Posted
Updated 27-Sep-17 2:46am

1 solution

You are doing that correct, what you need to add here is some sort of a middleware that takes that question and generates an answer for that. Since you have not shared any further information it is very difficult for us to answer. But, that will be something like this,

C#
stream.Read(buffer, 0, buffer.Length);
 
string question = Encoding.Default.GetString(buffer);

// Your middleware generates the answer
var answer = Middleware.GenerateAnswer(question);

// Send the answer to the client. 

It can be anything, a simple class that takes input, processes it and generates some response. I can write that as,
C#
public class Middleware {
    public static string GenerateAnswer(string question) {
        if(question == "How are you?") {
            return "I am fine, what about you?";
        }
        // ... else
    }
}

Of course, this gets much complex with complex questions, and difficult slang-based statements, and that is where you need to discuss how to move ahead.

You can try out some libraries, if you'd like for NLP; SyntaxNet  |  TensorFlow[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900