Jump to content

Problem with the server software

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
6 replies to this topic

#1
bodhi2016

bodhi2016

    Newbie

  • Members
  • PipPip
  • 29 posts
I have made the a portion of the server software(i did it till the bind portion) .
#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;

}    

I usually compile my programme from time to time .When i compiled the above code i got many errors but i was able to tackle them but few error i cant do away with are these one:-
--------------------------------------------------------------------------
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

#2
MeTh0Dz

MeTh0Dz

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,119 posts
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.

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;
       }

Do that stuff and then tell me how it works.

#3
bodhi2016

bodhi2016

    Newbie

  • Members
  • PipPip
  • 29 posts
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 :-
#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;
}    
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-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

Attached Files


Edited by WingedPanther, 23 June 2008 - 08:05 AM.
add code tags


#4
MeTh0Dz

MeTh0Dz

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,119 posts
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.

#5
TkTech

TkTech

    The Crazy One

  • Moderators
  • 1,396 posts

Quote

What are you talking about? You can't use cout without using namespace std; or std::cout.

Some of the older 'simple' IDE's (codewarrior for example) automaticly include STD. Some new users may not be familiar with namespaces because of this.

#6
bodhi2016

bodhi2016

    Newbie

  • Members
  • PipPip
  • 29 posts
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 :-

#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;

}   

}
When i compile and run the code the dos windows flashes for a sceond and then goes away,so i have to run this programme from the commmand line
Like this D:\>Dev-Cpp\winsock.exe
My server software:-
#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();

    [COLOR="red"]while (listen(servsock,SOMAXCONN)==SOCKET_ERROR);[/COLOR]   //[B]Putting the socket in listening mode [/B]

   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;

}  
When i run the software the dos windows shows "bind sucessful" but the dos windows stays there.it does not dissapear like the prevoius one
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

Edited by bodhi2016, 23 June 2008 - 10:45 AM.


#7
MeTh0Dz

MeTh0Dz

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,119 posts
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.