View Single Post
  #6 (permalink)  
Old 06-23-2008, 02:37 PM
bodhi2016 bodhi2016 is offline
Newbie
 
Join Date: Jan 2008
Posts: 29
Credits: 0
Rep Power: 4
bodhi2016 is on a distinguished road
Default Re: Problem with the server software

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),&amp;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*)&amp;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:-
Code:
#include<windows.h>
#include<iostream>
int main()
{
    WSAData wsadata;
    if (WSAStartup(MAKEWORD(2,0),&amp;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*)&amp;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*)&amp;sin,&amp;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

Last edited by bodhi2016; 06-23-2008 at 02:45 PM.
Reply With Quote