Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

Problem with network programming

bodhi2016bodhi2016 Posts: 50Member
i know that the standard header file of a c++
programme is
#include
not
#include
but it works
similarly we can use cout without
using namespace std.I know its a bad habit.But it works
My win-connect(client software) source file is in project winsock and my win-listen (server software) source file is in project winsock2.
My server software
[code]#include
#include
int main()
{
WSAData wsadata;
if (WSAStartup(MAKEWORD(2,0),&wsadata)!=0)
{cout<<"winsock startup failed
"<<WSAGetLastError();
WSACleanup();
return -1;
}
cout<<"winsock startup is succes
";
SOCKET servsock=socket(AF_INET,SOCK_STREAM,0);
if(servsock ==INVALID_SOCKET)
{cout<<"socket init failed
";
return -1;
}
cout<<"socket init
";
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!
";
WSACleanup();
while (listen(servsock,SOMAXCONN)==SOCKET_ERROR)
SOCKET client;
int len = sizeof(sin);
client=accept( servsock,(sockaddr*)&sin,&len);
cout<<"Connection established!
";
closesocket(client);
closesocket(servsock);
WSACleanup();
return 0;
} [/code]
I cannot understand the purpose of creating another socket "client".
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-CppWin_Listen.cpp `client' undeclared (first use this function)
Can anybody tell me why i am receiving this error but i have declared
"client" .
Then i read the theory portion of the tutorial very carefully once more i realised that "servsock" listens for more connection while the new socket "client" establishes connection with the client socket .But my compiler was prompting me the above error again again .In the example of my tutorial there was a semicolon after the [b]while[/b] statement so i placed a semicolon .Should there be a semicolon after while statement.But then it was working (i mean i got an output ).When i ran the programme from command line it showed
D:Dev-Cppwinsock2.exe
winsock startup is a success
socket init
Bind successful
-
But it was not returning the control to me .Does this mean that the servsock has gone into into an infinite loop(i mean it is supposed to ) to listen for incoming connection .
Doesn't this line [code]while (listen(servsock,SOMAXCONN)==SOCKET_ERROR);[/code]
send servsock to an infinite loop for listening for incoming connection (Am i correct in my conclusion of the above line of code).
Then i ran the client software(which is win-connect) in another command line but it was giving error 10061 which means there was no server software running.But the tutorial says that the client software should display "connection successful" and server software should display "connection established."I have given a screen shot of the two outputs of the command line(please take a look).Please can you help me brothers .Just for everybodys information i ran the server software first and then i ran the client software.
My client software is
[code]#include<windows.h>
#include
int main()
{
WSAData wsadata;
if(WSAStartup(MAKEWORD(2,0),&wsadata)!=0)
{
cout<<"1:(startup failed
"<<WSAGetLastError();
WSACleanup();
return -1;
}
else
{
cout<<"2:)Socket Init Success
"<<WSAGetLastError();
}
SOCKET mysock=socket(AF_INET,SOCK_STREAM,0);
if(mysock==INVALID_SOCKET )
{
cout<<"3:(Socket Init Failed
";
WSACleanup();
return -1;
}

else
{
cout<<"4:)Socket Init Success
";
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)
{cout<<"5:(Socket Init FAiled
"<<WSAGetLastError();
WSACleanup();
return -1;
}

else{
cout<<"connection sucessful";
closesocket(mysock);
}
return 0;
}
}[/code]
Thank You
Sign In or Register to comment.