|
||||||
| 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 |
|
|||
|
I have made the a portion of the server software(i did it till the bind portion) .
Code:
#include<windows.h>
#include<iostream.h>
int main()
{
WSAData wsadata;
if (WSAStartup(MAKEWORD(2,0),&wsadata)!=0);
{cout<<"winsock startup failed\n"<<WSAGetLastError();
WSACleanup();
return -1;
}
cout<<"winsock startup is succes\n";
SOCKET servsock=socket(AF_INET,SOCK_STREAM,0);
if(servsock ==INVALID_SOCKET)
{cout<<"socket init failed\n";
return -1;
}
cout<<"socket init\n";
sockaddr_in sin;
sin.sin_port=htons(80);
sin.sin_addr.s_addr=INADDR_ANY;
sin.sin_family=AF_INET;
if (bind(servsock,(sockaddr*)&sin,sizeof(sin))==SOCKET_ERROR)
{cout<<"FAILED TO BIND"<<WSAGetLastError();
WSACleanup();
return -1;
}
std::cout<<"Bind successful!\n";
WSACleanup();
return 0;
}
-------------------------------------------------------------------------- 32:2 D:\Dev-Cpp\include\c++\3.4.2\backward\backward_warning.h multiple definition of `main' 32:2 D:\Dev-Cpp\include\c++\3.4.2\backward\backward_warning.h first defined here 32:2 D:\Dev-Cpp\include\c++\3.4.2\backward\backward_warning.h ld returned 1 exit status -------------------------------------------------------------------------- Void or methodz can you please tell me where i am going wrong Thank You |
| Sponsored Links |
|
|
|
|||
|
First of all you are using <iostream.h>, which is a deprecated header. You just need to use <iostream>.
Next, you can't use 'cout' without 'using namespace std;', because 'cout' is in the std namespace. That is unless you do this 'std::cout', at every instance of 'cout'. There is also no reason to 'return -1' in your main function, it doesn't matter what you return because you are in the main function and it is going to exit your program regardless. The way you were doing WSAStartup was not working for me, and I looked at it for a while and it looked alright, so I just thought I'd show you this alternative method which I know works for sure. Code:
int ret;
WSAData wsadata;
WORD ver = MAKEWORD(2, 0);
ret = WSAStartup(ver,&wsadata);
if (thing !=0)
{cout<<"winsock startup failed\n"<<WSAGetLastError();
getch();
WSACleanup();
return -1;
}
|
|
|||
|
Methodz i know that the standard header file of a c++
programme is #include<stdio> not #include<stdio.h> but it works similarly we can use cout without using namespace std.I know its a bad habit.But it works I figured out the problem ,i was keeping both win_connect and win_listen in same source file,So i transferred win_listen to another project (i did not change the code)and it was working pefectly(i have attached a screenshotof the output take a look), then i updated the programme :- Code:
#include<windows.h>
#include<iostream.h>
int main()
{
WSAData wsadata;
if (WSAStartup(MAKEWORD(2,0),&wsadata)!=0)
{cout<<"winsock startup failed\n"<<WSAGetLastError();
WSACleanup();
return -1;
}
cout<<"winsock startup is succes\n";
SOCKET servsock=socket(AF_INET,SOCK_STREAM,0);
if(servsock ==INVALID_SOCKET)
{cout<<"socket init failed\n";
return -1;
}
cout<<"socket init\n";
sockaddr_in sin;
sin.sin_port=htons(80);
sin.sin_addr.s_addr=INADDR_ANY;
sin.sin_family=AF_INET;
if (bind(servsock,(sockaddr*)&sin,sizeof(sin))==SOCKET_ERROR)
{cout<<"FAILED TO BIND"<<WSAGetLastError();
WSACleanup();
return -1;
}
cout<<"Bind successful!\n";
WSACleanup();
while (listen(servsock,SOMAXCONN)==SOCKET_ERROR)
SOCKET client;
int len = sizeof(sin);
client=accept( servsock,(sockaddr*)&sin,&len);
cout<<"Connection established!\n";
closesocket(client);
closesocket(servsock);
WSACleanup();
return 0;
}
Please can you tell me the difference between listen and accept. Shouln't the servsocket be listening as well as accept incoming connections. And an error that i am getting is :- 34 D:\Dev-Cpp\Win_Listen.cpp `client' undeclared (first use this function) Can anybody tell me why i am receiving this error i have declared "client" Thank You Last edited by WingedPanther; 06-23-2008 at 12:05 PM. Reason: add code tags |
|
|||
|
You have to put the server into listen mode, before it can accept connections.
What are you talking about? You can't use cout without using namespace std; or std::cout. The point is not whether <iostream.h> will work, it is the fact that it is a deprecated header, if you don't understand that then maybe you should look it up. You create the other socket, because it might be a server that has received connections from multiple hosts. In that case you want to only send the appropriate data for each host, to the correct host. Creating client sockets allows you to do this. |
|
|||
|
Quote:
__________________
CodeCall Blog | CodeCall Wiki | Shareware | Linux Forum Chat with other CodeCall members on IRC; connect to irc.codecall.net and join #codecall |
| Sponsored Links |
|
|
|
|||
|
Methodz i have changed the code as you said by correcting that cout to std::cout.My client software is Win-Connect which is a in the project winsock.The code that i have written is :-
Code:
#include<windows.h>;
#include<iostream>;
int main()
{
WSAData wsadata;
if(WSAStartup(MAKEWORD(2,0),&wsadata)!=0)
{
std::cout<<"1:)startup failed\n"<<WSAGetLastError();
WSACleanup();
return -1;
}
else
{
std::cout<<"2:)Socket Init Success\n"<<WSAGetLastError();
}
SOCKET mysock=socket(AF_INET,SOCK_STREAM,0);
if(mysock==INVALID_SOCKET )
{
std::cout<<"3:)Socket Init Failed\n";
WSACleanup();
return -1;
}
else
{
std::cout<<"4:)Socket Init Success\n";
sockaddr_in sin;
sin.sin_port=htons(80);
sin.sin_addr.s_addr=inet_addr("127.0.0.1");
sin.sin_family=AF_INET;
if (connect(mysock,(sockaddr*)&sin,sizeof(sin))==SOCKET_ERROR)
{std::cout<<"5:(Socket Init FAiled\n"<<WSAGetLastError();
// getch();
WSACleanup();
// getch();
return -1;
}
std::cout<<"connection sucessful";
closesocket(mysock);
//getch();
return 0;
}
}
Like this D:\>Dev-Cpp\winsock.exe My server software:- Code:
#include<windows.h>
#include<iostream>
int main()
{
WSAData wsadata;
if (WSAStartup(MAKEWORD(2,0),&wsadata)!=0)
{std::cout<<;"winsock startup failed\n"<<WSAGetLastError();
WSACleanup();
return -1;
}
std::cout<<"winsock startup is succes\n";
SOCKET servsock=socket(AF_INET,SOCK_STREAM,0);
if(servsock ==INVALID_SOCKET)
{std::cout<<"socket init failed\n";
return -1;
}
std::cout<<"socket init\n";
sockaddr_in sin;
sin.sin_port=htons(80);
sin.sin_addr.s_addr=INADDR_ANY;
sin.sin_family=AF_INET;
if (bind(servsock,(sockaddr*)&sin,sizeof(sin))==SOCKET_ERROR)
{std::cout<<"FAILED TO BIND"<<WSAGetLastError();
WSACleanup();
return -1;
}
std::cout<<"Bind successful!\n";
WSACleanup();
while (listen(servsock,SOMAXCONN)==SOCKET_ERROR); //Putting the socket in listening mode
SOCKET client;
int len = sizeof(sin);
client=accept( servsock,(sockaddr*)&sin,&len);
std::cout<<"Connection established!\n";
closesocket(client);
closesocket(servsock);
WSACleanup();
// getch();
return 0;
}
But the beginner tutorial from where i am learning said that the win-connect will display "connection sucessful" and the win-listen will display "connection established" when they are running at the same time .But i could not find a way to run two programmes from same compiler(by clicking on run).So i opened the the win-listen and ran the programme from the compiler((by clicking on run). and the win-connect from the command line.But i never got the required output "win-connect will display connection sucessful and the win-listen will display connection established".So please please please can antbody tell me where i am going wrong .i am stuck with this code for 4 days, i will be really thankful whoever helps me out.And another thing is that when i am putting the socket in listening mode:-while (listen(servsock,SOMAXCONN)==SOCKET_ERROR); Should i keep the SOMAXCONN like this or i have to give a vaLUE LIKE 5 or 10 while (listen(servsock,10)==SOCKET_ERROR); Please help me out brothers .Please,Please Methodz I know it pains your eye to read the code please read the code and point out the problem. thank you Last edited by bodhi2016; 06-23-2008 at 02:45 PM. |
|
|||
|
Just put some system("pause") or getch() (#include <conio.h>) at the places where your program can exit so you can so how far it is getting. Then tell me where that gets you.
Also for listen(param1, param2) you could have param2 as 1 for your purpose. All it says is how many clients can be accepted. So all you really need your server to do is accept one client. |
![]() |
| 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 |
| Successfully Selling Your Software | Jordan | Tutorials | 11 | 07-16-2008 06:25 PM |
| DNS Hacking | John | Tutorials | 1 | 04-09-2008 01:34 PM |
| Download problem, internal Server Error | j3cubcapt | ionFiles | 1 | 07-02-2007 10:45 AM |
| WingedPanther | ........ | 2753.6 |
| Xav | ........ | 2704 |
| Brandon W | ........ | 1702.32 |
| John | ........ | 1207.73 |
| marwex89 | ........ | 1175.24 |
| morefood2001 | ........ | 966.05 |
| dcs | ........ | 655.75 |
| Steve.L | ........ | 475.59 |
| orjan | ........ | 418.58 |
| Aereshaa | ........ | 383.54 |