C Board  

Go Back   C Board > General Programming Boards > Networking/Device Communication

Reply
 
LinkBack Thread Tools Display Modes
Old 08-09-2009, 02:03 PM   #1
Registered User
 
Join Date: Mar 2009
Posts: 71
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 08:36 PM.
azjherben is offline   Reply With Quote
Old 08-09-2009, 04:16 PM   #2
Registered User
 
Join Date: Mar 2009
Posts: 71
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 06:01 PM.
azjherben is offline   Reply With Quote
Old 08-09-2009, 06:13 PM   #3
Registered User
 
carrotcake1029's Avatar
 
Join Date: Apr 2008
Posts: 299
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.
carrotcake1029 is offline   Reply With Quote
Old 08-09-2009, 07:32 PM   #4
Registered User
 
Join Date: Mar 2009
Posts: 71
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.
azjherben is offline   Reply With Quote
Old 08-09-2009, 07:52 PM   #5
Registered User
 
Join Date: Mar 2009
Posts: 71
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();
}
azjherben is offline   Reply With Quote
Old 08-09-2009, 10:11 PM   #6
int x = *((int *) NULL);
 
Cactus_Hugger's Avatar
 
Join Date: Jul 2003
Location: Banks of the River Styx
Posts: 891
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)
Cactus_Hugger is offline   Reply With Quote
Old 08-10-2009, 06:53 AM   #7
Registered User
 
Join Date: Mar 2009
Posts: 71
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 08:43 AM.
azjherben is offline   Reply With Quote
Old 08-10-2009, 09:36 AM   #8
Registered User
 
Join Date: Mar 2009
Posts: 71
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 07:25 AM. Reason: indenting
azjherben is offline   Reply With Quote
Old 08-11-2009, 07:27 AM   #9
Registered User
 
Join Date: Mar 2009
Posts: 71
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)
azjherben is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump


All times are GMT -6. The time now is 01:52 PM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

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