|
||||||
| C and C++ C and C++ forum for discussing all forms of C except for C#. These languages are powerful low level languages used for creating Operating Systems, Device Drivers, compilers and much more. |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Display Modes |
|
|||
|
Guys i am trying to learn the winsock programing from these two sites
Winsock Networking Tutorial (C++) - MadWizard.org C/C++ Winsock Tutorial - by Mulciber One thing that both the second tutorial is saying is that the "When you're writing the code presented in this tutorial, make sure that ***libwsock32*** is linked" What is this linking and how should i link it with my programme . And if i want to make a winsock programme the connection should be between the server and a client .Suppose if i want to make a connection betwwen my computer and my fathers laptop -can that be made or before that i have to change my computer to a server by installing a software like Apache or Personal Web Server(i may be wrong . I have never used these softwares before .But as far as i know they make the PC to act as a sever when i am connected to the net )and then make my fathers laptop act as a client .Can anybody tell some more good tutorial for winsock programming |
| Sponsored Links |
|
|
|
|||
|
First off what IDE are you using? It's different linking in each one. Also when you write client/server software the whole idea is that you are writing the software that is going to communicate. It doesn't need to be running on a designated server or client box, you are writing the software that will make the box run as a client or server. Therefore you can just put some server software that you write on your dad's box, and some client software on yours and you will be able to communicate. The simplest example to get started with is probably just an echo server.
|
|
|||
|
Quote:
You are telling that to write a server software on my own and put it into my fathers laptop.Can a amatuer coder like me write a server software .Its like making Apache of my own which has been created by world's great hacker . |
|
|||
|
Ok..... In Dev-Cpp goto Project >> Project Options >> Parameters >> Add Library or Object...
An echo server is a server that a client can send a message to and the echo server will reply with the exact same message. Server software is anything that can be requested for data from a client, Apache is obviously the extreme and you won't be writing that. But you can write simple server/client software. You just need to write applications that have the ability to communicate across a network, which is why I suggest starting with something easy like an echo server and client. |
|
|||
|
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;
}
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;
}
Quote:
|
| Sponsored Links |
|
|
|
|||
|
Okay, I didn't really look through the code because it pains me to read **** that isn't indented. I realize you didn't write it, but it hurts my eyes. And yes, there is a way you can run it on your on computer. Just run both programs, and connect to the ip address 127.0.0.1 with your client. And it will connect to the server running on your box.
127.0.0.1 is the home IP Address, which works on any box. Try that out. |
![]() |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | Search this Thread |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Adding multiple IPs to network adapter in C++ | aalfabob | C and C++ | 0 | 04-17-2008 11:47 PM |
| Windows Shortcut List - Saves Time | 2stamlers | The Lounge | 6 | 04-10-2008 06:58 AM |
| Delphi network help! | janevblagoj | Pascal/Delphi | 0 | 02-24-2008 12:01 PM |
| Windows XP Tricks & Tips!!!!..new ones. | pranky | Tutorials, Classes and Code | 7 | 02-01-2007 10:58 PM |
| Introducing The PAD Service Shareware Network | pad service | Hosting and Registrars | 2 | 05-16-2006 02:07 PM |
| John | ........ | 203.00000 |
| dargueta | ........ | 168.00000 |
| Xav | ........ | 164.00000 |
| gaylo565 | ........ | 18.00000 |
| WingedPanther | ........ | 15.00000 |
| |pH| | ........ | 15.00000 |
| Johnnyboy | ........ | 3.00000 |
| navghost | ........ | 1.00000 |
Goal: 100,000 Posts
Complete: 63%