All pastes #741532 Raw Edit

Someone

public text v1 · immutable
·published 2007-10-18 19:43 UTC
rendered paste body
// Implementation of the Socket class.

#include <iostream>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <unistd.h>
#include <string>
#include <vector>
#include <arpa/inet.h>
#include <errno.h>
#include <fcntl.h>
using namespace std;
//everything is included move on

const int MAXHOSTNAME = 200;
const int MAXCONNECTIONS = 5;
const int MAXRECV = 500;

int port = 1935;
//vector<string> read_sockets;
int master;
sockaddr_in m_addr;

int recv ( std::string& s ) {
  char buf [ MAXRECV + 1 ];

  s = "";

  memset ( buf, 0, MAXRECV + 1 );

  int status = ::recv ( master, buf, MAXRECV, 0 );

  if ( status == -1 ) {
      std::cout << "status == -1   errno == " << errno << "  in Socket::recv\n";
      return 0;
    }  else if ( status == 0 )  {
      return 0;
    } else {
      s = buf;
      return status;
    }
}

int send ( const std::string s ) {
  int status = ::send ( master, s.c_str(), s.size(), '0' );
  if ( status == -1 )
    {
      return false;
    }
  else
    {
      return true;
    }
}

int main() {
	
	
	master = socket ( AF_INET, SOCK_STREAM, 0 );
	// TIME_WAIT - argh
	int on = 1;
	setsockopt ( master, SOL_SOCKET, SO_REUSEADDR, ( const char* ) &on, sizeof ( on ) );
	
	m_addr.sin_family = AF_INET;
	m_addr.sin_addr.s_addr = INADDR_ANY;
	m_addr.sin_port = htons ( port );
	int bind_return = ::bind ( master, ( struct sockaddr * ) &m_addr, sizeof ( m_addr ) );
	if (!bind_return) {
		cout << "can't bind to " + port;
	}
	
	int listen_return = ::listen ( master, MAXCONNECTIONS );
	
	//const char ceva = master;
	
	//read_sockets.push_back();
	char read_sockets[10] = {master};
	int len;

	len = strlen(read_sockets);
	
	while (true) {
		int addr_length = sizeof ( m_addr );
		int client = ::accept ( master, ( sockaddr * ) &m_addr, ( socklen_t * ) &addr_length );
		//while (true) {
		//for (int i=0; i<len; i++) {
			//$bytes = socket_recv($socket, $buffer, 2048, 0);
			/*char buf [ MAXRECV + 1 ];
			memset ( buf, 0, MAXRECV + 1 );
			int status = ::recv ( master, buf, MAXRECV, 0 );
			cout << status;*/
			std::string data;
			
			data = "lalala";
			//recv(data);
			//send(data);
			
			int status = ::send ( master, data.c_str(), data.size(), '0' );
			//exit (-1);
			
		//}
		//}
	}
	
	return 0;
}