Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   C++ (http://www.programmingforums.org/forum15.html)
-   -   sockets, problem with select() (http://www.programmingforums.org/showthread.php?t=7968)

Wizard1988 Jan 15th, 2006 4:09 PM

sockets, problem with select()
 
I trying to learn how to use select() with sockets, I am having a weird problem. My code does work when i use perr() for my error checking, but when I use a method I wrote the program doesen't work. Inside an if statement I check the return value of select() for anything below zero and if the return value is below zero i display the error by calling ReportError(). Somehow select returns 1 but the body of the if statement still executes. :confused:

:

#include <iostream>
#include "winsock2.h"
using namespace std;
 
void ReportError(const char*,int);
 
int main()
{
        WSADATA wsaData;
        SOCKET m_socket;
        SOCKET AcceptSocket;
        sockaddr_in service;
        sockaddr_in client;
 
        fd_set read_fds;
        fd_set SocketList;
 
        int fdmax;
        int listener;
        int newfd;
 
        FD_ZERO(&SocketList);
        FD_ZERO(&read_fds);
 
 
        int      BytesSent;
        int      BytesReceived;
        char SndBuffer[32]      = "Server: Sending Data.";
        char RecBuffer[32]      = "";
       
        if(WSAStartup(MAKEWORD(2,0), &wsaData) != NO_ERROR)
        {
                ReportError("WSAStartup()",WSAGetLastError());
                return 1;
        }
 
                if((m_socket = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0)
                {
          cout << "WSAStartup() failed\n";
          return (1);
                }
                memset(&service, 0, sizeof(service));
        service.sin_family = AF_INET;
        service.sin_addr.s_addr = inet_addr("127.0.0.1");
        service.sin_port = htons(1000);
 
        if(bind(m_socket,(struct sockaddr *) &service,sizeof(service)) == SOCKET_ERROR)
        {
                ReportError("bind()",WSAGetLastError());
                closesocket(m_socket);
                return 1;
        }
 
        if(listen(m_socket,10) == SOCKET_ERROR)
        {
                ReportError("listen()",WSAGetLastError());
                return 1;
        }
 
        FD_SET(m_socket,&SocketList);
 
        fdmax = m_socket;
 
        cout << "Waiting for a client to connect..." << endl;
 
        for(;;)
        {
                int err;
                read_fds  = SocketList;
                if(select(m_socket+1, &read_fds, NULL, NULL, NULL) < 0)
                {
                                        ReportError("select()",WSAGetLastError());
                    //perror("select()");
                }
 
                for(int x = 0; x<= fdmax; x++)
                {
                        if(FD_ISSET(x,&read_fds))
                        {
                                if(x == m_socket)
                                {
                                        int AddrSize = sizeof(client);
                                        if(AcceptSocket = accept(m_socket,(struct sockaddr *)&client,&AddrSize) == -1)
                                        {
                                                                                        ReportError("accept()",WSAGetLastError());
                                            //perror("accept()");
                                        }
                                        else
                                        {
                                                FD_SET(AcceptSocket,&SocketList);
                                                if(AcceptSocket > fdmax)
                                                {
                                                        fdmax = AcceptSocket;
                                                }
                                                cout << "New Connection from " << inet_ntoa(client.sin_addr) << " on socket " << AcceptSocket << endl;
                                        }
                                }
                                else
                                {
                                        if((BytesReceived = recv(AcceptSocket,RecBuffer,strlen(RecBuffer),0)) <=0)
                                        {
                                                if(BytesReceived == 0)
                                                {
                                                        cout << "Socket " << x << " hung up" << endl;
                                                }
                                                else
                                                {
                                                        perror("recv()");
                                                }
                                                closesocket(AcceptSocket);
                                                FD_CLR(AcceptSocket,&SocketList);
                                        }
                                        else
                                        {
                                                cout << "Data Received from client" << endl;
                                                cout << RecBuffer << endl;
                                                /*
                                                for(int tempSock = 0; tempSock <= fdmax; tempSock++)
                                                {

                                                }
                                                */
                                        }
                                }
                        }
                }
        }
 
        WSACleanup();
        system("pause");
        return 0;
}
 
void ReportError(const char* message, int error)
{
        cout << "Error occoured at " << message << endl;
        cout << "Error returned: " << error << endl;
        system("pause");
}


Thanks for help in advance


All times are GMT -5. The time now is 4:05 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC