• Welcome to Valhalla Legends Archive.
 

Compiler issues?

Started by Eli_1, February 16, 2004, 03:27 PM

Previous topic - Next topic

Eli_1

This is the code for a simple console app chat bot. The code compiles on both VC++ and Borland but when compiled on the Borland compiler, it never sends the data to the server...
Here's the code.

Little thread code:

DWORD WINAPI chat_func(LPVOID lpParam) {
   while(true) {
      char input[512];
      cin.getline(input, 512);
      sprintf(input, "%s\r\n", input);
      senddata(input);
   }
}


senddata() function:

int senddata(char *msg) {
   if (sck_sock == -1)
      return -1;

   if (connected == false)  /*I'v removed both of these if statements
      return -1;               to see if it would change, but it still doesn't
                                send...*/
   int i = send(sck_sock, msg, strlen(msg), 0);
   if (i == -1) {
      closesocket(sck_sock);
      init_wsa();
      return 1;
   }
   return 0;
}

Any idea why this won't work when compiled with Borland?


[edit] it made my comments funky lookin... :P

iago

Why don't you add error messages to each of the failed if's to see where exactly it's failing?
This'll make an interesting test for broken AV:
QuoteX5O!P%@AP[4\PZX54(P^)7CC)7}$EICAR-STANDARD-ANTIVIRUS-TEST-FILE!$H+H*


Adron

Even easier might be to step through the code and see.

Eli_1

The senddata() function is returning a 0

iago

Quote from: Eli_1 on February 16, 2004, 04:52 PM
The senddata() function is returning a 0

That means the send() is failing.  I think the function to find out what went wrong with send() is WSAGetLastError().  Find out what the returns.
This'll make an interesting test for broken AV:
QuoteX5O!P%@AP[4\PZX54(P^)7CC)7}$EICAR-STANDARD-ANTIVIRUS-TEST-FILE!$H+H*


Eli_1

I just don't get how it would suddenly fail when I compile it with Borland...

iago

Neither do I, that's why we should try to figure out why.  I've never used Borland before.
This'll make an interesting test for broken AV:
QuoteX5O!P%@AP[4\PZX54(P^)7CC)7}$EICAR-STANDARD-ANTIVIRUS-TEST-FILE!$H+H*


Moonshine

You won't be able to receive any data from the socket while you're waiting for user-input on cin.getline(), btw.

iago

Quote from: Moonshine on February 16, 2004, 07:12 PM
You won't be able to receive any data from the socket while you're waiting for user-input on cin.getline(), btw.

QuoteLittle thread code
This'll make an interesting test for broken AV:
QuoteX5O!P%@AP[4\PZX54(P^)7CC)7}$EICAR-STANDARD-ANTIVIRUS-TEST-FILE!$H+H*


Moonshine

Quote from: iago on February 16, 2004, 07:30 PM
Quote from: Moonshine on February 16, 2004, 07:12 PM
You won't be able to receive any data from the socket while you're waiting for user-input on cin.getline(), btw.

QuoteLittle thread code

Then it wouldn't be a very simple console app bot :P

Eli_1

#10
is it possible Borland is having a problem with


sprintf(input, "%s\r\n", input);


so I'm actually sending my data, but the server doesn't like what it see's and ignores it?

ALSO: I'm trying to move away from iostream.h so how can I use scanf() to get the whole line, and not just stop when it hits a space (whitespace I guess it's called?)

Moonshine

#11
Eli, that's probably the only thing that I can see on there that would create such a bug.  I would suggest just trying to make a temporary buffer and see if that fixes your problem.  If it does, then obviously the sprintf() is at fault.  In addition to this, you can't use scanf() to read in user input like cin.getline() does.  For what you want to do, I would suggest using fgets().  

fgets can be used like this:

fgets(input, 512, stdin);

stdin just means get data from the console's input.

Edit: fgets is in the stdio header, btw

Also, if  (i'm taking a guess) init_wsa() is calling WSAStartup, then you don't need to call this after a call to send() fails, you just need to call WSAStartup ONCE at the beginning of your program before you use any socket related functions.  Finally, be sure to call the WSACleanup function after your program is finished (in every exiting situation).

Eli_1

#12
Quote from: Moonshine on February 17, 2004, 02:36 PM
Also, if  (i'm taking a guess) init_wsa() is calling WSAStartup, then you don't need to call this after a call to send() fails, you just need to call WSAStartup ONCE at the beginning of your program before you use any socket related functions.  Finally, be sure to call the WSACleanup function after your program is finished (in every exiting situation).

Well in the init_wsa(), WSAStartup() is only called if the socket is NULL I believe... if it's not then my Connect() function is called...  ;D

Thanks btw about the fgets() info, moon

Eli_1

ok the problem is definantly sprintf(); send() has to be working because the bot is logging in properly. maybe because my connect_string is put together using strcpy() and strcat()...

what's the difference between

sprintf(buffer, "%s\r\n", msg);

and...

strcpy(buffer, msg);
strcat(buffer, "\r\n");

(besides the fact that the first is much prettier -.^)

Zakath

Less overhead, you're only making 1 function call instead of 2. Any time you can cut down on the number of different functions that need to be loaded into memory, you're improving the efficiency of your program.
Quote from: iago on February 02, 2005, 03:07 PM
Yes, you can't have everybody...contributing to the main source repository.  That would be stupid and create chaos.

Opensource projects...would be dumb.