int main(int argc, char *argv[]) { SOCKET sock = create_socket(); if (sock != INVALID_SOCKET) { if (connect_socket(&sock, IP_ADDR, PORT_NO, 10)) { char buffer[256] = {0}; int n; keyboard_read("Please enter the message: ", buffer, 255); n = send_socket(&sock, buffer, strlen(buffer)); if (n >= 0) { memset((void*) buffer, 0, 256); n = recv_socket(&sock, buffer, 256); if (n >= 0) { printf("%s\n", buffer); } else { puts("ERROR reading from socket"); } } else { puts("ERROR writing to socket"); } } else { puts("ERROR connecting"); } disconnect_socket(&sock); } else { puts("ERROR opening socket"); } destroy_socket(&sock); return 0; }
int main(int argc, char *argv[]) { WSADATA wsaData; // Initialize Winsock int iResult = WSAStartup(MAKEWORD(2,2), &wsaData); if (iResult != 0) { printf("WSAStartup failed with error: %d\n", iResult); return 1; } SOCKET sockfd = create_socket(); if (sockfd != INVALID_SOCKET) { if (bind_socket(&sockfd, PORT_NO, 10)) { SOCKET sockets[MAX_SOCKETS] = {INVALID_SOCKET}; int opened_sockets = 0; while (1) { int idx, max; if (opened_sockets < MAX_SOCKETS) { SOCKET new_sockfd = wait_connection(&sockfd); if (new_sockfd != INVALID_SOCKET) { sockets[opened_sockets++] = new_sockfd; } //else if (errno != WSAETIMEDOUT) { //puts("ERROR waiting"); } } for (idx = 0, max = opened_sockets; idx < max; idx++) { if (sockets[idx] != INVALID_SOCKET) { if (do_processing(&sockets[idx])==-1) { if (errno != WSAETIMEDOUT) { printf("Disconnecting...\n"); disconnect_socket(&sockets[idx]); sockets[idx] = sockets[opened_sockets-1]; sockets[opened_sockets-1] = INVALID_SOCKET; opened_sockets--; } } } } } } else { printf("ERROR binding\n"); } disconnect_socket(&sockfd); } else { printf("ERROR opening socket\n"); } destroy_socket(&sockfd); return 0; }
int main(int argc, char *argv[]) { SOCKET sockfd = create_socket(); if (sockfd != INVALID_SOCKET) { if (bind_socket(&sockfd, PORT_NO, 10)) { SOCKET sockets[MAX_SOCKETS] = {INVALID_SOCKET}; int opened_sockets = 0; while (1) { int idx, max; if (opened_sockets < MAX_SOCKETS) { SOCKET new_sockfd = wait_connection(&sockfd); if (new_sockfd != INVALID_SOCKET) { sockets[opened_sockets++] = new_sockfd; } else if (WSAGetLastError() != WSAETIMEDOUT) { //printf("ERROR waiting\n"); } } for (idx = 0, max = opened_sockets; idx < max; idx++) { if (sockets[idx] != INVALID_SOCKET) { if (do_processing(&sockets[idx]) < 0) { printf("Disconnecting...\n"); disconnect_socket(&sockets[idx]); sockets[idx] = sockets[opened_sockets-1]; sockets[opened_sockets-1] = INVALID_SOCKET; opened_sockets--; } } } } } else { perror("ERROR binding"); } disconnect_socket(&sockfd); } else { perror("ERROR opening socket"); } destroy_socket(&sockfd); return 0; }
int main(int argc, char *argv[]) { SOCKET sock = create_socket(), sockets[MAX_CLIENT] = {INVALID_SOCKET}; int num_clients = 0; //cria e conecta o socket if(sock != INVALID_SOCKET) { if(bind_socket(&sock, PORT_NO) == SOCKET_ERROR) { printf("ERROR binding\n"); exit(1); } else { listen(sock, MAX_CLIENT); printf("Servidor iniciado.\n"); } } else { printf("ERROR opening socket"); exit(1); } //aceita conexões puts("Esperando por conexoes..."); while (1) { int i, max; if (num_clients < MAX_CLIENT) { SOCKET new_sockfd = wait_connection(&sock); if(new_sockfd != INVALID_SOCKET) { sockets[num_clients++] = new_sockfd; } else if (WSAGetLastError() != WSAETIMEDOUT) { printf("ERROR waiting\n"); } } for (i = 0, max = num_clients; i < max; i++) { if (sockets[i] != INVALID_SOCKET) { if (!do_processing(&sockets[i])) { if (WSAGetLastError() != WSAETIMEDOUT) { printf("Disconnecting...\n"); disconnect_socket(&sockets[i]); sockets[i] = sockets[num_clients-1]; sockets[num_clients-1] = INVALID_SOCKET; num_clients--; } } } } } disconnect_socket(&sock); destroy_socket(&sock); return 0; }