Ok, thanks alot jmd15, you've been a great help.
Ok, thanks alot jmd15, you've been a great help.
This is what I have so far
I'm just making my own code but referring to your code and the tutorial you gave me.Code:#include <windows.h> #include <winsock.h> #include <fstream.h> #define NETWORK_ERROR -1 #define NETWORK_OK 0 void ReportError(int, const char*); int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrevInst, LPSTR lpCmd, int nShow) { WORD sockVersion; /*Initializing the word variable to hold which version of Winsock we want to use(which is 1.1)*/ WSADATA wsaData; /*The structure that holds our data needed for startup, etc*/ int nret;/*Just an integer that we use to check if the functions failed or not*/ socksVersion(MAKEWORD 1,1); /*Giving sockVer the right value so when passed to WSAStartup it knows to use version 1.1*/ WSAStartup(sockVersion, &wsadata);/*Starting up WSA which stands for Windows Socket Architecture(NEEDS TO BE INITIALIZED TO USE WINSOCK!!!)*/ SOCKET listningsocket;/*Creating a variable of type SOCKET to use*/ listningsocket=socket(AF_INET,/*Go over TCP/IP*/ SOCK_STREAM,/*this is a stream-oriented socket*/IPPROTO_TCP/*we would like TCP rather than UDP*/); if (listningsocket==INVALID_SOCKET) { nret=WSAGetLastError(); /*Get a more detailed error message*/ ReportError(nret,"socket()"); /*Report our error with our custom function*/ WSACleanup(); /*Shutdown WinSOCK*/ return NETWORK_ERROR; /*Return error value*/ } struct sockaddr_in { short sin_family=AF_INET; /*Specifies protocol*/ u_short sin.port=1337; /*Specifies port, just having fun with the port number*/ struct in_addr sin_addr=24.186.150.196 /*Specifies IP address of server*/ char sin_zero[8]; /*unused*/ }
Last edited by bikr692002; 11-18-2005 at 07:40 PM.
Yeah, right here:
SOCKADDR_IN is already defined, I know in the tutorial it makes it LOOK like you have to define it, but they were just showing you the members of that structure. You use it like so:Code:struct sockaddr_in { short sin_family=AF_INET; /*Specifies protocol*/ u_short sin.port=1337; /*Specifies port, just having fun with the port number*/ struct in_addr sin_addr=24.186.150.196 /*Specifies IP address of server*/ char sin_zero[8]; /*unused*/ }
EDIT:Code:SOCKADDR_IN serverInfo; //declaring a sockaddr_in struct serverInfo.sin_family=AF_INET; //Setting all the member values serverInfo.sin_port=htons(1337); //Need to use htons serverInfo.sin_addr=*((LPIN_ADDR)*hostEntry->h_addr_list);
Oh yeah, and this part:
It may be legal code but I just really don't like the comments within the function. If you want to do it then fine, but it bugs me is all.Code:listningsocket=socket(AF_INET,/*Go over TCP/IP*/ SOCK_STREAM,/*this is a stream-oriented socket*/IPPROTO_TCP/*we would like TCP rather than UDP*/);
Trinity: "Neo... nobody has ever done this before."
Neo: "That's why it's going to work."
c9915ec6c1f3b876ddf38514adbb94f0
So this is the code, completed, but not compiling :'(
Just to connect/disconnectCode:#include <windows.h> #include <winsock.h> #include <stdio.h> #define NETWORK_ERROR -1 #define NETWORK_OK 0 void ReportError(int, const char*); /*The variable to control the error message*/ int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrevInst, LPSTR lpCmd, int nShow) { WORD sockVersion; /*Initializing the word variable to hold which version of Winsock we want to use(which is 1.1)*/ WSADATA wsaData; /*The structure that holds our data needed for startup, etc*/ int nret;/*Just an integer that we use to check if the functions failed or not*/ socksVersion(MAKEWORD 1,1); /*Giving sockVer the right value so when passed to WSAStartup it knows to use version 1.1*/ WSAStartup(sockVersion, &wsadata);/*Starting up WSA which stands for Windows Socket Architecture(NEEDS TO BE INITIALIZED TO USE WINSOCK!!!)*/ SOCKET listningsocket;/*Creating a variable of type SOCKET to use*/ listningsocket=socket(AF_INET,/*Go over TCP/IP*/ SOCK_STREAM,/*this is a stream-oriented socket*/ IPPROTO_TCP);/*we would like TCP rather than UDP*/ if (listningsocket==INVALID_SOCKET) { nret=WSAGetLastError(); /*Get a more detailed error message*/ ReportError(nret,"socket()"); /*Report our error with our custom function*/ WSACleanup(); /*Shutdown WinSOCK*/ return NETWORK_ERROR; /*Return error value*/ } /*Use a SOCKADDR_IN to fill in address information*/ SOCKADDR_IN serverInfo; serverInfo.sin_family=AF_INET; /*Tells what protocol to use*/ serverInfo.sin_addr.s_addr=INADDR_ANY; /*Since this socket is listning for connections, any local address will do*/ serverInfo.sin_port=htons(1337); /*Converts integer (1337) to network-byte order and inserts into the port field*/ /*Bind the socket to our local server address*/ nret=bind(listningsocket,(LPSOCKADDR)&serverInfo, sizeof(struct sockaddr)); if (nret==SOCKET_ERROR) { nret=WSAGetLastError(); ReportError(nret,"bind()"); WSACleanup(); return NETWORK_ERROR; } /*Make the socket listen*/ nret=listen(listningsocket,10); /*Up to 10 connections may wait at any one time to be accept()'ed*/ if (nret==SOCKET_ERROR) { nret=WSAGetLastERROR(); ReportError(nret,"listen()"); WSACleanup(); return NETWORK_ERROR; } /*Wait for client*/ SOCKET theClient; theClient=accept(listeningsocket, NULL, /*Address of sockaddr structure*/ NULL); /*Address of a variable containing size of sockaddrstruct*/ if (theClient==INVALID_SOCKET) { nret=WSAGetLastError(); ReportError(nret, "accept()"); WSACleanup(); return NETWORK_ERROR; } /*Send and recieve from the client, and filally*/ closesocket(theClient); closesocket(listningsocket); /*Shutdown WinSOCK*/ WSACleanup(); return NETWORK_OK; } void ReportError(int ErrorCode, const char *whichFunc) { char errorMsg[92]; ZeroMemory(errorMsg, 92); /* The following line copies the phrase, whichFunc string, and integer errorCode into the buffer*/ sprintf(errorMsg, "Call to %s returned error %d!", (char*)whichFunc, errorCode); MessageBox(NULL, errorMsg, "socketIndication",MB_OK); }
Right here:
socksVersion is a variable not a function. MAKEWORD is the macro, used like so:Code:socksVersion(MAKEWORD 1,1);
Other than that it looks pretty good, but PLEASE you need to post your compile errors so I don't have to read through the whole code again.Code:socksVersion=MAKEWORD(1,1);
Trinity: "Neo... nobody has ever done this before."
Neo: "That's why it's going to work."
c9915ec6c1f3b876ddf38514adbb94f0
I fixed that
But theres more
And the compile errors are.......
Trinity: "Neo... nobody has ever done this before."
Neo: "That's why it's going to work."
c9915ec6c1f3b876ddf38514adbb94f0