Search code examples
c#vb.netweb-servicesasyncsocket

I just want my client to say hello to my server but can't


OK this is such a simple problem but its driving me so mad that I have had to drink a whole jug of water to calm myself down.

EDIT

Added c# in the tag too, a c# answer will be fine too.

Basically I am implementing this asynchronous client/server example here on microsoft's website http://msdn.microsoft.com/en-us/library/bbx2eya8.aspx .

Now I have a project instance running the server and the server is OK, and just waiting for a connection.

I am trying to understand the asynhronous communication model in .net and I simply want my client.vb to say hello to server who is listening locally on port 2000.

The code in the microsoft example is long and I am working to understand it bit by bit , but for now I just want a SIMPLE few lines code to say "hello" to my waiting server.

Dim ipHostInfo As IPHostEntry = Dns.Resolve(Dns.GetHostName())
    Dim ipAddress As IPAddress = ipHostInfo.AddressList(0)
    Dim localEndPoint As New IPEndPoint(ipAddress, 2000)
    clientSocket.BeginConnect("localhost", 2000, AddressOf ConnectionMade, clientSocket)

The above isn't working, I get a null pointer exception. There are so many callback, acceptcallback and other methods in microsoft example that I am sure i will understand fine soon, but for now it is incredibly confusing.

Please save me from wearing my laptop has a hat and help.Just a simple "hello" string to server and my life will be a happy one. thanks


Solution

  • I used your code to create an example, and the only way I can make it give me a null reference exception is if I don't properly instantiate the socket. But without seeing your declaration of clientSocket or the ConnectionMade method, it's hard to tell.

    IPHostEntry ipHostInfo = Dns.Resolve(Dns.GetHostName());
    IPAddress ipAddress = ipHostInfo.AddressList[0];
    IPEndPoint localEndPoint = new IPEndPoint (ipAddress, 2000);
    Socket clientSocket = new Socket(SocketType.Raw, ProtocolType.IPv4);
    clientSocket.BeginConnect("http://www.google.com", 80, new AsyncCallback(method), clientSocket);