0

I have this program and it is sending a string to a different socket on the same computer every 45 seconds. Below is the function which send a string over a socket using the built in Socket class of C#.

Issue I am having is that when I run this program and give 127.0.0.1 I am not getting any responses back from the socket. But when I run the program on a different computer using the ipaddress of the computer then it works flawlessly.

So I questioned if allowed to pass in 127.0.0.1 as the ip for the Socket class and have it recognize that I want to send data to a different socket on the same computer. Do I have to do anything different to make sure that it works on 127.0.0.1?

Thanks!

static string QueryMiner(string command)
{
    byte[] bytes = new byte[1024];
    try
    {
        //code for gettting current machines IP Use this code if client is running on the miner
        IPAddress ipAddr = IPAddress.Parse("127.0.0.1");
       //IPAddress ipAddr = IPAddress.Parse("198.xxx.xxx.xxx");
        IPEndPoint ipEndPoint = new IPEndPoint(ipAddr, 4028);

        Socket sender = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
        sender.Connect(ipEndPoint);
        Console.WriteLine("Socket connected to {0}", sender.RemoteEndPoint.ToString());

        string SummaryMessage = command;
        byte[] msg = Encoding.ASCII.GetBytes(SummaryMessage);

        sender.Send(msg);
        byte[] buffer = new byte[1024];
        int lengthOfReturnedBuffer = sender.Receive(buffer);
        char[] chars = new char[lengthOfReturnedBuffer];

        Decoder d = System.Text.Encoding.UTF8.GetDecoder();
        int charLen = d.GetChars(buffer, 0, lengthOfReturnedBuffer, chars, 0);
        String SummaryJson = new String(chars);
        sender.Shutdown(SocketShutdown.Both);
        sender.Close();
        return SummaryJson;
    }
    catch (Exception ex)
    {
        Console.WriteLine("Exception: {0}", ex.ToString());
        return ex.ToString();
    }
}
Lex Li
  • 60,503
  • 9
  • 116
  • 147
Elliot Weil
  • 183
  • 4
  • 16
  • the code runs and compiles. I am wondering if I can connect to a socket on the same computer using socket. thanks for your response! – Elliot Weil Aug 12 '13 at 22:02
  • 1
    `I am wondering if I can connect to a socket on the same computer using socket` **YES** – I4V Aug 12 '13 at 22:05
  • Sorry for looking like I wanted you to debug my program I was just giving the code for reference. Thanks for answering my question. When I said socket I mean the Socket library of C#. and I am correct in using the ip 127.0.0.1? – Elliot Weil Aug 12 '13 at 22:08
  • yes `127.0.0.1` or `localhost` means the computer you are working on... – I4V Aug 12 '13 at 22:11
  • Awesome Ill add some logging statements and figure out why its not working in this strange situation. Going to edit my original post to be more clear about what I'm looking to get answered. – Elliot Weil Aug 12 '13 at 22:14
  • _"it isn't working"_ is not an error. Don't guess (_"am I allowed to pass in 127.0.0.1 as the ip?"_), explain what exactly goes wrong. – CodeCaster Aug 12 '13 at 22:21
  • Thanks I edited the original post to be come clear about the problems I am having and the questions I am asking. – Elliot Weil Aug 12 '13 at 22:29
  • I am more concerned about the receiver side source code, not the sender side. If the receiver does not listen on the current binding (IP address + port), this could happen naturally (as @Yaur pointed out). – Lex Li Aug 13 '13 at 05:16

1 Answers1

0

The code where you set up the server portion of this (e.g. call bind) isn't here, but your problem is almost certianly that you are binding to a specific ip address and not to IPAddress.Any.

If so using your local IP rather than the loopback should work.

Yaur
  • 7,333
  • 1
  • 25
  • 36
  • There is another program running that will response If I send it a string to socket 4028. Sorry for not clarifying that. – Elliot Weil Aug 12 '13 at 22:33
  • hmm Ill try using my local IP instead of 127.0.0.1. If that works I am going to be so confused. Because I feel like I am specifying the ip and the socket the string should go straight there but its obviously not working like that. – Elliot Weil Aug 12 '13 at 22:36
  • Is there a function to return my local ip in c#? – Elliot Weil Aug 12 '13 at 22:39
  • @ElliotWeil to clarify the loopback IP is on its own interface (the loopback interface) and if the service is bound to a specific IP/interface it is absolutely expected that it will not accept connections on other interfaces. Very bad things would happen if this were not the case. – Yaur Aug 13 '13 at 00:29