C Board  

Go Back   C Board > General Programming Boards > Networking/Device Communication

Reply
 
LinkBack Thread Tools Display Modes
Old 04-02-2009, 04:42 AM   #1
Registered User
 
Join Date: Mar 2009
Posts: 71
(C++) Winsock connect and listen program. Need Help!

Question is below code.
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 FOR LISTENING PROGRAM:

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;
}
Works fine with ip 127.0.0.1 and port 808 but what ip and port do I enter for other pc's. It usually dosn't recive if I enter another ip even if I have a sender open on that pc. I have no firewall problems.

Last edited by azjherben; 04-02-2009 at 04:46 AM.
azjherben is offline   Reply With Quote
Old 04-02-2009, 04:45 AM   #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   Reply With Quote
Old 04-05-2009, 07:44 PM   #3
WDT
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);
Please explain the use of that loop.
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   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

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


All times are GMT -6. The time now is 07:26 PM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22