Click here to Skip to main content
15,890,579 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello Everyone!

I have following TCP Server Socket code that will receive messages from the port 25000 and sends the response on port 25001, This is working fine.
How i can receive the response in Socket Client?
Please help me with a code snippet.

C#
using System;
using System.Net;
using System.Net.Sockets;
using System.Text;

public class SynchronousSocketListener
{
    public static string data = null;

    public static void StartListening()
    {
        byte[] bytes = new Byte[1024];
        IPEndPoint localEndPoint = new IPEndPoint(IPAddress.Loopback, 25000);

        Socket listener = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
        try
        {
            listener.Bind(localEndPoint);
            listener.Listen(10);
            while (true)
            {
                Console.WriteLine("Waiting for a connection...");
                Socket handler = listener.Accept();
                data = null;
                while (true)
                {
                    bytes = new byte[1024];
                    int bytesRec = handler.Receive(bytes);
                    data += Encoding.ASCII.GetString(bytes, 0, bytesRec);
                    if (data.IndexOf("<EOF>") > -1)
                    {
                        break;
                    }
                }
                Console.WriteLine("Text received : {0}", data);
                byte[] msg = Encoding.ASCII.GetBytes(data);

                IPEndPoint ipep = new IPEndPoint(IPAddress.Any, 25001); // EndPoint For Response
                EndPoint ep = (EndPoint)ipep;
                handler.SendTo(msg, ep); // How, I receive this Message in Client Socket?????????


                handler.Shutdown(SocketShutdown.Both);
                handler.Close();
            }

        }
        catch (Exception e)
        {
            Console.WriteLine(e.ToString());
        }

        Console.WriteLine("\nPress ENTER to continue...");
        Console.Read();
    }

    public static int Main(String[] args)
    {
        StartListening();
        return 0;
    }
}
Posted
Updated 14-Nov-15 1:44am
v2
Comments
Richard MacCutchan 14-Nov-15 8:28am    
Where is the messsage that is being sent, being sent to?
Awesh Vishwakarma 14-Nov-15 8:30am    
Please find these lines...

IPEndPoint ipep = new IPEndPoint(IPAddress.Any, 25001); // EndPoint For Response
EndPoint ep = (EndPoint)ipep;
handler.SendTo(msg, ep); // How, I receive this Message in Client Socket?????????

1 solution

C#
private void ReadResponse()
       {
           IPEndPoint ep = new IPEndPoint(IPAddress.Any, 25001);
           EndPoint epp = (EndPoint)ep;
           int count = 0;
           byte[] recBuf = new byte[1024];
           int rec = requestSocket.ReceiveFrom(buffer, ref epp);
           byte[] data = new byte[rec];
           Array.Copy(buffer, data, rec);
           MethodInvoker action = delegate
           {
               labelResponse.Text = Encoding.ASCII.GetString(data);
           };
           labelResponse.BeginInvoke(action);
       }
 
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