Raheel Sadiq
Raheel Sadiq

Reputation: 9867

cocoaasynsocket to c sharp server

I want to communicate between iPhone and C# server through Sockets, When I try to create socket from iPhone to my C Sharp server over a LAN using cocoaasynsockets, but it does not create any socket on my server. I tried giving different IPs like 127.0.0.1/192.168.2.102(local)/IP found on (whatispmyip.com), but no gain,

here is my testing code for iPhone

- (IBAction)connect:(id)sender{
GCDAsyncSocket *socket;
socket = [[GCDAsyncSocket alloc] initWithDelegate:self delegateQueue:dispatch_get_main_queue()];
NSError *err = nil;

if (![socket connectToHost:@"192.168.2.102" onPort:52523 error:&err]) // Asynchronous!
{
    NSLog(@error : %@"", err);
}

}


- (void)socket:(GCDAsyncSocket *)sender didConnectToHost:(NSString *)host port:(UInt16)port
{
NSLog(@"connected");
}

and here is my c sharp server code

TcpListener _tcpListerner;

    public void startingServer() {

       // IPAddress ipad = Dns.Resolve("localhost").AddressList[0];

        IPAddress ipad = IPAddress.Parse("192.168.2.102");
         _tcpListerner = new TcpListener(ipad, 52523);


         _tcpListerner.Start();

        Console.WriteLine("The local End point is  :" +_tcpListerner.LocalEndpoint);
        Console.WriteLine("Waiting for a connection.....");


        Thread thread = new Thread(acceptClients);
        thread.Start();

    }

    void acceptClients()
    {


        Socket s = _tcpListerner.AcceptSocket();
        Console.WriteLine("Connection accepted from " + s.RemoteEndPoint);


        byte[] b = new byte[100];
        int k = s.Receive(b);

        char cc = ' ';
        string test = null;
        Console.WriteLine("Recieved...");
        for (int i = 0; i < k - 1; i++)
        {
            Console.Write(Convert.ToChar(b[i]));
            cc = Convert.ToChar(b[i]);
            test += cc.ToString();
        }

What I am missing in it, what IP should I give, or there is some settings on server side or on iphone(client side) to accept/create socket from network

Upvotes: 1

Views: 391

Answers (1)

Nikolai Fetissov
Nikolai Fetissov

Reputation: 84239

Use IPAddress.Any on the server to listen on all machine addresses. On the client side use the LAN address of the server you want to connect to.

Upvotes: 1

Related Questions