×

Discussion Board

Results 1 to 6 of 6
  1. #1
    Registered User
    Join Date
    Mar 2007
    Posts
    11

    SOCKET PROGRAMMING: Client can not connect to server...

    SOCKET PROBLEM:

    Hello fellas;
    I have problem with client-server communication. I run server and client which are seperate programs, on seperate emulators. I give 127.0.0.1:4540 to both client and server as address.
    Server says "listening", then i run client.
    The outputs are:

    Server output:

    Socket opened...
    Socket binded...
    Listening...

    Client output:

    Socket opened...
    Error connecting socket...
    press any key

    What could the error be about? Any help please?I have spent long hours rewriting the code and trying to find out why client does not connect to server.

    The source codes for server and client are as follows:

    SERVER:
    ////////////////////////////////////////////////////////////////////////////////////////
    #include <in_sock.h>
    #include <e32cons.h>
    #include <es_sock.h>

    CConsoleBase* console;

    void MainL()
    {
    RSocketServ socketServ;
    RSocket listener;
    RSocket blank;
    socketServ.Connect();
    CleanupClosePushL(socketServ);//if function leaves ensure socket serv session closes

    TInetAddr addr;
    addr.SetAddress(INET_ADDR(127,0,0,1));
    addr.SetPort(4540);

    TInt errno;
    errno=listener.Open(socketServ,KAfInet,KSockStream, KProtocolInetTcp);
    if(errno!=KErrNone) console->Write(_L("Error opening socket...\n"));
    else console->Write(_L("Socket opened...\n"));

    errno=listener.Bind(addr);
    if(errno!=KErrNone) console->Write(_L("Error binding socket...\n"));
    else console->Write(_L("Socket binded...\n"));

    TRequestStatus status;
    errno=listener.Listen(1);
    if(errno!=KErrNone) console->Write(_L("Error listening...\n"));
    else console->Write(_L("Listening...\n"));

    blank.Open(socketServ);
    listener.Accept(blank, status);
    User::WaitForRequest(status);
    if(status != KErrNone) User::Leave(KErrGeneral);

    console->Write(_L("\n\n----------------\npress any key...\n"));
    console->Getch();
    CleanupStack::Pop(&socketServ);
    socketServ.Close();
    }


    void ConsoleMainL()
    {
    console = Console::NewL( _L("Socket Server"), TSize(KConsFullScreen, KConsFullScreen) );
    CleanupStack::PushL(console);
    MainL();
    CleanupStack::PopAndDestroy(console);
    }


    TInt E32Main()
    {
    __UHEAP_MARK;
    CTrapCleanup* cleanupStack = CTrapCleanup::New();
    TRAPD(err, ConsoleMainL());
    delete cleanupStack;
    __UHEAP_MARKEND;
    return KErrNone;
    }
    ////////////////////////////////////////////////////////////////////////////////////////////////




    and CLIENT
    ////////////////////////////////////////////////////////////////////////////////////////////////
    // Include Files
    #include "client.h"
    #include <es_sock.h>
    #include <ss_std.h>
    #include <in_sock.h>
    #include <e32base.h>
    #include <e32std.h>
    #include <e32cons.h> // Console

    // Constants
    _LIT(KTextConsoleTitle, "Console");
    _LIT(KTextFailed, " failed, leave1 code = %d");
    _LIT(KTextPressAnyKey, " [press any key]\n");
    _LIT(KName,"Fatih\n");

    // Global Variables
    LOCAL_D CConsoleBase* console; // write all messages to this

    // Local Functions
    LOCAL_C void MainL()
    {
    RSocketServ socketServ;
    RSocket client;
    socketServ.Connect();
    CleanupClosePushL(socketServ);//if function leaves ensure socket serv session closes

    TInetAddr addr;
    addr.SetAddress(INET_ADDR(127,0,0,1));
    addr.SetPort(4540);

    TInt errno;
    errno=client.Open(socketServ,KAfInet,KSockStream, KProtocolInetTcp);
    if(errno!=KErrNone) console->Write(_L("Error opening socket...\n"));
    else console->Write(_L("Socket opened...\n"));

    TRequestStatus status;
    client.Connect(addr,status);
    if(status!=KErrNone) console->Write(_L("Error connecting socket...\n"));
    else console->Write(_L("Socket connected...\n"));

    console->Write(KTextPressAnyKey);
    console->Getch();
    CleanupStack::Pop(&socketServ);
    socketServ.Close();
    }

    LOCAL_C void DoStartL()
    {
    console = Console::NewL( _L("Socket Client"), TSize(KConsFullScreen, KConsFullScreen) );
    CleanupStack::PushL(console);
    MainL();
    CleanupStack::PopAndDestroy(console);
    }

    // Global Functions
    GLDEF_C TInt E32Main()
    {
    __UHEAP_MARK;
    CTrapCleanup* cleanupStack = CTrapCleanup::New();
    TRAPD(err, DoStartL());
    delete cleanupStack;
    __UHEAP_MARKEND;
    return KErrNone;
    }
    ///////////////////////////////////////////////////////////////////////////////////////////////

    Any ideas please?
    Thanks...

  2. #2
    Nokia Developer Moderator
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    28,692

    Re: SOCKET PROGRAMMING: Client can not connect to server...

    Try listener.SetLocalPort(4540) instead of binding to a full address.

  3. #3
    Registered User
    Join Date
    Mar 2007
    Posts
    11

    Re: SOCKET PROGRAMMING: Client can not connect to server...

    listener.setLocalPort() crashes...
    Emulator comes up with blank screen, and in a few seconds it ends up with program error. "server.exe needs to be closed" by windows.

    I declared:
    TSockAddr addr;
    addr.SetPort(4540);

    This crashes as well...

    Only:
    TInetAddr addr;
    addr.SetAddress(INET_ADDR(127,0,0,1));
    addr.SetPort(4540);

    works without error, but when i type:
    >netstat/na

    in command prompt, there is nothing shown in my apllications listening port.

    I use series60 v2.0 and carbide.vs and visual studo.net 2003

    Note: I am porting to carbide.c++ now...

  4. #4
    Nokia Developer Moderator
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    28,692

    Re: SOCKET PROGRAMMING: Client can not connect to server...

    Do not worry, Carbide will not help. However you might consider installing a newer SDK, since it might have better support for networking with the emulator.

  5. #5
    Regular Contributor
    Join Date
    Sep 2006
    Location
    Australia, NSW
    Posts
    200

    Re: SOCKET PROGRAMMING: Client can not connect to server...

    Bind to TInetAddr(KInetAddrAny, YOUR_SERVER_PORT) is server's case instead of loopback.
    Also, you forget to bind client's socket.

  6. #6
    Registered User
    Join Date
    Mar 2007
    Posts
    11

    Re: SOCKET PROGRAMMING: Client can not connect to server...

    As i have a past with ANSI C Socket programming i thought client sockets are not binded.
    I tried KInetAddrAny as server address.
    It seems to run but again i cant see my server when i type netstat in comand prompt.

Similar Threads

  1. Question about bluetooth application (one client and server in the same device)
    By gonzi in forum Mobile Java Networking & Messaging & Security
    Replies: 3
    Last Post: 2008-07-24, 12:01
  2. Send multiple message from client to server
    By Palermo4Ever in forum Mobile Java Networking & Messaging & Security
    Replies: 2
    Last Post: 2006-06-19, 11:49
  3. Replies: 0
    Last Post: 2005-09-16, 16:38
  4. [Bluetooth] Client and server at the same time?
    By titan_ordalie@yahoo.fr in forum Mobile Java Networking & Messaging & Security
    Replies: 0
    Last Post: 2005-08-26, 20:03
  5. http error to connect to Activ server
    By supriya_nandi in forum WAP Servers
    Replies: 2
    Last Post: 2002-11-06, 09:32

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
×