Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Jan 15th, 2006, 4:09 PM   #1
Wizard1988
Professional Programmer
 
Wizard1988's Avatar
 
Join Date: Oct 2005
Location: Chitown
Posts: 417
Rep Power: 3 Wizard1988 is on a distinguished road
Send a message via AIM to Wizard1988
Angry 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.

#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
Wizard1988 is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 8:38 PM.

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