View Single Post
  #3 (permalink)  
Old 06-21-2008, 04:47 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 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;
}
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
File Type: rar winsock.rar (36.7 KB, 0 views)

Last edited by WingedPanther; 06-23-2008 at 12:05 PM. Reason: add code tags
Reply With Quote