I am fairly new to Linux/socket programming. I am using select
to check connections in my server program (it will eventually be a chatroom server). I am using telnet to test it and something weird is happening. When I first run telnet (telnet localhost 5794), select
returns 1 and adds the new connection to my master file descriptor list. Everything looks fine.
But then I try to type things in telnet and nothing happens. Select returns 0 unless I open up a new telnet session.
Is select
just for finding new connections? I thought I could use it to check for input as well. Below is a copy of my code (It's a little messy at the moment because I've been furiously messing with it for the last couple hours. I'm sorry)
#include "chatpacket.cpp"
#include "serverFunctions.cpp"
#define SERVER_PORT 5794
#define MAX_PENDING 10
int main () {
fd_set connections;
fd_set waitingConnections;
user *clients = new user[50];
int serverSocket = ServerSetup (SERVER_PORT, MAX_PENDING);
int maxFD = serverSocket;
int ConnectionCount;
struct timeval tv;
FD_ZERO(&connections);
FD_SET(0, &connections);
FD_SET(serverSocket, &connections);
tv.tv_sec = 1;
tv.tv_usec = 100;
bool shutdown = false;
bool tmpflag = true;
while(!shutdown) {
if (tmpflag == true){printf("in the loop!\n");tmpflag=false;}
waitingConnections = connections;
ConnectionCount = select((maxFD+1), &waitingConnections, NULL, NULL, &tv);
if (ConnectionCount == -1) {
///HANDLE ERROR!!!!!!
printf("Connection Error!");
}
else if (ConnectionCount > 0) {
if (FD_ISSET(serverSocket, &waitingConnections)){
newConnection(serverSocket, connections, maxFD, clients); //this works fine
}
else {
checkConnections(clients, waitingConnections, maxFD); //the code never gets here
}
}
//check keyboard
shutdown = checkKeyboard();
}
}
EDIT: Here is the code for newConnection:
bool newConnection(int serverSocket, fd_set& ConnectionList, int maxFD, user* userGroup){
printf("in newConnection\n");
struct sockaddr_storage remoteaddr;
socklen_t addrlen = sizeof remoteaddr;
int newFD = accept(serverSocket,(struct sockaddr *)&remoteaddr,&addrlen);
FD_SET(newFD, &ConnectionList);
if (newFD > maxFD)
maxFD = newFD;
printf("We have a new connection!!! (newConnetcion)\n");
bool userAdded = false;
for (int i = 0; i < 50; i++){
if (userGroup[i].active == false){
userGroup[i].socket = newFD;
userGroup[i].active = true;
userAdded = true;
printf("User added in the %ith position of the array.(socket number %i)\n",i,newFD);
break;
}
}
if (!userAdded)
printf("new user was not added! (newConnetcion)\n");
}
The checkConnections function has a printf at the beginning of it so I can see whenever it enters the function. It never prints.
Here is the problem.
Note that there are two variables named
maxFD
: one in themain
function and one in thenewConnection
function. Changing one does not change the other. Recommendation: use a global instead. (Reason: There is only one for the whole application and many functions need to access it.)This is a very, very basic error. If you don't smack your forehead and say, "D'oh, that's obvious," then you may wish to go back and review an intro to programming book.