Skip to main content
gamedev.net
🔒 Locked

Wierd error with my code

Started by kzar 23 replies 4.2k views
Original Post
kzar
kzar
edit: added tags and i forgot to say, this is in c. edit2: i have done the protocol wrong and things so this code wont work properly, but i have fixed that, the problem is just with the error. Im trying to write a simple program to connect to a online game i play. Bit new to it all so the mistakes are probably silly. This is the odd error i get "botattempt.c:20: error: number of arguments doesn't match prototype" Here is my code so far..

#include <stdio.h>
#include <string.h>
#include <SDL_net.h>
 
/* Hostname and port */
char *hostname = "eternal-lands.solexine.fr";
int port = 2000;

/* Ip address */
IPaddress ipaddress;

/* Socket */
TCPsocket tcpsock; 

/* Username and Password */
char *Username = "robocop";
char *Password = "changedovbiously";

int main()
{
    int len, result;
    char *temp;
    
    /* Start up SDL and SDL_NET */
    if(SDL_Init(0) == -1) 
    {
        printf("Couldnt start SDL up");
        exit(1);
    }
    if(SDLNet_Init() == -1) 
    {
        printf("Couldnt start SDL_NET up");
        exit(2);
    }
    
    /* Get the ip address from hostname */
    if (SDLNet_ResolveHost(&ipaddress, hostname, port) == -1)
    {
        printf("Couldnt resolve ip address from hostame");
        exit(1);
    }    
    
    /* Open the connection */
    TCPsocket = SDLNet_TCP_Open(&IPaddress);
    if(tcpsock != 0) 
    {
        printf("Could not connect");
        exit(2);
    }
    
    /* Login - Send Username */
    strcpy(temp, "140 ");
    strcat(temp, Username);
    len = strlen(temp) + 1;
    result = SDLNet_TCP_Send(TCPsocket, temp, len);    
    if(result < len) 
    {
        printf("Didnt manage to send Username");
    }
        
    /* Login - Send Password */
    strcpy(temp, "140 ");
    strcat(temp, Password);
    len = strlen(temp) + 1;
    result = SDLNet_TCP_Send(TCPsocket, temp, len);      
    if(result < len) 
    {
        printf("Didnt manage to send Password");
    }    
    
    /* Close the connection and shutdown SDL_NET*/
    SDLNet_TCP_Close(TCPsocket);
    SDLNet_Quit();
}



Qw3r7yU10p!
Qw3r7yU10p!
/* Open the connection */TCPsocket = SDLNet_TCP_Open(&IPaddress);if(tcpsock != 0) {    printf("Could not connect");    exit(2);}


looks as though the first line should be

tcpsock = SDLNet_TCP_Open(&ipaddress);

You were passing types instead of variables. Same later as well.
kzar
kzar
Good point *slaps self*

Still getting that wierd error though.. here are the errors im getting at the moment.

Quote:

botattempt.c: In function `SDL_main':
botattempt.c:20: error: number of arguments doesn't match prototype
SDL_main.h:54: error: prototype declaration


and here is my new and "improved" code.

#include <stdio.h>#include <string.h>#include <SDL_net.h> /* Hostname and port */char *hostname = "eternal-lands.solexine.fr";int port = 2000;/* Ip address */IPaddress ipaddress;/* Socket */TCPsocket tcpsock; /* Username and Password */char *Username = "robocop";char *Password = "rememberedagain";int main(){    int len, result;    char *temp;        /* Start up SDL and SDL_NET */    if(SDL_Init(0) == -1)     {        printf("Couldnt start SDL up");        exit(1);    }    if(SDLNet_Init() == -1)     {        printf("Couldnt start SDL_NET up");        exit(2);    }        /* Get the ip address from hostname */    if (SDLNet_ResolveHost(&ipaddress, hostname, port) == -1)    {        printf("Couldnt resolve ip address from hostame");        exit(1);    }            /* Open the connection */    tcpsock = SDLNet_TCP_Open(&ipaddress);    if(tcpsock != 0)     {        printf("Could not connect");        exit(2);    }        /* Send login packet */    strcpy(temp, "140 ");    strcat(temp, Username);    strcat(temp, " ");    strcat(temp, Password);    len = strlen(temp) + 1;    result = SDLNet_TCP_Send(tcpsock, temp, len);        if(result < len)     {        printf("Didnt manage to send Login message");    }        /* Close the connection and shutdown SDL_NET*/    SDLNet_TCP_Close(tcpsock);    SDLNet_Quit();}    
Qw3r7yU10p!
Qw3r7yU10p!
Don't know about SDL.

Does it replace main with SDL_main, or something?

Maybe you should use:

int main(int argc, char* argv[])
kzar
kzar
well i tried doing
Quote:
int main(void)

It changed the error but didnt fix it. I think your right its somthing to do with the sdl stuff thats included, shame i havnt got a clue what the problem is though :(
Qw3r7yU10p!
Qw3r7yU10p!
Looking here it looks as though this is exactly what you need to do:

/* The application's main() function must be called with C linkage,   and should be declared like this:#ifdef __cplusplus extern "C"#endif        int main(int argc, char *argv[])        {        } */
kzar
kzar
Your right it fixed the wierd error, now i get a few others. Thing is i dont understand why it fixed it! Anyway here are the errors.
Thanks for the help by the way!

Quote:

[Linker error] undefined reference to SDL_Init'
[Linker error] undefined reference to
SDLNet_Init'
[Linker error] undefined reference to SDLNet_ResolveHost'
[Linker error] undefined reference to
SDLNet_TCP_Open'
[Linker error] undefined reference to SDLNet_TCP_Send'
[Linker error] undefined reference to
SDLNet_TCP_Close'
[Linker error] undefined reference to SDLNet_Quit'
[Linker error] undefined reference to
WinMain@16'


And the code so far..

#include <stdio.h>#include <string.h>#include <SDL_net.h> /* Hostname and port */char *hostname = "eternal-lands.solexine.fr";int port = 2000;/* Ip address */IPaddress ipaddress;/* Socket */TCPsocket tcpsock; /* Username and Password */char *Username = "robocop";char *Password = "gullible";int main(int argc, char *argv[]){    int len, result;    char *temp;        /* Start up SDL and SDL_NET */    if(SDL_Init(0) == -1)     {        printf("Couldnt start SDL up");        exit(1);    }    if(SDLNet_Init() == -1)     {        printf("Couldnt start SDL_NET up");        exit(2);    }        /* Get the ip address from hostname */    if (SDLNet_ResolveHost(&ipaddress, hostname, port) == -1)    {        printf("Couldnt resolve ip address from hostame");        exit(1);    }            /* Open the connection */    tcpsock = SDLNet_TCP_Open(&ipaddress);    if(tcpsock != 0)     {        printf("Could not connect");        exit(2);    }        /* Send login packet */    strcpy(temp, "140 ");    strcat(temp, Username);    strcat(temp, " ");    strcat(temp, Password);    len = strlen(temp) + 1;    result = SDLNet_TCP_Send(tcpsock, temp, len);        if(result < len)     {        printf("Didnt manage to send Login message");    }        /* Close the connection and shutdown SDL_NET*/    SDLNet_TCP_Close(tcpsock);    SDLNet_Quit();}    
Qw3r7yU10p!
Qw3r7yU10p!
A linker error means that you haven't specified the libraries that the linker needs to build your application. Let it know about the SDL library and it will work.
wyrzy
wyrzy
Quote:
Original post by kzar
Thing is i dont understand why it fixed it!

Whenever you are using SDL main must be declared as int main( int, char** ) or else you will generate those same errors. As the above posters have mentioned, linker errors can mean you haven't added a .lib file to your project. You'll also need the .dll files for SDL in your main project's folder to run it as well.
iMalc
iMalc
Are you allowed to write a bot for whatever program you're writing this for?
Many programs nowdays forbit such a thing in their license agreement.
kzar
kzar
its forbiden in the licence technicly but kind of allowed as long as you tell them what you are doing. (http://www.eternal-lands.com/forum/index.php?showtopic=7234)
kzar
kzar
ok I tried the lines to include libs. It didnt change the errors but i probably did it wrong! heres me code just to make sure i got the right idea.

#include <stdio.h>#include <string.h>#include <SDL_net.h>/* Includes the SDL librarys */#pragma comment(lib, "SDL")#pragma comment(lib, "SDLmain")#pragma comment(lib, "SDLnet") /* Hostname and port */char *hostname = "eternal-lands.solexine.fr";int port = 2000;/* Ip address */IPaddress ipaddress;/* Socket */TCPsocket tcpsock; /* Username and Password */char *Username = "robocop";char *Password = "imstupid";int main(int argc, char *argv[]){    int len, result;    char *temp;        /* Start up SDL and SDL_NET */    if(SDL_Init(0) == -1)     {        printf("Couldnt start SDL up");        exit(1);    }    if(SDLNet_Init() == -1)     {        printf("Couldnt start SDL_NET up");        exit(2);    }        /* Get the ip address from hostname */    if (SDLNet_ResolveHost(&ipaddress, hostname, port) == -1)    {        printf("Couldnt resolve ip address from hostame");        exit(1);    }            /* Open the connection */    tcpsock = SDLNet_TCP_Open(&ipaddress);    if(tcpsock != 0)     {        printf("Could not connect");        exit(2);    }        /* Send login packet */    strcpy(temp, "140 ");    strcat(temp, Username);    strcat(temp, " ");    strcat(temp, Password);    len = strlen(temp) + 1;    result = SDLNet_TCP_Send(tcpsock, temp, len);        if(result < len)     {        printf("Didnt manage to send Login message");    }        /* Close the connection and shutdown SDL_NET*/    SDLNet_TCP_Close(tcpsock);    SDLNet_Quit();}    
Qw3r7yU10p!
Qw3r7yU10p!
Quote:
Original post by Anonymous Poster
You need to make sure that you are including the right libraries. Either add SDL.lib SDLmain.lib and SDLnet.lib to your library includes in your project files, or add these lines:

#pragma comment(lib, "SDL")
#pragma comment(lib, "SDLmain")
#pragma comment(lib, "SDLnet")


That only works if you're using Microsoft C++. From the error messages he's getting he's obviously using a different compiler.

You need to add the libraries to your project or make file or whatever you are using. Check your documentation.
owiley
owiley
which compiler are you using and i could probably help you
Bring more Pain
kzar
kzar
dev-c++
Qw3r7yU10p!
Qw3r7yU10p!
I just download dev-c++ to look at what documentation you have available to you:

Go to

Help->Help on Dev++

then

Dev-C++ 5->Basic Steps->Managing Project Options->Linking libraries with your project

I'm probably being too helpful here and going against my better nature - RTFM
kzar
kzar
cool right well i read the flippin manual heh. made me a project, added the stuff for the librarys, got it to compile. When i run it however it just finishes instantly and doesnt look at the code. (i put a printf in just to check and it didnt display)
I have probably messed up the project somehow :o

At least it compiles though! wouldnt have managed it on my own either, so thanks for the advice everyone.
kzar
kzar
ok it is running its just i didnt notice this
Quote:

One last thing: when you do printf(...) when having the SDL stuff in the project options in Dev-C++ the output of printf is written to a file called stdout.txt, not the screen.

http://cone3d.gamedev.net/cgi-bin/index.pl?page=tutorials/gfxsdl/tut1
owiley
owiley
when i started working with sdl it displayed correctly on my first try but i used the example.
Bring more Pain

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.