1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162
| #include <iostream> #include <stdio.h> #include <arpa/inet.h> #include <errno.h> #include <stdlib.h> #include <string.h> #include <sys/socket.h> #include <sys/types.h> #include <st.h>
#define LISTEN_PORT 8000
#define ERR_EXIT(m) \ do \ { \ perror(m); \ exit(-1); \ } while (0)
void *client_thread(void *arg) { st_netfd_t client_st_fd = (st_netfd_t)arg; int client_fd = st_netfd_fileno(client_st_fd);
sockaddr_in client_addr; socklen_t client_addr_len = sizeof(client_addr); int ret = getpeername(client_fd, (sockaddr *)&client_addr, &client_addr_len); if (ret == -1) { printf("[WARN] Failed to get client ip: %s\n", strerror(ret)); }
char ip_buf[INET_ADDRSTRLEN]; memset(ip_buf, 0, sizeof(ip_buf)); inet_ntop(client_addr.sin_family, &client_addr.sin_addr, ip_buf, sizeof(ip_buf));
while (1) { char buf[1024] = {0}; ssize_t ret = st_read(client_st_fd, buf, sizeof(buf), ST_UTIME_NO_TIMEOUT); if (ret == -1) { printf("client st_read error\n"); break; } else if (ret == 0) { printf("client quit, ip = %s\n", ip_buf); break; }
printf("recv from %s, data = %s", ip_buf, buf);
ret = st_write(client_st_fd, buf, ret, ST_UTIME_NO_TIMEOUT); if (ret == -1) { printf("client st_write error\n"); } } }
void *listen_thread(void *arg)
{ while (1) { st_netfd_t client_st_fd = st_accept((st_netfd_t)arg, NULL, NULL, ST_UTIME_NO_TIMEOUT); if (client_st_fd == NULL) { continue; }
printf("get a new client, fd = %d\n", st_netfd_fileno(client_st_fd));
st_thread_t client_tid = st_thread_create(client_thread, (void *)client_st_fd, 0, 0); if (client_tid == NULL) { printf("Failed to st create client thread\n"); } } }
int main() { int ret = st_set_eventsys(ST_EVENTSYS_ALT); if (ret == -1) { printf("st_set_eventsys use linux epoll failed\n"); } ret = st_init(); if (ret != 0) { printf("st_init failed. ret = %d\n", ret); return -1; } int listen_fd = socket(AF_INET, SOCK_STREAM, 0); if (listen_fd == -1) { ERR_EXIT("socket"); }
int reuse_socket = 1;
ret = setsockopt(listen_fd, SOL_SOCKET, SO_REUSEADDR, &reuse_socket, sizeof(int)); if (ret == -1) { ERR_EXIT("setsockopt"); }
struct sockaddr_in server_addr; server_addr.sin_family = AF_INET; server_addr.sin_port = htons(LISTEN_PORT); server_addr.sin_addr.s_addr = INADDR_ANY; ret = bind(listen_fd, (struct sockaddr *)&server_addr, sizeof(struct sockaddr)); if (ret == -1) { ERR_EXIT("bind"); }
ret = listen(listen_fd, 128); if (ret == -1) { ERR_EXIT("listen"); } st_netfd_t st_listen_fd = st_netfd_open_socket(listen_fd); if (!st_listen_fd) { printf("st_netfd_open_socket open socket failed.\n"); return -1; }
st_thread_t listen_tid = st_thread_create(listen_thread, (void *)st_listen_fd, 1, 0); if (listen_tid == NULL) { printf("Failed to st create listen thread\n"); }
while (1) { st_sleep(1); }
return 0; }
|