0

I am writing a basic TCP server/Client in C# and for some reason my server is not getting the data sent to it from the Client. and i cannot figure out why this is happening

    static void Main(string[] args)
    {
        Console.WriteLine("Server Started");
        int PORT = Int32.Parse(System.Configuration.ConfigurationManager.AppSettings["Port"]);

        IPEndPoint ipep = new IPEndPoint(IPAddress.Any, PORT);
        Socket newsock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

        newsock.Bind(ipep);
        newsock.Listen(10);
        Socket client = newsock.Accept();
        while (true)
        {
            byte[] data = new byte[1024];
            int recv = client.Receive(data);
            if (recv == 0) break;
            string str = Encoding.ASCII.GetString(data, 0, recv);
            Console.WriteLine(str);
            data = Encoding.ASCII.GetBytes(str.ToUpper());
            client.Send(data, recv, SocketFlags.None);
        }
        client.Close();
        newsock.Close();
        Console.ReadLine();
    }

And The client looks like this:

        static void Main(string[] args)
    {
        Console.WriteLine("Starting Client");

        IPEndPoint ipep = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 80);
        Socket server = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

        try
        {
            server.Connect(ipep);
        }catch (SocketException e)
        {Console.WriteLine("Unable to connect to Server" + e.ToString());}


            while (true)
            {
            string input = Console.ReadLine();
            if (input == "exit") break;
            server.Send(Encoding.ASCII.GetBytes(input));
            Console.WriteLine("Sent");
            byte[] data = new byte[1024];
            int recv = server.Receive(data);
            Console.WriteLine("after client rec");
            string stringData = Encoding.ASCII.GetString(data, 0, recv);
            Console.WriteLine(stringData);
            }
            server.Shutdown(SocketShutdown.Both);
            server.Close();
        }
    }
user2955610
  • 815
  • 2
  • 9
  • 15
  • Questions like this are complex and just showing the code is unlikely to be sufficient. You need data as well. How do you know the data is not being received? How do you it is even being sent? You have those debug prints.. don't you think the people you're asking to help would like to see that output too? Wireshark is your friend when troubleshooting network communication issues like this. – Jonathon Reinhart Mar 23 '16 at 12:26
  • Look at this question, it might help: http://stackoverflow.com/questions/869744/how-to-write-a-scalable-tcp-ip-based-server – daniel59 Mar 23 '16 at 12:32
  • 1
    Maybe a stupid question, but the client always connects to port 80, which may or may not be the same as the server is listening on. If you have something else (IIS for instance) running on port 80, you may not notice that since it will seem to connect fine - but not to your server! – Lucero Mar 23 '16 at 12:34
  • It's been a long time since I last did stuff like this but some things you could try: -Check firewall settings - If you have another computer handy, run the client and the server on different computers -Use SocketTest (https://sourceforge.net/projects/sockettest) to test both the client and the server separately – HebeleHododo Mar 23 '16 at 12:42
  • I haven't used this but this is how you capture localhost traffic with Wireshark https://wiki.wireshark.org/CaptureSetup/Loopback. (Back in my day it was impossible) – HebeleHododo Mar 23 '16 at 12:47

0 Answers0