login server

09/07/2012 18:40 szymek111#1
Hi I have a problem.I writes a socket connection on c + +

It is this code

#pragma comment(lib,"ws2_32.lib")
#include <WinSock2.h>
#include <iostream>
#include <stdio.h>
#include <tchar.h>

int _tmain(int argc, _TCHAR* argv[])
{
WSAData wsa;
WORD Version = MAKEWORD(2,1);
WSAStartup(Version, &wsa);

SOCKET Listen = socket(AF_INET, SOCK_STREAM, 0);
SOCKET Connect = socket(AF_INET, SOCK_STREAM, 0);

SOCKADDR_IN Server;

char ip[12] = "86.63.109.231";
int port;

port = 4003;
Server.sin_addr.s_addr = inet_addr(ip);
Server.sin_family = AF_INET;
Server.sin_port = htons(port);

bind(Listen, (SOCKADDR*)&Server, sizeof(Server));

listen(Listen, 1);
int size = sizeof(Server);

std::cout << "--------------------------------------------------------------------------------"
<< " Server connected." << std::endl
<< " Host: " << ip << " : " << port << std::endl
<< "--------------------------------------------------------------------------------" << std::endl;
for(;;){
if(Connect = accept(Listen, (SOCKADDR*)&Server, &size)){
std::cout << "Connection accepted from: " << inet_ntoa(Server.sin_addr) << std::endl;
char recvbuf[225];
recv(Connect,recvbuf,225,0);
std::cout << recvbuf << std::endl;
}

}

WSACleanup();
std::cin.get();
return 0;
}


When trying to enable the program,do I see such errors


C:\Documents and Settings\dom\Pulpit\Untitled2.c|3|iostream: No such file or directory|
C:\Documents and Settings\dom\Pulpit\Untitled2.c||In function `main':|
C:\Documents and Settings\dom\Pulpit\Untitled2.c|9|error: `WSAData' undeclared (first use in this function)|
C:\Documents and Settings\dom\Pulpit\Untitled2.c|9|error: (Each undeclared identifier is reported only once|
C:\Documents and Settings\dom\Pulpit\Untitled2.c|9|error: for each function it appears in.)|
C:\Documents and Settings\dom\Pulpit\Untitled2.c|9|error: syntax error before "wsa"|
C:\Documents and Settings\dom\Pulpit\Untitled2.c|11|error: `wsa' undeclared (first use in this function)|
C:\Documents and Settings\dom\Pulpit\Untitled2.c|18|warning: initializer-string for array of chars is too long|
C:\Documents and Settings\dom\Pulpit\Untitled2.c|31|error: syntax error before ':' token|
C:\Documents and Settings\dom\Pulpit\Untitled2.c|37|error: duplicate label `std'|
C:\Documents and Settings\dom\Pulpit\Untitled2.c|31|error: `std' previously defined here|
C:\Documents and Settings\dom\Pulpit\Untitled2.c|37|error: syntax error before ':' token|
C:\Documents and Settings\dom\Pulpit\Untitled2.c|40|error: duplicate label `std'|
C:\Documents and Settings\dom\Pulpit\Untitled2.c|31|error: `std' previously defined here|
C:\Documents and Settings\dom\Pulpit\Untitled2.c|40|error: syntax error before ':' token|
C:\Documents and Settings\dom\Pulpit\Untitled2.c|46|error: duplicate label `std'|
C:\Documents and Settings\dom\Pulpit\Untitled2.c|31|error: `std' previously defined here|
C:\Documents and Settings\dom\Pulpit\Untitled2.c|46|error: syntax error before ':' token|
||=== Build finished: 16 errors, 1 warnings ===|


What can I change in this code?
09/07/2012 19:38 SmackJew#2
Aside from the fact that your ip buffer is too small for the string, I don't see anything wrong with the code. The compiler can't find iostream, check if your include directories contain the files you include. You file should end on .cpp, not .c as well, if you want to use the STL.
09/08/2012 12:26 ernilos#3
Change the name of page for 'main.cpp' and try it.
09/08/2012 13:09 szymek111#4
I don't understand ;p
09/08/2012 13:15 MrSm!th#5
Which compiler do you use? Is it properly installed?
09/08/2012 13:27 szymek111#6
Code::Blocks and Microsoft Visual c++
09/08/2012 14:28 ernilos#7
Quote:
Originally Posted by szymek111 View Post
I don't understand ;p
Change the name of file(Untield.c) To main.cpp ans try it now..
09/08/2012 14:38 lolix3#8
I have compiled this code and it works, but you have in you copy 2 syntax errors.

Greetz
09/08/2012 15:23 MrSm!th#9
Quote:
Originally Posted by szymek111 View Post
Code::Blocks and Microsoft Visual c++
And which one are you using for that program? MSVC oder C::B?
Quote:
Originally Posted by ernilos View Post
Change the name of file(Untield.c) To main.cpp ans try it now..
That doesn't matter.

Quote:
Originally Posted by lolix3 View Post
I have compiled this code and it works, but you have in you copy 2 syntax errors.

Greetz
If it really has syntax errors, it obviously cannot compile.
I didn't find any syntax errors btw.
09/08/2012 15:30 SmackJew#10
Quote:
Originally Posted by MrSm!th View Post
That doesn't matter.
Yes it does.

Quote:
Originally Posted by MrSm!th View Post
If it really has syntax errors, it obviously cannot compile.
I didn't find any syntax errors btw.
Code:
char ip[12] = "86.63.109.231";
My posts seem to be invisible.
09/08/2012 16:47 szymek111#11
Already everything is ok but when I turn on the debugger.The program sounded for a few seconds and switches off ;/.I have no errors
09/08/2012 16:49 MrSm!th#12
Quote:
Originally Posted by SmackJew View Post
Yes it does.
You were the one who said that the name of the file does not matter, yourself.
Quote:
Code:
char ip[12] = "86.63.109.231";
My posts seem to be invisible.
That is not a syntax error.
09/08/2012 17:39 SmackJew#13
Quote:
Originally Posted by MrSm!th View Post
You were the one who said that the name of the file does not matter, yourself.
Quote:
Originally Posted by Me
Your file should end on .cpp, not .c as well, if you want to use the STL.
Quote:
Originally Posted by MrSm!th View Post
That is not a syntax error.
So your compiler lets you put 14 bytes in a 13 byte buffer? I'd switch if I were you.
09/08/2012 18:26 MrSm!th#14
Yes, I saw that post, but I didn't mean it. I meant an old post of yours in which you demonstrated that even "whatever.schweineschauze" is possible.
Why the hell should the file name matter for includes? His include-config clearly seems corrupted.

Quote:
Originally Posted by SmackJew View Post
So your compiler lets you put 14 bytes in a 13 byte buffer? I'd switch if I were you.
It's rather a runtime error, if you exceed the range of an array. My compiler complains about such errors, but old compilers (and C::B is shipped with an old one) may ignore them and let you do what you want (afaik C compilers let you fully decide at which index you want to write, but I just heard that from a C coder).
09/08/2012 19:28 SmackJew#15
Quote:
Originally Posted by MrSm!th View Post
Yes, I saw that post, but I didn't mean it. I meant an old post of yours in which you demonstrated that even "whatever.schweineschauze" is possible.
Why the hell should the file name matter for includes? His include-config clearly seems corrupted.
It matters in the sense that Code::Blocks, by default, either comes with GCC or no compiler. Code::Blocks calls GCC for you, and chooses GCC's parameters according to your settings, but also according to your files, if you've ever compiled something with GCC you will also know that include directories are passed as an argument as well. My guess is, if you compile a .c file with Code::Blocks it automatically assumes you're compiling C Code, and thus doesn't pass the include directories to gcc which contain C++ headers, like iostream, and frankly, why would it?

The difference between whatever.schweineschnauze, main.cpp and main.c is Code::Blocks assumptions. OP probably opened a C++ project in Code::Blocks. Now if his file was named whatever.schweineschnauze or main.cpp or mycatlikestolickitscrotch.cpp all would be well, Code::Blocks makes the right assumptions and doesn't really care about the file name. If you end your file on .c, as described, Code::Blocks most likely assumes it's C, resulting in the error described.

Quote:
Originally Posted by MrSm!th View Post
It's rather a runtime error, if you exceed the range of an array. My compiler complains about such errors, but old compilers (and C::B is shipped with an old one) may ignore them and let you do what you want (afaik C compilers let you fully decide at which index you want to write, but I just heard that from a C coder).
There's a difference between an error that is detectable during interpretation of the code, like this one, and an error that is not, like copying more bytes than the buffers capacity with strcpy for example. I seriously doubt any compiler, including GCC, most likely used by OP, would compile this. It would be a testament to sloppy design and callousness, if enabled by default.