Page 1 of 2

TCP Protocols

PostPosted: Jul 9, 2007 @ 1:56pm
by coolkc
Hi ,

I needed some help with TCP protocols... I will paste some code here and I wanted to know if there is EDGE version for the code. I am using EDGE IDE for builiding and I want the code to run for Series 60(3rd edition). The code is in C and I wanted the manipulations that are to be made so that it can be used in EDGE IDE. Also I wanted to know how to send requests in TCP. Request sending is not there in the tutorial(It was there only for HTTP-- is it the same way?).

This is the code:

#include <stdio.h>
#include <winsock.h>
#include <stdlib.h>

#include "helper.h" /* Our own helper functions */

/* Global constants */

#define MAX_LINE (1000)

int ParseCmdLine(int argc, char *argv[], char **szAddress, char **szPort);

/* main() */

int main(int argc, char *argv[])
{
SOCKET conn_s; /* connection socket */
short int port; /* port number */
struct sockaddr_in servaddr; /* socket address structure */
char buffer[MAX_LINE]; /* character buffer */
char *szAddress; /* Holds remote IP address */
char *szPort; /* Holds remote port */
char *endptr; /* for strtol() */
struct hostent *he;

u_long nRemoteAddr;
WSADATA wsaData;

he=NULL;

/* Get command line arguments */

ParseCmdLine(argc, argv, &szAddress, &szPort);


/* Set the remote port */

port = strtol(szPort, &endptr, 0);
if ( *endptr )
{
printf("client: porta non riconosciuta.\n");
exit(EXIT_FAILURE);
}


if (WSAStartup(MAKEWORD(1,1), &wsaData) != 0)
{
printf("errore in WSAStartup()\n");
exit(EXIT_FAILURE);
}

/* Create the listening socket */

if ( (conn_s = socket(AF_INET, SOCK_STREAM, 0)) == INVALID_SOCKET)
{
fprintf(stderr, "client: errore durante la creazione della socket.\n");
exit(EXIT_FAILURE);
}


/* Set all bytes in socket address structure to
zero, and fill in the relevant data members */
memset(&servaddr, 0, sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_port = htons(port);

/* Set the remote IP address */
nRemoteAddr = inet_addr(szAddress);
if (nRemoteAddr == INADDR_NONE)
{
printf("client: indirizzo IP non valido.\nclient: risoluzione nome...");
if ( (he=gethostbyname(szAddress)) == 0)
{
printf("fallita.\n");
exit(EXIT_FAILURE);
}
printf("riuscita.\n\n");
nRemoteAddr = *((u_long *)he->h_addr_list[0]);
}
servaddr.sin_addr.s_addr = nRemoteAddr;



/* connect() to the remote echo server */

if ( connect(conn_s, (struct sockaddr *) &servaddr, sizeof(servaddr) ) == SOCKET_ERROR )
{
printf("client: errore durante la connect.\n");
exit(EXIT_FAILURE);
}


/* Get string to echo from user */

printf("Inserire la stringa da spedire: ");
fgets(buffer, MAX_LINE, stdin);

/* Send string to echo server, and retrieve response */

Writeline(conn_s, buffer, strlen(buffer));
memset(buffer, 0, sizeof(char)*(strlen(buffer)+1));
Readline(conn_s, buffer, MAX_LINE-1);


/* Output echoed string */

printf("Risposta del server: %s\n", buffer);

WSACleanup();
return EXIT_SUCCESS;
}


int ParseCmdLine(int argc, char *argv[], char **szAddress, char **szPort)
{
int n = 1;

while ( n < argc )
{
if ( !strncmp(argv[n], "-a", 2) || !strncmp(argv[n], "-A", 2) )
{
*szAddress = argv[++n];
}
else
if ( !strncmp(argv[n], "-p", 2) || !strncmp(argv[n], "-P", 2) )
{
*szPort = argv[++n];
}
else
if ( !strncmp(argv[n], "-h", 2) || !strncmp(argv[n], "-H", 2) )
{
printf("Sintassi:\n\n");
printf(" client -a (indirizzo server) -p (porta del server) [-h].\n\n");
exit(EXIT_SUCCESS);
}
++n;
}
if (argc==1)
{
printf("Sintassi:\n\n");
printf(" client -a (indirizzo server) -p (porta del server) [-h].\n\n");
exit(EXIT_SUCCESS);
}
return 0;
}


Hoping to get a reply soon.

Thank you,
coolkc

PostPosted: Jul 9, 2007 @ 2:09pm
by edge

PostPosted: Jul 9, 2007 @ 3:10pm
by coolkc

PostPosted: Jul 10, 2007 @ 9:29am
by edge
Hi Coolkc,

Can you tell me why you like to run them from the DOS prompt? Which Windows version are you using?

Try to run the connection sample from the EDGELIB SDK on your Symbian phone and Windows PC to check if they can connect to each other. The connection sample should list all the basics on how to handle client/server connections and sending/receiving data.

PostPosted: Jul 10, 2007 @ 1:27pm
by coolkc
Hi,
I am using Windows XP and I was running them on DOS prompt for testing. Once it runs well then I wanted to test them on the Symbian device. But i faced the problem.

Thank you,
coolkc

PostPosted: Jul 11, 2007 @ 9:54am
by coolkc
Hi,

error: `ecd' undeclared (first use this function)

How to declare ecd and remove this error??
Do we have to use some parsing or is it pre-defined?
Please tell me how to declare ecd?

Hoping for a reply soon.

Thank you,
coolkc

PostPosted: Jul 11, 2007 @ 10:01am
by edge

PostPosted: Jul 11, 2007 @ 10:54am
by coolkc
Hi,


I will paste my code of client_main.h :

class ClassMain : public ClassEdge
{
private:
ENetStack netstack;
public:
ClassMain(const EDGEDATA *data);
~ClassMain(void);
bool Connect(bool);
void OnNetMessage(unsigned char connection, long id, long msg, long bufsize, unsigned char *buffer);
};

//ClassMain: constructor
ClassMain::ClassMain(const EDGEDATA *data) : ClassEdge(data)
{
}

ClassMain::~ClassMain(void)
{
}

bool Connect(bool usebluetooth)
{
if (usebluetooth)
{
if (ecd.net[0]->Init(ECONN_BLUETOOTH, this) == E_OK)
{
if (!ecd.net[0]->SearchHost("00:00:54:1F:8D:24", 4))
return(false);
}
else
return(false);
}
else
{
if(ecd.net[0]->Init(ECONN_SOCKET, this) == E_OK)
{
if(!ecd.net[0]->SearchHost("127.0.0.1", 4000))
return(false);
}
else
return(false);
}
}

void ClassMain::OnNetMessage(unsigned char connection, long id, long msg, long bufsize, unsigned char *buffer)
{
char testmsg[128];
switch(msg)
{
case ENET_SERVERFOUND:
netstack.Alloc(1024);
break;
case ENET_RECEIVEDATA:
if (netstack.Push(buffer, bufsize))
{
unsigned long peeksize = netstack.Peek();
if (peeksize == 128)
{
netstack.Pop(testmsg, peeksize);
}
}
break;
}
}

Kindly check for any errors(both syntactic and semantic) and suggest the changes to be made so that the program runs well. I will also paste the client.cpp code :

//Include Edge
#include "edgemain.h"

//Link the Edge static library
#pragma comment(lib, "edge.lib")

/*//Include headers and link OpenGL libraries
#if defined(EGL_USEGL)
#if defined(EGL_USEGLES)
#include "GLES\\gl.h"
#include "GLES\\egl.h"
#else
#include "GL\\gl.h"
#endif
#if defined(DEVICE_WIN32)
#if defined(EGL_PC)
#pragma comment(lib, "opengl32.lib")
#pragma comment(lib, "plugingl.lib")
#endif
#endif*/

//Contains ClassMain, the application framework
#include "client_main.h"

#if defined(DEVICE_WIN32)

//Include internal resources for native Win32 builds
include "resource.h"

#endif

MAINRET EdgeMain(EDGEDATA *data)
{
#if defined(DEVICE_WIN32)
data->appicon = IDI_MAIN;
#endif
ClassEStd::StrCpy(data->caption, "client1");
ClassMain *main = new ClassMain(data);
return(main->Start());
}

Do I have to make any changes in this code too?? Please tell me the changes.

Hoping to get a reply soon.

Thank you,
coolkc

PostPosted: Jul 11, 2007 @ 8:41pm
by edge
Hi Coolkc,

Your code seems to be correct. The next time you post code we recommend using the code tags (you can see the code button when editing your post). It colorizes and outlines your code :)

Do you still get the undeclared ECD error? Because in your code I see you're using ClassEdge. ClassEdge and ECD should be defined in the same header files.

PostPosted: Jul 12, 2007 @ 8:56am
by coolkc
Hi,

Sorry about not using code button. I did not see it.

Anyways I am still getting the same error message that undeclared identifier ecd.
I am building this code in EDGE IDE. Is that causing any problems??

I will tell of what all things I tried out. In the code I changed the definition line of

bool Connect(bool usebluetooth) to
bool ClassMain :: Connect(bool usebluetooth)

Then when I build it the exe file is being created with 1 warning

client_main.h:44: warning: control reaches end of non-void function

So I think in this case the control is not entering the Connect function and reaching the end of the code. So there is some problem in that part. Can you help me with this??

Also I am building it for Series 60(3rd edition) just in case you needed to know.

Hope this information is sufficient and hope I get a reply soon with a solution.

Thank you,
coolkc

PostPosted: Jul 12, 2007 @ 10:11am
by edge
Hi Coolkc,

Alright, that makes more sense. Because connect wasn't part of ClassMain, the ecd member couldn't be accessed.

The new error you receive is given when the function ends without giving a return value. Use 'return(true)' or 'return(false)' at the end of the function to fix this.

PostPosted: Jul 12, 2007 @ 11:18am
by coolkc
Hi,

Thank you for the reply.

Now I got the executable and the size is 664KB . Is it not more for an executable file??

Also now how to run this executable?? DOS prompt wont work and I did not understand the solution given by you earlier. Can you tell me how to run the executables that are created to test how the programs are running???

Hoping to get a reply soon.

Thank you
coolkc

PostPosted: Jul 12, 2007 @ 2:33pm
by edge
Hi Coolkc,

To run the application you need to execute it from the Edge IDE. Or you can browse to the executable and double click on it.

However, if you're building for Symbian, you need to use the 'create setup' command to build a .sis file (as Windows Desktop is unable to run Symbian executables). You can send the .sis file through bluetooth (or use Nokia PC suite) and install it on your device.

PostPosted: Jul 13, 2007 @ 8:48am
by coolkc
Hi,

In the server code the following error is arising:

error: `strlen' undeclared (first use this function)

I included the string.h library and also specified the path in Library/Include path. But then many other errors are coming saying supports only Win32 application. If I am building it for Series 60 (3rd edition) how should I include the string libraries. How to get rid of this error??

Hoping to get a reply soon.

Thanking you,
coolkc

PostPosted: Jul 13, 2007 @ 9:42am
by coolkc
Hi,

Also I had one more doubt. Can we use commands like printf in EDGE IDE?? If I want to display some message on the screen how to do it???

Thank you,
coolkc