AFo

HANGRY_Sol&Cekas發表於2024-12-01
#include <bits/stdc++.h>
#include "default.h"
#include<stdio.h>
#include<stdlib.h>
#include<netinet/in.h>
#include<sys/socket.h>
#include<arpa/inet.h>
#include<string.h>
#include<unistd.h>
#include <termio.h>
using namespace std; 
int client_fds[10]; 
char input_msg[10], recv_msg[10]; 
struct sockaddr_in server_addr; 

inline int getch() {
	struct termios tm, tm_old; int fd = 0, ch; 
	if (tcgetattr(fd, &tm) < 0) {
		return -1; 
	}

	tm_old = tm; 
	cfmakeraw(&tm);  

	if (tcsetattr(fd, TCSANOW, &tm) < 0) { 
		return -1; 
	}
	ch = getchar(); 
	if (tcsetattr(fd, TCSANOW, &tm_old) < 0) { 
		return -1; 
	}
	return ch;
}

signed main() {
	server_addr.sin_family = AF_INET; 
	server_addr.sin_port = htons(11332); 
	server_addr.sin_addr.s_addr = inet_addr("127.0.0.1"); 
	bzero(&(server_addr.sin_zero), 10); 
	int server_sock_fd = socket(AF_INET, SOCK_STREAM, 0); 
	if (server_sock_fd == -1) {
		Red; printf("Socket Error"); End; 
		return 1; 
	}
	int bind_result = bind(server_sock_fd, (struct sockaddr *)&server_addr, sizeof(server_addr)); 
	if (bind_result == -1) {
		Red; printf("Bind Error"); End; 
		return 1; 
	}
	if (listen(server_sock_fd, 5) == -1) {
		Red; printf("Listen Error"); 
		return 1; 
	}
	fd_set server_fd_set; 
	int max_fd = -1; 
	struct timeval tv; 

	while (1) {
		tv.tv_sec = 20; 
		tv.tv_usec = 0; 
		FD_ZERO(&server_fd_set); 
		FD_SET(STDIN_FILENO, &server_fd_set); 
		if (max_fd < STDIN_FILENO) max_fd = STDIN_FILENO; 
		FD_SET(server_sock_fd, &server_fd_set); 
		
		if (max_fd < server_sock_fd) max_fd = server_sock_fd; 
		for (int i = 0; i < 8; ++ i) {
			if (client_fds[i] != 0) {
				FD_SET(client_fds[i], &server_fd_set); 
				if (max_fd < client_fds[i]) max_fd = client_fds[i]; 
			}
		}
		int ret = select(max_fd +1, &server_fd_set, NULL, NULL, &tv); 
		if (ret < 0) {
			Red; printf("Select Error\n"); End; 
			continue; 
		} else if (ret == 0) {
			Red; printf("Socket Time Limit Exeed\n"); End; 
			continue; 
		} else {
			if (FD_ISSET(STDIN_FILENO, &server_fd_set)) {
				Red; printf("")
			}
		}
	}
}

相關文章