![]() |
| | #1 |
| Registered User Join Date: Mar 2009
Posts: 71
| (C++) Winsock connect and listen program. Need Help! MY CODE FOR RECIVING CONNECTING PROGRAM: Code: #include <windows.h> //Required for socket init
#include <iostream>
int main(){
char buf[256];
WSAData wsdata; //Declare WSAData
WORD wsver=MAKEWORD(2, 0); //We want Winsock 2.0
int nret=WSAStartup(wsver, &wsdata); //Pass version 2.0 and pointer to implement
if(nret != 0){ //Init failed
std::cout<<"Startup failed, error code: "<<WSAGetLastError(); //Returns error code
WSACleanup(); //Cleanup Winsock library
return -1;
}
std::cout<<"Init success\n";
SOCKET kSock=socket(AF_INET, SOCK_STREAM, 0);
if(kSock == INVALID_SOCKET){
std::cout<<"Socket init failed";
return -1;
}
std::cout<<"Socket initialized\n";
sockaddr_in sin;
std::cout<<"Enter a port number: ";
int porta;
std::cin >> porta;
sin.sin_port=htons(porta); //Connect to port 80
std::cout<<"Enter a ip number: ";
std::string ipad;
ipad = "";
std::cin >> ipad;
char ipaad[256];
strcpy(ipaad, ipad.c_str());
sin.sin_addr.s_addr=inet_addr(ipaad); //Connect to localhost
sin.sin_family=AF_INET;
if(connect(kSock,(sockaddr*)&sin, sizeof(sin)) == SOCKET_ERROR){ //Check the condition
std::cout<<"Connect failed, error: "<<WSAGetLastError(); //Returns error code
WSACleanup(); //Cleanup the library
std::string lisquit;
std::cin>>lisquit;
}
std::cout<<"Connection successful!\n";
recv(kSock, buf, sizeof(buf), 0); //Receive "Hello" from server
std::cout << buf;
closesocket(kSock);
std::string lisquit;
std::cin>>lisquit;
}
Code: #include <windows.h> //Required for socket init
#include <iostream>
int main(){
char buf[256];
std::cout<<"Enter a message to send: \n";
std::cin >> buf;
WSAData wsdata; //Declare WSAData
WORD wsver=MAKEWORD(2, 0); //We want Winsock 2.0
int nret=WSAStartup(wsver, &wsdata); //Pass version 2.0 and pointer to implement
if(nret != 0){ //Init failed
/*A successful return value should be 0
std::cout<<"Startup failed, error code: "<<WSAGetLastError(); //Returns error code
WSACleanup(); //Cleanup Winsock library
return -1;
}
std::cout<<"Init success\n";
SOCKET kSock=socket(AF_INET, SOCK_STREAM, 0);
if(kSock == INVALID_SOCKET){
std::cout<<"Socket init failed";
return -1;
}
std::cout<<"Socket initialized\n";
sockaddr_in sin;
std::cout<<"Enter a port number: ";
int porta;
std::cin >> porta;
sin.sin_port=htons(porta);
sin.sin_addr.s_addr=INADDR_ANY;
sin.sin_family=AF_INET;
if(bind(kSock,(sockaddr*)&sin, sizeof(sin)) == SOCKET_ERROR){
std::cout<<"Failed to bind\n";
WSACleanup(); //Cleanup Winsock library
return -1;
}
std::cout<<"Bind successful!\n";
while (listen(kSock, SOMAXCONN) == SOCKET_ERROR); //Loop in order to constantly listen
/* set the number of connections to SOMAXCONN, in which case the provider chooses a reasonable value (5 in Windows XP
Professional)
SOCKET client;
int len = sizeof(sin);
client=accept(kSock, (sockaddr*)&sin, &len);
std::cout<<"Connection established!\n";
send(client, buf, sizeof(buf), 0); //Send "Hello"
closesocket(client); //Close both socket handles
closesocket(kSock);
WSACleanup();
std::string lisquit;
std::cin>>lisquit;
}
Last edited by azjherben; 04-02-2009 at 04:46 AM. |
| azjherben is offline | |
| | #2 |
| Kernel hacker Join Date: Jul 2007 Location: Farncombe, Surrey, England
Posts: 15,686
| Some indentation wouldn't go amiss. -- Mats
__________________ Compilers can produce warnings - make the compiler programmers happy: Use them! Please don't PM me for help - and no, I don't do help over instant messengers. |
| matsp is offline | |
| | #3 |
| Tha 1 Sick RAT Join Date: Dec 2003
Posts: 267
| WhOA!!! :S what a mess to sift through. :S Code: while (listen(kSock, SOMAXCONN) == SOCKET_ERROR); Yes I know what you're trying to do but, logically speaking, explain it's function there.
__________________ A hundred Elephants can knock down the walls of a fortress... One diseased rat can kill everyone inside |
| WDT is offline | |
![]() |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| What exactly does listen() do from winsock 1.1 | *DEAD* | Networking/Device Communication | 6 | 12-14-2007 06:48 PM |
| Winsock Messaging Program | Morgul | Windows Programming | 13 | 04-25-2005 04:00 PM |
| my server program auto shut down | hanhao | Networking/Device Communication | 1 | 03-13-2004 10:49 PM |
| Non blocking listen() with winsock | manwhoonlyeats | C Programming | 1 | 12-08-2002 07:00 PM |
| win32 Winsock problem | spoon_ | Windows Programming | 3 | 07-19-2002 01:19 AM |