Sign in to follow this  
Drake22

Network Programming Help!

Recommended Posts

Drake22    122
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[i],&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[i],&tempCliSocks))
		{
			int ret = recv(cliSocks[i],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[i]);
	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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this