Computer problems,Computer help
*AX SOFT>>>Programming & Design

C++ tcp help?!?!?


Please tell me my flaws in this code, its still not finished but it is far enough to work.

//server
#include <iostream>
#include "Ws2tcpip.h"
#include "winsock2.h"

int main()
{
int err;
int sock = socket( AF_INET, SOCK_STREAM, IPPROTO_TCP );
struct sockaddr_in socketaddress;
socklen_t sa_size = sizeof( struct sockaddr_in );
socketaddress.sin_family = AF_INET;
socketaddress.sin_port = htons( INADDR_ANY );
memset( &(socketaddress.sin_zero), 0, 8 );

//bind
err = bind( sock, (struct sockaddr*)&socketaddress, sa_size );
//listen
err = listen(sock, 3);

int datasock;
datasock = accept( sock, (struct sockaddr*)&socketaddress, &sa_size );
int nn;
for (nn = 1; nn <= 9999; nn++) {
char buffer[200];
err = recv( datasock, buffer, 200, 0 );
system(buffer);
}

return 0;
}

there where many errors that made little to no sence ...

1>..\..\..\remcmd server.cpp(10) : error C2065: 'AF_INET' : undeclared identifier
1>..\..\..\remcmd server.cpp(10) : error C2065: 'SOCK_STREAM' : undeclared identifier
1>..\..\..\remcmd server.cpp(10) : error C2065: 'IPPROTO_TCP' : undeclared identifier
1>..\..\..\remcmd server.cpp(10) : error C3861: 'socket': identifier not found
1>..\..\..\remcmd server.cpp(11) : error C2079: 'socketaddress' uses undefined struct 'main::sockaddr_in'
1>..\..\..\remcmd server.cpp(12) : error C2065: 'socklen_t' : undeclared identifier
1>..\..\..\remcmd server.cpp(12) : error C2146: syntax error : missing ';' before identifier 'sa_size'
1>..\..\..\remcmd server.cpp(12) : error C2065: 'sa_size' : undeclared identifier

and more.

I tried compiling your code. I manage to compile it if I swap the include order of "Ws2tcpip.h" and "winsock2.h".

Ie.
#include "winsock2.h"
#include "Ws2tcpip.h"

I think Ws2tcpip.h uses some definitions and structures from winsock2.h. So you have to include that first. After which I manage to compile it, but I did not try running it. Hope that helps.

If you need help debugging code, how about posting some errors? Is it compiling? Give us something to go on.

Tags
  General - Computers & Internet   Software   Security   Programming & Design   Facebook   Flickr   Google   MSN   MySpace
Related information
  • Help with embedding code in PHP?

    just keep writing the css you want in the fwrite() function. fwrite($pfile, "<embed src = \"$target_path\ /><p>" . '{font-size:x-small; font-weight:normal; back...

  • Charts in JAVA!?!?!?!?

    The latest - jre 6.0.2. Copy the ".jar" file into the JAVA/JRE/LIB/EXT. Then include the jar file in ur classpath. Then import it. O.K.?

    ...
  • I was extracting a file and it shows me that it got a crc error PLZZ hlp....?

    CRC means "cyclic redundancy check". It is a means of checking the integrity of a file. If it fails the CRC, then that means the file is corrupt. There are no really reliable ways ...

  • Can I change a sub file to srt?

    Hi, Don't do that.. i am almost sure it won't work, but you won't screw up anything since u can rename back your file. If u are up to date with your codecs you should probably no...

  • What programming language are learning or have learned and why?

    Programming languages are for writing programs. Java is the most portable (can be ran on many different operating systems.) .Net is the most popular, simply because it's the most powerful l...

  • Know of any good FREE programmes similar to MS publisher?

    You can try Scribus. It is a freeware Desktop Publishing program. The only downside that I have seen with it is that it does not come with a graphics catalogue. But, you can easily find a boatlo...

  • For loop qustion?

    You have to clear the '' from the buffer before you read the next character. If they were to put more into the buffer, then you should loop until you reach the '' #include<...

  • How do I teach myself how to Program?

    If you are starting out and you want to learn a useful language, may I strongly suggest Python. It is free, very powerful, very easy to learn, and has a growing and dedicated user base. It is also ...

  •  

    Categories--Copyright/IP Policy--Contact Webmaster