View Single Post
  #5 (permalink)  
Old 06-18-2008, 11:59 AM
bodhi2016 bodhi2016 is offline
Newbie
 
Join Date: Jan 2008
Posts: 29
Rep Power: 4
bodhi2016 is on a distinguished road
Default Re: Network Question For Beginners

Win Connect
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
/*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;
sin.sin_port=htons(808); //Connect to port 80
sin.sin_addr.s_addr=inet_addr("127.0.0.1"); //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
return -1;
}
std::cout<<"Connection successful!\n";
recv(kSock, buf, sizeof(buf), 0); //Receive "Hello" from server
std::cout << buf;
closesocket(kSock);
return 0;
}
Win Listen
Code:
#include <windows.h> //Required for socket init
#include <iostream>
int main(){
char buf[] = "Hello\n";
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;
sin.sin_port=htons(808);
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();
return 0;
}
Methodz i found an example of winsock programming on the net.I just wanted to verify from you .The portion of code Win Connect is the client software (which will go inside my my laptop)and the sceond portion Win Listen is the server software(which will go inside my fathers laptop). Is there anyway i can execute the both portion of code in my computer. This is a quote from the tutorial that i am reading.the line marked in red has made me ask you this question.
Quote:
In the part 1, we looked at initializing the library, creating sockets, making our own connections, and terminating the
library and our socket handle(s). Now it's time to look at all of this, but instead of connecting, we will be listening for
our own connections instead.
To do so, we need to bind our socket to a defined port, then use the listen() function to wait
for connections. To test whether or not our program succeeded, we can use the command prompt. netstat -ano (o for PID, n for
numerical output, and a for displaying listening ports/active connections) does the trick because we can refer to the PID of
our application using the task manager, then check whether or not the PID shows up in the output, along with the defined
port.
Reply With Quote

Sponsored Links