Jump to content
  • Advertisement
Sign in to follow this  
Drake22

Network Programming Help!

This topic is 4567 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I am developing using Gentoo Linux, C++, and Anjuta IDE. I am attempting to make a very simple server and client. THe server accepts connections on a port and displays any data that it receives from clients. The client connects to a server on a port and sends data. The client works just fine, but the server for some reason never accepts connections or recv's data. Please help me! Here is the code for the server: Server.cc:
#include "../include/Server.h"

int Server::start()
{
	port = 1337;
	backlog = 10;
	numClients = 0;
	sockfd = 0;
	sockaddr_in ina_srv;
	
	int sin_size = sizeof(struct sockaddr);
	maxfd = sockfd = socket(PF_INET,SOCK_STREAM,0);
	if(sockfd == -1)
	{
		cout << "Failed to establish socket." << endl;
		return -1;
	}
	cout << "Established socket" << endl;
	ina_srv.sin_family = AF_INET;
	ina_srv.sin_port = htons(port);
	ina_srv.sin_addr.s_addr = INADDR_ANY;
	memset(&(ina_srv.sin_zero),'\0',8);
	if(bind(sockfd,(struct sockaddr*)&ina_srv,sin_size) == -1)
	{
		cout << "Failed to bind socket.";
		return -1;
	}
	cout << "Bound socket" << endl;
	if(listen(sockfd,backlog) == -1)
	{
		cout << "Failed to listen to socket.";
		return -1;
	}
	cout << "Listening on socket" << endl;
	cout << "Server Started" << endl;
	return 0;
}

int Server::recvClis()
{
	fd_set tempSrvSock;
	FD_SET(sockfd,&tempSrvSock);
	timeval timeOut;
	timeOut.tv_sec = 0;
	timeOut.tv_usec = 0;
	int numClis = select(maxfd,&tempSrvSock,NULL,NULL,&timeOut);
	if(numClis == -1)
	{
		cout << "select() failed in recvClis()" << endl;
		return -1;
	}
	if(numClis != 0)
		cout << numClis << endl;
	for(int i = 0; i < numClis; i++)
	{
		sockaddr_in cliAddr;
		socklen_t sin_size = sizeof(struct sockaddr);
		SOCKET client = accept(sockfd,(struct sockaddr*)&cliAddr,&sin_size);
		if(client > maxfd)
			maxfd = client;
		if(client == -1)
		{
			cout << "Failed to accept connection" << endl;
			continue;
		}
		cout << "New Client Connected";
		cliSocks[numClients] = client;
		numClients++;
	}
	return 0;
}
	
int Server::recvMsgs()
{
	fd_set tempCliSocks;
	for(int i = 0; i < numClients; i++)
		FD_SET(cliSocks,&tempCliSocks);
	timeval timeOut;
	timeOut.tv_sec = 0;
	timeOut.tv_usec = 0;
	if(select(maxfd,&tempCliSocks,NULL,NULL,&timeOut) == -1)
	{
		cout << "select() failed in recvMsgs()" << endl;
		return -1;
	}
	char msg[1024];
	for(int i = 0; i < numClients; i++)
	{
		if(FD_ISSET(cliSocks,&tempCliSocks))
		{
			int ret = recv(cliSocks,msg,1024,0);
			if(ret == -1)
				cout << "Failed to receive message." << endl;
			else if(ret == 0)
				cout << "Client disconnected." << endl;
			else
				cout << msg;
		}
	}
	return 0;
}

int Server::stop()
{
	for(int i = 0; i < numClients; i++)
		close(cliSocks);
	close(sockfd);
	return 0;
}

int Server::update()
{
	int ret = 0;
	if(recvClis() == -1)
		ret = -1;
	if(recvMsgs() == -1)
		ret = -1;
	return ret;	
}


Server.h:
/***************************************************************************
 *            Server.h
 *
 *  Wed Nov 23 17:04:14 2005
 *  Copyright  2005  User
 *  Email
 ****************************************************************************/

#ifndef _SERVER_H
#define _SERVER_H

#include <list>
#include <sys/time.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <iostream>

using namespace std;
#define SOCKET int

class Server
{
protected:
	sockaddr_in srvAddr;
	short port;
	int backlog, numClients, maxfd;
	SOCKET sockfd, cliSocks[255];
	int recvClis();
	int recvMsgs();
public:	
	Server(){};
	int start();
	int update();
	int stop();
};

#endif /* _SERVER_H */


main.cc:
/* Created by Anjuta version 1.2.2 */
/*	This file will not be overwritten */

#include <iostream>
#include "../include/Server.h"

int main()
{
	Server srv;
	srv.start();
	while(true)
		srv.update();
	srv.stop();
	return 0;
}


Share this post


Link to post
Share on other sites
Advertisement
you should FD_ZERO(&tempSrvSock); before setting it..
I think select as its first arg takes 'highest fd' + 1.. you're setting maxfd to 'listener' fd, where it should be that + 1..

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!