Exemplo n.º 1
0
ServerSocket
TcpServerSocket_create(char* address, int port)
{
    ServerSocket serverSocket = NULL;

    int fd;

    if ((fd = socket(AF_INET, SOCK_STREAM, 0)) >= 0) {
        struct sockaddr_in serverAddress;

        prepareServerAddress(address, port, &serverAddress);

        //TODO check if this works with BSD
        int optionReuseAddr = 1;
        setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (char *) &optionReuseAddr, sizeof(int));

        if (bind(fd, (struct sockaddr *) &serverAddress, sizeof(serverAddress)) >= 0) {
            serverSocket = malloc(sizeof(struct sServerSocket));
            serverSocket->fd = fd;
            serverSocket->backLog = 0;
        }
        else {
            close(fd);
            return NULL ;
        }

#if (CONFIG_ACTIVATE_TCP_KEEPALIVE == 1)
        activateKeepAlive(fd);
#endif
    }

    return serverSocket;
}
Exemplo n.º 2
0
int
Socket_connect(Socket self, char* address, int port)
{
    struct sockaddr_in serverAddress;

    prepareServerAddress(address, port, &serverAddress);

    self->fd = socket(AF_INET, SOCK_STREAM, 0);

#if (CONFIG_ACTIVATE_TCP_KEEPALIVE == 1)
        activateKeepAlive(self->fd);
#endif

    if (connect(self->fd, (struct sockaddr *) &serverAddress, sizeof(serverAddress)) < 0)
        return 0;
    else
        return 1;
}
Exemplo n.º 3
0
bool
Socket_connect(Socket self, const char* address, int port)
{
    struct sockaddr_in serverAddress;

    if (DEBUG_SOCKET)
        printf("Socket_connect: %s:%i\n", address, port);

    if (!prepareServerAddress(address, port, &serverAddress))
        return false;

    self->fd = socket(AF_INET, SOCK_STREAM, 0);

#if CONFIG_ACTIVATE_TCP_KEEPALIVE == 1
    activateKeepAlive(self->fd);
#endif

    fd_set fdSet;
    FD_ZERO(&fdSet);
    FD_SET(self->fd, &fdSet);

    fcntl(self->fd, F_SETFL, O_NONBLOCK);

    if (connect(self->fd, (struct sockaddr *) &serverAddress, sizeof(serverAddress)) < 0) {
        if (errno != EINPROGRESS)
            return false;
    }

    struct timeval timeout;
    timeout.tv_sec = self->connectTimeout / 1000;
    timeout.tv_usec = (self->connectTimeout % 1000) * 1000;

    if (select(self->fd + 1, NULL, &fdSet, NULL, &timeout) <= 0)
        return false;
    else
        return true;
}
Exemplo n.º 4
0
ServerSocket
TcpServerSocket_create(const char* address, int port)
{
    ServerSocket serverSocket = NULL;

    int fd;

    if ((fd = socket(AF_INET, SOCK_STREAM, 0)) >= 0) {
        struct sockaddr_in serverAddress;

        if (!prepareServerAddress(address, port, &serverAddress)) {
            close(fd);
            return NULL;
        }

        int optionReuseAddr = 1;
        setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (char *) &optionReuseAddr, sizeof(int));

        if (bind(fd, (struct sockaddr *) &serverAddress, sizeof(serverAddress)) >= 0) {
            serverSocket = GLOBAL_MALLOC(sizeof(struct sServerSocket));
            serverSocket->fd = fd;
            serverSocket->backLog = 0;
        }
        else {
            close(fd);
            return NULL ;
        }

#if CONFIG_ACTIVATE_TCP_KEEPALIVE == 1
        activateKeepAlive(fd);
#endif

        setSocketNonBlocking((Socket) serverSocket);
    }

    return serverSocket;
}
Exemplo n.º 5
0
Arquivo: comm.c Projeto: fcovatti/iccp
int prepare_Send(char * addr, int port, struct sockaddr_in * server_addr){

	ServerSocket serverSocket = NULL;
	int ec;
	WSADATA wsa;
	SOCKET listen_socket = INVALID_SOCKET;
	socklen_t optlen;
	int buf_size = 163840;

	if ((ec = WSAStartup(MAKEWORD(2,0), &wsa)) != 0) {
		LOG_MESSAGE("winsock error: code %i\n");
		return -1;
	}

	if (prepareServerAddress(addr, port, server_addr) < 0){
	  	LOG_MESSAGE("error preparing address\n");
	  	return -1;
	}

	listen_socket = socket(AF_INET, SOCK_DGRAM,IPPROTO_UDP);

	if (listen_socket == INVALID_SOCKET) {
		LOG_MESSAGE("socket failed with error: %i\n", WSAGetLastError());
		WSACleanup();
		return -1;
	}
	
	optlen = sizeof(int);
	getsockopt(listen_socket,SOL_SOCKET, SO_SNDBUF, (char *) &buf_size, &optlen);
	LOG_MESSAGE("socket size %d\n", buf_size);

	setsockopt(listen_socket,SOL_SOCKET, SO_SNDBUF, (char *) &buf_size, optlen);
	setsockopt(listen_socket,SOL_SOCKET, SO_RCVBUF, (char *) &buf_size, optlen);
	
	return listen_socket;
}