Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* Код сервера на языке Си */
- #include <sys/types.h>
- #include <sys/socket.h>
- #include <netinet/in.h>
- #include <arpa/inet.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <unistd.h>
- #define port 1100
- int main(void) {
- struct sockaddr_in stSockAddr;
- int i32SocketFD = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
- if (i32SocketFD == -1) {
- perror("ошибка при создании сокета");
- exit(EXIT_FAILURE);
- }
- memset(&stSockAddr, 0, sizeof (stSockAddr));
- stSockAddr.sin_family = PF_INET;
- stSockAddr.sin_port = htons(port);
- stSockAddr.sin_addr.s_addr = htonl(INADDR_ANY);
- if (bind(i32SocketFD, (struct sockaddr*) &stSockAddr, sizeof (stSockAddr)) == -1) {
- perror("Ошибка: связывания");
- close(i32SocketFD);
- exit(EXIT_FAILURE);
- }
- if (listen(i32SocketFD, 10) == -1) {
- perror("Ошибка: прослушивания");
- close(i32SocketFD);
- exit(EXIT_FAILURE);
- }
- for (;;) {
- int i32ConnectFD = accept(i32SocketFD, 0, 0);
- if (i32ConnectFD < 0) {
- perror("Ошибка: принятия");
- close(i32SocketFD);
- exit(EXIT_FAILURE);
- }
- /* выполнение операций чтения и записи ... */
- shutdown(i32ConnectFD, SHUT_RDWR);
- close(i32ConnectFD);
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement