Valhalla Legends Forums Archive | C/C++ Programming | Compiler issues?

AuthorMessageTime
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:
[code]
DWORD WINAPI chat_func(LPVOID lpParam) {
   while(true) {
      char input[512];
      cin.getline(input, 512);
      sprintf(input, "%s\r\n", input);
      senddata(input);
   }
}
[/code]

senddata() function:
[code]
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;
}
[/code]
Any idea why this won't work when compiled with Borland?


[edit] it made my comments funky lookin... :P
February 16, 2004, 9:27 PM
iago
Why don't you add error messages to each of the failed if's to see where exactly it's failing?
February 16, 2004, 9:52 PM
Adron
Even easier might be to step through the code and see.
February 16, 2004, 10:25 PM
Eli_1
The senddata() function is returning a 0
February 16, 2004, 10:52 PM
iago
[quote author=Eli_1 link=board=30;threadid=5326;start=0#msg44656 date=1076971935]
The senddata() function is returning a 0
[/quote]

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.
February 16, 2004, 11:40 PM
Eli_1
I just don't get how it would suddenly fail when I compile it with Borland...
February 16, 2004, 11:59 PM
iago
Neither do I, that's why we should try to figure out why. I've never used Borland before.
February 17, 2004, 12:24 AM
Moonshine
You won't be able to receive any data from the socket while you're waiting for user-input on cin.getline(), btw.
February 17, 2004, 1:12 AM
iago
[quote author=Moonshine link=board=30;threadid=5326;start=0#msg44672 date=1076980370]
You won't be able to receive any data from the socket while you're waiting for user-input on cin.getline(), btw.
[/quote]

[quote]Little thread code[/quote]
February 17, 2004, 1:30 AM
Moonshine
[quote author=iago link=board=30;threadid=5326;start=0#msg44674 date=1076981417]
[quote author=Moonshine link=board=30;threadid=5326;start=0#msg44672 date=1076980370]
You won't be able to receive any data from the socket while you're waiting for user-input on cin.getline(), btw.
[/quote]

[quote]Little thread code[/quote]
[/quote]

Then it wouldn't be a very simple console app bot :P
February 17, 2004, 6:11 AM
Eli_1
is it possible Borland is having a problem with

[code]
sprintf(input, "%s\r\n", input);
[/code]

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?)
February 17, 2004, 7:37 PM
Moonshine
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).
February 17, 2004, 8:36 PM
Eli_1
[quote author=Moonshine link=board=30;threadid=5326;start=0#msg44770 date=1077050162]
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).
[/quote]

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
February 17, 2004, 8:54 PM
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
[code]
sprintf(buffer, "%s\r\n", msg);
[/code]
and...
[code]
strcpy(buffer, msg);
strcat(buffer, "\r\n");
[/code]
(besides the fact that the first is much prettier -.^)
February 17, 2004, 9:30 PM
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.
February 17, 2004, 10:13 PM
Eli_1
I ment more along the lines of how will the two outputs differ... because the latter code works and it all sends properly, but sprintf() doesn't. :P
February 17, 2004, 10:23 PM
Skywing
[quote author=Zakath link=board=30;threadid=5326;start=0#msg44793 date=1077056031]
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]
Not true. If you have a large section of code that is used lots, it's better to put it in it's own function.
February 17, 2004, 10:24 PM
Adron
[quote author=Zakath link=board=30;threadid=5326;start=0#msg44793 date=1077056031]
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]

Yes, obviously it's always better to load one 10 kb function than two 100 byte ones.
February 17, 2004, 10:45 PM
Zakath
Well, within reason, of course. Assuming, though, that you have some well-defined goal, and that there are two ways to do it: one with one function, and one with two. Generally I wouldn't expect the actual amount of code necessary to accomplish the goal to be much different, so using a single function has the advantage.
February 17, 2004, 11:52 PM
Adron
Well, what we were talking about here was whether to use strcpy and strcat or to use sprintf... At least that was the question you replied to. Nothing about writing your own functions in that post as far as I can see. And choosing between those, I think using strcpy and strcat is the more efficient choice for a small program.
February 17, 2004, 11:58 PM
Eli_1
well considering that's the only one that works, I'll go with that one... thanks ;D
February 18, 2004, 12:20 AM

Search