开发者

c# hamachi application

开发者 https://www.devze.com 2023-03-15 07:37 出处:网络
I created an application with server and client tool using sockets etc. When using my code on my computer works. Now I installed the Himachi software and I need to use this software in my application

I created an application with server and client tool using sockets etc. When using my code on my computer works. Now I installed the Himachi software and I need to use this software in my application so that when a user connects with me, the application created could be used in this network. Note that this is my first time using sockets. The problem is that they are not connecting to each other and also it gives me this error on changing the ip and port: The requested address is not valid in its context

The send Tool

public Send(string Group, string port, string ttl, string rep, string data) 
{
 IPAddress ip;
 try
        {
            Console.WriteLine("Send on Group: {0} Port: {1} TTL: {2}", Group,port,ttl);

            ip = IPAddress.Parse(Group);

            Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);

            s.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.AddMembership, new MulticastOption(ip));

            s.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.MulticastTimeToLive, int.Parse(ttl));

            IPEndPoint ipep = new IPEndPoint(IPAddress.Parse(Group),int.Parse(port));

            Console.WriteLine("Connecting...");

            s.Connect(ipep);

            byte[] byData = System.Text.Encoding.ASCII.GetBytes(data);
            s.Send(byData, SocketFlags.None);

            Console.WriteLine("Closing Connection...");
            s.Close();
        } 
        catch(System.Exception e) { Console.Error.WriteLine(e.Message); }
    }

The Receive tool

public string RecvData(string Group, string port) 
    {
        string str = "";

        Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);

        IPEndPoint ipep = new IPEndPoint(IPAddress.Any, int.Parse(port));
        s.Bind(ipep);

        IPAddress ip = IP开发者_运维技巧Address.Parse(Group);

        s.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.AddMembership, new MulticastOption(ip,IPAddress.Any));

        // Getting the data
        byte[] buffer = new byte[1024];
        int iRx = s.Receive(buffer);

        str = System.Text.Encoding.ASCII.GetString(buffer, 0, buffer.Length);

        // Closing a Socket
        s.Close();

        return str;
    }

Thanks


So your problem is that you are trying to use a VPN (hamachi) to connect two apps the server and the client so that the client can receive messages from the server right? I think that the error given "The requested address is not valid in its context" is because you are using a VPN but I don't know how this can be solved sorry. What I think is that maybe you might also need the network id and password but again I'm not sure. Please keep us informed because this is a very interesting question.

0

精彩评论

暂无评论...
验证码 换一张
取 消