简体   繁体   中英

How to configure my server for socket

on local machine this code working but when i use for other machine it does not work.

 socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
 PEndPoint localendpoint = new IPEndPoint(IPAddress.Parse("192.168.0.102"), 6001);
            try
            {socket.Connect(localendpoint);}
            catch
            {
                Console.Write("unable");
                Main(args);
            }
            String sms = "i;want;send;data";
            byte[] data = Encoding.ASCII.GetBytes(sms);
            socket.Send(data);
            Console.Read();
            socket.Close();    }

my server side:

 socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
 socket.Bind(new IPEndPoint(IPAddress.Parse("192.168.0.102"),6001));
 socket.Listen(100);
 Socket acepted = socket.Accept();

Actual i want configure other machine i used this same code with difrent IP but do not find . other machine IP is 192.168.0.102 how to configure that is located in other place ?

Just listen on the IPAddress.Any IP, so the server will listen to requests to literally any address it's assigned. This will allow you to connect from any network.

If you only want to let programs running on the same computer to be able to connect, use IPAddress.Loopback , which will make your socket listen on 127.0.0.1 .

Using IPAddress.Any may expose you to all kinds of security risks as you will be listening on all NIC cards on your machine, including the internet & intranet one. Instead, use IPAddress.Loopback for your primary LAN NIC card. Or instead, store the ip-address of your desired NIC card in a database or config file.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM