Read-only archive of the All About Symbian forum (2001–2013) · About this archive

Problem in connecting to a server socket

0 replies · 1,862 views · Started 01 August 2006

I implemented a server socket and it is possible to connect to that from a client in the same emulator by getting the local IP from code and then opening a connection. However it is not possible to connect to the server from a client using the IP address of the emulator. Does anyone have an idea on what is going on here? I am really stuck here.

Starting the server is done in this method.

void CTCPServer::StartL( TRequestStatus& aObserverRequestStatus, TUint aPort )
{
// For reporting request completion
iStatusObserver = &aObserverRequestStatus;

// Open host resolver for determining local address...
RHostResolver HR;
User::LeaveIfError( HR.Open( iSocketServer, KAfInet, KProtocolInetTcp ) );
TNameEntry Result;
HR.GetByName(KNullDesC, Result );
HR.Close(); // ... done; close it

// Set the server address and port
TInetAddr ServerAddr = Result().iAddr;
//TUint32 KInetAddr = INET_ADDR(128,223,6,226);
//TInetAddr ServerAddr = TInetAddr(KInetAddr, 4444);
ServerAddr.SetPort( aPort );

// Open listening socket for TCP/IP protocol
User::LeaveIfError( iListeningSocket.Open( iSocketServer, KAfInet, KSockStream, KProtocolInetTcp ) );

// Bind listening socket to the server address
User::LeaveIfError( iListeningSocket.Bind( ServerAddr ) );

// Listen to a number of incoming connections equal to KListeningQueSize
User::LeaveIfError( iListeningSocket.Listen( KListeningQueueSize ) );

iAcceptedSocket.Close(); // Close old connection - if any

// Open a blank socket for accepting an incoming connection
User::LeaveIfError( iAcceptedSocket.Open( iSocketServer ) );

// Combine the listening socket with the accepting socket for accepting
// any incoming connection
iListeningSocket.Accept( iAcceptedSocket, iStatus );


// Indicate that a request was issued and is now outstanding.
SetActive();

// Display IP address to end user
TBuf<100> IPAddr;
ServerAddr.Output( IPAddr );

CEikonEnv* eikEnv = CEikonEnv::Static(); // Cache the Eikon pointer, as Static() is slow
HBufC* IPAddressMessage =eikEnv->AllocReadResourceLC(R_TCPCONNECTION_IP_MESSAGE);
eikEnv->InfoWinL(*IPAddressMessage, IPAddr );
CleanupStack::PopAndDestroy(IPAddressMessage);
}

------------------
Making a client connection is done here.

void CSocketsEngine::ConnectL( TUint32 aAddr )
{
// Initiate attempt to connect to a socket by IP address
if ( iEngineStatus == ENotConnected )
{
// Open a TCP socket
User::LeaveIfError( iSocket.Open( iSocketServ,
KAfInet,
KSockStream,
KProtocolInetTcp ) );

// Set up address information
iAddress.SetPort( iPort );
iAddress.SetAddress( aAddr );


//set the local IP and port 4444 --------------------

RSocketServ iSocketServer;
User::LeaveIfError( iSocketServer.Connect() );

RHostResolver HR;
User::LeaveIfError( HR.Open( iSocketServer, KAfInet, KProtocolInetTcp ) );
TNameEntry Result;
HR.GetByName(KNullDesC, Result );
HR.Close(); // ... done; close it

// Set the server address and port
TInetAddr ServerAddr = Result().iAddr;
TUint aPort = 4444;
ServerAddr.SetPort( aPort );


//add end -----------------

// Initiate socket connection
//iSocket.Connect( iAddress, iStatus );
//changed code------------------
iSocket.Connect( ServerAddr, iStatus );
//change end-------------------
ChangeStatus( EConnecting );

// Start a timeout
iTimer->After( KTimeOut );

SetActive();
}
}

When I get the local address and then make the connection, as it is done here it works. But when the IP and port number of the emulator is passes it doesn't work. I am certain that the client is working properly and that the IP used is the correct one.

The server code should also display the IP of the server after starting it. Instead of displaying the correct IP, it displays ::1, which is loopback for IPv6.

I'm using CodeWarrior IDE and Series 60 (Symbain version 8.0a)

Any help is highly appreciated.

Regards,
Daya