Good hosting? For geting and sending positions?

This is a discussion on Good hosting? For geting and sending positions? within the Networking/Device Communication forums, part of the General Programming Boards category; See fourth reply. (Mine) For what I need help with....

  1. #1
    Registered User
    Join Date
    Mar 2009
    Posts
    76

    Good hosting? For geting and sending positions?

    See fourth reply. (Mine)
    For what I need help with.
    Last edited by azjherben; 08-09-2009 at 09:36 PM.

  2. #2
    Registered User
    Join Date
    Mar 2009
    Posts
    76
    Ya know, I noticed that when you accept you put it in a socket of it's own. Maybe if I put those in an array, I could go and check each one.......

    Am I on the right track with that?



    I have this so far:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <AzjSock.h>
    
    class socketinfo{
    public:
    SOCKET sock;
    int SendBytes;
    int RectBytes;
    };
    
    
    
    
    int main(int argc, char *argv[])
    {
    char* buff;    
    
    WSA2S();
    
    SOCKET server;
    server = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); 
    
    
    SOCKADDR_IN sinServer;
    memset(&sinServer, 0, sizeof(sinServer));
    sinServer.sin_family = AF_INET;
    sinServer.sin_addr.s_addr = INADDR_ANY; // Where to start server?
    sinServer.sin_port = htons(PORT); // Port
            
    if(bind(server, (LPSOCKADDR)&sinServer, sizeof(sinServer)) == SOCKET_ERROR){ easymsg("Couldn't bind."); }
    
    
    int numclients = 0;
    socketinfo clients[9];
    
    if(listen(server,9)){ easymsg("Listen failed."); }
    ioctlsocket(server, FIONBIO, &iMode);
    
    FD_SET Writer;
    FD_SET Reader;
         
         
    while (true){
    FD_ZERO(&Reader);
    FD_ZERO(&Writer);
    
    FD_SET(server, &Reader);
    
    
    
    for(int o = 0; o<numclients; o++)
    {
    //if(clients[o].RectBytes > clients[o].SendBytes){ FD_SET(clients[o].sock,&Writer);
    //} else{ FD_SET(clients[o].sock,&Reader);
    //}
    FD_SET(clients[o].sock,&Reader);
    }
    
    
    
    
    if(select(0,&Reader,&Writer,NULL,NULL))
    {
    
    if(FD_ISSET(server,&Reader)){
     
    SOCKET ATemp;
    
    if((ATemp = accept(server,NULL,NULL)) != INVALID_SOCKET){
    numclients++;
    clients[numclients].sock = ATemp;
    ioctlsocket(clients[numclients].sock,FIONBIO,&iMode);
    clients[numclients].SendBytes = 0;
    clients[numclients].RectBytes = 0;
    cout<<"Accepted a client.\nNow up to "<<numclients<<" clients.\n";
            
    }                 
    }
    
    
    
    
    
    
    
    
    for (int i = 0; i<numclients; i++)
    {
    
    
    recv(clients[i].sock,buff,sizeof(buff),0);
    cout<<"Recived: "<<buff<<"\n";
    
    
    }
    
    
    
    
    
    
    }
        Sleep(5);
    }     
    
    
    
    closesocket(server);
    WSACleanup();
    }

    It can accept connections and SHOULD be able to recive.
    Last edited by azjherben; 08-09-2009 at 07:01 PM.

  3. #3
    Registered User carrotcake1029's Avatar
    Join Date
    Apr 2008
    Posts
    398
    What you really need to look into is either the use of threads and as you have already mentioned, the function select().

    Also, do you have a specific problem?

    You also should be checking the return of your calls to send and recv, or else you may be dealing with incomplete or corrupted data.

    Also for a cosmetic tip, please indent your code. I(and I am sure other posters here) tend to be more helpful when I don't have to struggle to read code.

  4. #4
    Registered User
    Join Date
    Mar 2009
    Posts
    76
    Quote Originally Posted by carrotcake1029 View Post
    What you really need to look into is either the use of threads and as you have already mentioned, the function select().

    Also, do you have a specific problem?

    You also should be checking the return of your calls to send and recv, or else you may be dealing with incomplete or corrupted data.

    Also for a cosmetic tip, please indent your code. I(and I am sure other posters here) tend to be more helpful when I don't have to struggle to read code.

    Atm my problem is that it's not reciving and it should.

  5. #5
    Registered User
    Join Date
    Mar 2009
    Posts
    76
    I'd also like to add...

    I made this program... it crashes... why?

    Code:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <AzjSock.h>
    
    class socketinfo{
    public:
    SOCKET sock;
    WSABUF DataBuf;
    bool isused;
    int SendBytes;
    int RectBytes;
    };
    
    
    
    
    int main(int argc, char *argv[])
    {
    char* buff;    
    
    WSA2S();
    
    SOCKET server;
    server = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); 
    
    
    SOCKADDR_IN sinServer;
    memset(&sinServer, 0, sizeof(sinServer));
    sinServer.sin_family = AF_INET;
    sinServer.sin_addr.s_addr = INADDR_ANY; // Where to start server?
    sinServer.sin_port = htons(PORT); // Port
            
    if(bind(server, (LPSOCKADDR)&sinServer, sizeof(sinServer)) == SOCKET_ERROR){ easymsg("Couldn't bind."); }
    
    
    int numclients = 0;
    socketinfo clients[9];
    
    for(int uu = 0; uu<9; uu++){
    clients[uu].isused = false;
    }
    
    if(listen(server,9)){ easymsg("Listen failed."); }
    ioctlsocket(server, FIONBIO, &iMode);
    
    FD_SET Writer;
    FD_SET Reader;
         
         
    while (true){
    FD_ZERO(&Reader);
    FD_ZERO(&Writer);
    
    FD_SET(server, &Reader);
    
    
    
    for(int o = 0; o<numclients; o++)
    {
    //if(clients[o].RectBytes > clients[o].SendBytes){ FD_SET(clients[o].sock,&Writer);
    //} else{ FD_SET(clients[o].sock,&Reader);
    //}
    FD_SET(clients[o].sock,&Reader);
    }
    
    
    
    
    if(select(0,&Reader,&Writer,NULL,NULL))
    {
    
    if(FD_ISSET(server,&Reader)){
     
    SOCKET ATemp;
    
    if((ATemp = accept(server,NULL,NULL)) != INVALID_SOCKET){
    numclients++;
    clients[numclients].sock = ATemp;
    ioctlsocket(clients[numclients].sock,FIONBIO,&iMode);
    clients[numclients].SendBytes = 0;
    clients[numclients].RectBytes = 0;
    clients[numclients].isused = true;
    cout<<"Accepted a client.\nNow up to "<<numclients<<" clients.\n";
            
    }                 
    }
    
    
    
    
    
    
    
    
    for (int i = 0; i<numclients; i++)
    {
    if (clients[i].isused == true)
    {
    
    if (FD_ISSET(clients[i].sock, &Reader))
    {
    
    cout<<"Stuff.\n";
    clients[i].DataBuf.len = 256;
    clients[i].DataBuf.buf = buff;
    DWORD Flags;  
    DWORD Rec;                              
    WSARecv(clients[i].sock, &(clients[i].DataBuf), 1,&Rec, &Flags, NULL, NULL);
    
    char* bob = new char[clients[i].DataBuf.len];
    bob = clients[i].DataBuf.buf;
    
    cout<<"Recived: "<<bob<<"\n";
    
    }
    
    }
    }
    
    
    
    
    
    
    }
        Sleep(5);
    }     
    
    
    
    closesocket(server);
    WSACleanup();
    }

  6. #6
    int x = *((int *) NULL); Cactus_Hugger's Avatar
    Join Date
    Jul 2003
    Location
    Banks of the River Styx
    Posts
    902
    It crashes due to a lack of indentation. Fix that, then we'll pursue any remaining problems.
    long time; /* know C? */
    Unprecedented performance: Nothing ever ran this slow before.
    Any sufficiently advanced bug is indistinguishable from a feature.
    Real Programmers confuse Halloween and Christmas, because dec 25 == oct 31.
    The best way to accelerate an IBM is at 9.8 m/s/s.
    recursion (re - cur' - zhun) n. 1. (see recursion)

  7. #7
    Registered User
    Join Date
    Mar 2009
    Posts
    76
    Okay, here it is, indented:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <AzjSock.h>
    
    class socketinfo{
    public:
    SOCKET sock;
    WSABUF DataBuf;
    bool isused;
    int SendBytes;
    int RectBytes;
    };
    
    
    
    
    int main(int argc, char *argv[])
    {
    char* buff;    
    
    WSA2S();
    
    SOCKET server;
    server = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); 
    
    
    SOCKADDR_IN sinServer;
    memset(&sinServer, 0, sizeof(sinServer));
    sinServer.sin_family = AF_INET;
    sinServer.sin_addr.s_addr = INADDR_ANY; // Where to start server?
    sinServer.sin_port = htons(PORT); // Port
            
    if(bind(server, (LPSOCKADDR)&sinServer, sizeof(sinServer)) == SOCKET_ERROR){ easymsg("Couldn't bind."); }
    
    
    int numclients = 0;
    socketinfo clients[9];
    
               for(int uu = 0; uu<9; uu++){
                       clients[uu].isused = false;
               }
    
    if(listen(server,9)){ easymsg("Listen failed."); }
    ioctlsocket(server, FIONBIO, &iMode);
    
    FD_SET Writer;
    FD_SET Reader;
         
         
         while (true){
               FD_ZERO(&Reader);
               FD_ZERO(&Writer);
    
               FD_SET(server, &Reader);
    
    
    
                         for(int o = 0; o<numclients; o++)
                         {
                         FD_SET(clients[o].sock,&Reader);
                         }
    
    
    
    
                         if(select(0,&Reader,&Writer,NULL,NULL))
                         {
    
                                   if(FD_ISSET(server,&Reader))
                                   {
     
                                   SOCKET ATemp;
    
                                   if((ATemp = accept(server,NULL,NULL)) != INVALID_SOCKET){
                                   numclients++;
                                   clients[numclients].sock = ATemp;
                                   ioctlsocket(clients[numclients].sock,FIONBIO,&iMode);
                                   clients[numclients].SendBytes = 0;
                                   clients[numclients].RectBytes = 0;
                                   clients[numclients].isused = true;
                                   cout<<"Accepted a client.\nNow up to "<<numclients<<" clients.\n";
                                   }
                                   }                 
                        }
    
    
    
    
    
    
    
    
                        for (int i = 0; i<numclients; i++)
                        {
                                 if (clients[i].isused == true)
                                 {
    
                                         if (FD_ISSET(clients[i].sock, &Reader))
                                         {
    
                                         cout<<"Stuff.\n";
                                         clients[i].DataBuf.len = 256;
                                         clients[i].DataBuf.buf = buff;
                                         DWORD Flags;  
                                         DWORD Rec;                              
                                         WSARecv(clients[i].sock, &(clients[i].DataBuf), 1,&Rec, &Flags, NULL, NULL);
    
                                         char* bob = new char[clients[i].DataBuf.len];
                                         bob = clients[i].DataBuf.buf;
    
                                         cout<<"Recived: "<<bob<<"\n";
    
                                         }
    
                                 }
                      }
    
    
    
    
    
    
              
        Sleep(5);
    }     
    
    
    
    closesocket(server);
    WSACleanup();
    }


    Also...

    My timeval dosn't work...
    Code:
    struct timeval tv;
    tv.tv_sec = 10;
    tv.tv_usec = 500000;
    Anything wrong with it? Cause it makes compiler errors.
    Last edited by azjherben; 08-10-2009 at 09:43 AM.

  8. #8
    Registered User
    Join Date
    Mar 2009
    Posts
    76
    Here is my newest code, it almost works:

    I indented it again.

    Code:
    #include <sys/time.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <AzjSock.h>
    
    
    
    
    class socketinfo{
          public:
                 SOCKET sock;
                 WSABUF DataBuf;
                 bool isused;
                 int SendBytes;
                 int RectBytes;
    };
    
    
    
    
    int main(int argc, char *argv[])
    {
        
    timeval tv;
    tv.tv_sec = 10;
    tv.tv_usec = 500000;
    
    char* buff;    
    
    WSA2S();
    
    SOCKET server;
    server = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); 
    
    
    SOCKADDR_IN sinServer;
    memset(&sinServer, 0, sizeof(sinServer));
    sinServer.sin_family = AF_INET;
    sinServer.sin_addr.s_addr = INADDR_ANY; // Where to start server?
    sinServer.sin_port = htons(PORT); // Port
            
    if(bind(server, (LPSOCKADDR)&sinServer, sizeof(sinServer)) == SOCKET_ERROR){ easymsg("Couldn't bind."); }
    
    
    int numclients = 0;
    socketinfo clients[9];
    
               for(int uu = 0; uu<9; uu++){
                       clients[uu].isused = false;
               }
    
    if(listen(server,9)){ easymsg("Listen failed."); }
    ioctlsocket(server, FIONBIO, &iMode);
    
    
    FD_SET Reader;
         
         
         while (true){
               FD_ZERO(&Reader);
    
    
               FD_SET(server, &Reader);
    
    
    
                         for(int o = 0; o<numclients; o++)
                         {
                         FD_SET(clients[o].sock,&Reader);
                         }
    
    
    
    
                         if(select(0,&Reader,NULL,NULL,&tv))
                         {
    
                                   if(FD_ISSET(server,&Reader))
                                   {
     
                                   SOCKET ATemp;
    
                                   if((ATemp = accept(server,NULL,NULL)) != INVALID_SOCKET){
                                   numclients++;
                                   clients[numclients].sock = ATemp;
                                   ioctlsocket(clients[numclients].sock,FIONBIO,&iMode);
                                   clients[numclients].SendBytes = 0;
                                   clients[numclients].RectBytes = 0;
                                   clients[numclients].isused = true;
                                   cout<<"Accepted a client.\nNow up to "<<numclients<<" clients.\n";
                                   }
                                   }         
                                   
                                   for (int i = 0; i<9; i++)
                                   {
                                 if (clients[i].isused == true)
                                 {
    
                                         if (FD_ISSET(clients[i].sock, &Reader))
                                         {
    
                                         clients[i].DataBuf.len = 256;
                                         clients[i].DataBuf.buf = buff;
                                         DWORD Flags;  
                                         DWORD Rec;                              
                                         WSARecv(clients[i].sock, &(clients[i].DataBuf), 1,&Rec, &Flags, NULL, NULL);
    
                                         char* bob = new char[clients[i].DataBuf.len];
                                         bob = clients[i].DataBuf.buf;
    
                                         cout<<"Recived: "<<bob<<"\n";
    
                                         
                                         }
    
                                 }
                      }        
                        }
    
    
    
    
    
    
    
    
                        
    
    
    
    
    
    
              
        Sleep(5);
    }     
    
    
    
    closesocket(server);
    WSACleanup();
    }
    Last edited by azjherben; 08-11-2009 at 08:25 AM. Reason: indenting

  9. #9
    Registered User
    Join Date
    Mar 2009
    Posts
    76
    There are olny two things wrong with it now.....
    1. It keeps repeating "Recived: " MESSAGEHERE until something else is recived.
    2. It won't recive until someone new connects.


    (And I did Indent it)

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21