1

I want to connect server from my pc. but its not working. its always showing error. I also want to send a message to server from client side so please help me what should i do?

The system detected an invalid pointer address in attempting to use a pointer argument in a call

My code is :

public partial class Form1 : Form
    {
        Socket socket;
        IPAddress ipAddress;
        string address = "192.168.1.203";
        public Form1()
        {
            InitializeComponent(); 
        }

        private void btnStartServer_Click(object sender, EventArgs e)
        {
            SocketPermission socketPermission = new SocketPermission(NetworkAccess.Connect, TransportType.Tcp, "", SocketPermission.AllPorts);
            socketPermission.Demand();
            IPHostEntry ipHOst = Dns.GetHostEntry("");
            ipAddress = ipHOst.AddressList[0];
            IPEndPoint ipEndPoint = new IPEndPoint(IPAddress.Parse(address), 4532);
            socket = new Socket(ipAddress.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
            socket.NoDelay = false;
            socket.Connect(ipEndPoint);
            txtClinet1.Text = "Socket connected to " + socket.RemoteEndPoint.ToString();
        }

    }
user207421
  • 305,947
  • 44
  • 307
  • 483
Durgpal Singh
  • 11,481
  • 4
  • 37
  • 49
  • `ipAddress` is likely ending up as an IPV6 address, see http://stackoverflow.com/questions/1059526/get-ipv4-addresses-from-dns-gethostentry – Alex K. Jan 28 '15 at 12:44

1 Answers1

1

What makes you think that AddressList[0] is the one you can use? According to the DOCS you have to iterate through the list until you find the correct one.

For example:

private void button1_Click(object sender, EventArgs e)
    {
        SocketPermission socketPermission = new SocketPermission(NetworkAccess.Connect, TransportType.Tcp, "", SocketPermission.AllPorts);
        socketPermission.Demand();
        IPHostEntry ipHOst = Dns.GetHostEntry("");
        for (int i = 0; i < ipHOst.AddressList.Length; i++)
        {

            ipAddress = ipHOst.AddressList[i];
            IPEndPoint ipEndPoint = new IPEndPoint(IPAddress.Parse(address), 4532);
            socket = new Socket(ipAddress.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
            socket.NoDelay = false;
            try
            {
                socket.Connect(ipEndPoint);
                MessageBox.Show("Socket connected to " + socket.RemoteEndPoint.ToString());
                break;
            }
            catch (Exception eX)
            {
                MessageBox.Show(eX.Message);
            }
        }

    }

In my computer it connects in the third address AddressList[2];

ja_mesa
  • 1,971
  • 1
  • 11
  • 7
  • Is working fine. How can i send message to server and recived from server. Please tell me i am begginer in socket programming. i dont find any good metter about socket programming. – Durgpal Singh Jan 29 '15 at 05:08
  • To send messages between server and client, you need to use the `socket.Send()` and `socket.Receive()` methods. And what you need is a protocol so that you can interchange messages between each other. I'd suggest to read about TCP protocol and get an idea how it works. – ja_mesa Jan 29 '15 at 10:02