Beispiel #1
0
/*
-- FUNCTION: initializeServer
--
-- DATE: March 12, 2011
--
-- REVISIONS: September 22, 2011 - Added some extra comments about failure and
-- a function call to set the socket into non blocking mode.
--
-- DESIGNER: Luke Queenan
--
-- PROGRAMMER: Luke Queenan
--
-- INTERFACE: void initializeServer(int *listenSocket, int *port);
--
-- RETURNS: void
--
-- NOTES:
-- This function sets up the required server connections, such as creating a
-- socket, setting the socket to reuse mode, binding it to an address, and
-- setting it to listen. If an error occurs, the function calls "systemFatal"
-- with an error message.
*/
void initializeServer(int *listenSocket, int *port)
{
    // Create a TCP socket
    if ((*listenSocket = tcpSocket()) == -1)
    {
        systemFatal("Cannot Create Socket!");
    }
    
    // Allow the socket to be reused immediately after exit
    if (setReuse(&(*listenSocket)) == -1)
    {
        systemFatal("Cannot Set Socket To Reuse");
    }
    
    // Bind an address to the socket
    if (bindAddress(&(*port), &(*listenSocket)) == -1)
    {
        systemFatal("Cannot Bind Address To Socket");
    }
    
    // Set the socket to listen for connections
    if (setListen(&(*listenSocket)) == -1)
    {
        systemFatal("Cannot Listen On Socket");
    }
}
Beispiel #2
0
/*
-- FUNCTION: createTransferSocket
--
-- DATE: September 29, 2011
--
-- REVISIONS: (Date and Description)
--
-- DESIGNER: Luke Queenan
--
-- PROGRAMMER: Luke Queenan
--
-- INTERFACE: void createTransferSocket(int *socket);
--
-- RETURNS: void
--
-- NOTES:
-- This function creates a transfer socket for the server to communicate with
-- the client. The socket is bound to port 7000.
*/
void createTransferSocket(int *socket)
{
    int *defaultPort = (int*)malloc(sizeof(int));
    *defaultPort = TRANSFER_PORT;
    
    // Create a TCP socket
    if ((*socket = tcpSocket()) == -1)
    {
        systemFatal("Cannot Create Socket!");
    }
    
    // Allow the socket to be reused immediately after exit
    if (setReuse(socket) == -1)
    {
        systemFatal("Cannot Set Socket To Reuse");
    }
    
    // Bind an address to the socket
    if (bindAddress(defaultPort, socket) == -1)
    {
        systemFatal("Cannot Bind Address To Socket");
    }
    
    free(defaultPort);
}
Beispiel #3
0
/*
-- FUNCTION: initializeServer
--
-- DATE: September 23, 2011
--
-- REVISIONS:
--
-- DESIGNER: Karl Castillo
--
-- PROGRAMMER: Karl Castillo
--
-- INTERFACE: void initalizeServer(int* port, int* socket)
--				port - the port the client will listen on
--				socket - the socket that will hold the new socket
--
-- RETURNS: void
--
-- NOTES:
-- This function will create the socket, set reuse and listen for any incoming
-- connections from the server.
*/
void initalizeServer(int* port, int* socket)
{
    int sock = 0;

    // Create a TCP socket
    if ((sock = tcpSocket()) == -1) {
        systemFatal("Cannot Create Socket!");
    }
    
    // Allow the socket to be reused immediately after exit
    if (setReuse(&sock) == -1) {
        systemFatal("Cannot Set Socket To Reuse");
    }
    
    // Bind an address to the socket
    if (bindAddress(port, &sock) == -1) {
        systemFatal("Cannot Bind Address To Socket");
    }
    
    // Set the socket to listen for connections
    if (setListen(&sock) == -1) {
        systemFatal("Cannot Listen On Socket");
    }
    
    if((*socket = acceptConnection(&sock)) == -1) {
    	systemFatal("Cannot Accept on Socket");
    }
    close(sock);
}
Beispiel #4
0
// --------------------------------------------------
void WSAClientSocket::bind(Host &h)
{
	struct sockaddr_in localAddr;

#pragma warning(disable : 4244)
	if ((sockNum = socket (PF_INET, SOCK_STREAM, IPPROTO_TCP)) == -1)
#pragma warning(default : 4244)
		throw SockException("Can`t open socket");

	setBlocking(false);
	setReuse(true);

	memset(&localAddr,0,sizeof(localAddr));
	localAddr.sin_family = AF_INET;
	localAddr.sin_port = htons(h.port);
	localAddr.sin_addr.s_addr = INADDR_ANY;

	if( ::bind (sockNum, (sockaddr *)&localAddr, sizeof(localAddr)) == -1)
		throw SockException("Can`t bind socket");

	if (::listen(sockNum,SOMAXCONN))
		throw SockException("Can`t listen",WSAGetLastError());

	host = h;
}
Beispiel #5
0
/*
-- FUNCTION: initConnection
--
-- DATE: September 23, 2011
--
-- REVISIONS:
--
-- DESIGNER: Karl Castillo
--
-- PROGRAMMER: Karl Castillo
--
-- INTERFACE: int initConnection(int port, const char* ip) 
--				port - the port the client will connect to to the server
--				ip - ip address of the server
--
-- RETURNS: int - the new socket created
--
-- NOTES:
-- This function will create the socket, set reuse and connect to the server.
*/
int initConnection(int port, const char* ip) 
{
	int socket;

	// Creating Socket
	if((socket = tcpSocket()) == -1) {
		systemFatal("Error Creating Socket");
	}

	// Setting Socket to Reuse
	if(setReuse(&socket) == -1) {
		systemFatal("Error Set Socket Reuse");
	}
	
	// Connect to transfer server
	if(connectToServer(&port, &socket, ip) == -1) {
		systemFatal("Cannot Connect to server");
	}
	
	return socket;
}
Beispiel #6
0
// --------------------------------------------------
void UClientSocket::bind(Host &h)
{
	struct sockaddr_in localAddr;

	if ((sockNum = socket (PF_INET, SOCK_STREAM, IPPROTO_TCP)) == -1)
		throw SockException("Can`t open socket");

	setReuse(true);
	setBlocking(false);

	memset(&localAddr,0,sizeof(localAddr));
	localAddr.sin_family = AF_INET;
	localAddr.sin_port = htons(h.port);
	localAddr.sin_addr.s_addr = INADDR_ANY;

	if( ::bind (sockNum, (sockaddr *)&localAddr, sizeof(localAddr)) == -1)
		throw SockException("Can`t bind socket");

	if (::listen(sockNum,3))
		throw SockException("Can`t listen");

	host = h;
}