Advertisement
Abstract-Chief

netkeyboard/server.c

Apr 27th, 2022
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.00 KB | None | 0 0
  1. #include <sys/types.h>
  2. #include <sys/types.h>
  3. #include <sys/socket.h>
  4. #include <netinet/in.h>
  5. #include<unistd.h>
  6. #include<stdlib.h>
  7. #include<stdio.h>
  8. #include<arpa/inet.h>
  9. void error(const char *msg){
  10.    perror(msg);
  11.    exit(1);
  12. }
  13.  
  14. int main(int argc,char *argv[]){
  15.    int my_socket;
  16.    my_socket=socket(AF_INET,SOCK_DGRAM,0);
  17.    if(my_socket<0){
  18.       error("mysocket");
  19.    }
  20.    /*создали сокет сервера*/
  21.    struct sockaddr_in socket_addr;
  22.    socket_addr.sin_family=AF_INET;
  23.    socket_addr.sin_port=htons(12345);
  24.    socket_addr.sin_addr.s_addr=htons(INADDR_ANY);
  25.    /*заполнили структуру адреса*/
  26.    if(bind(my_socket,(struct sockaddr*)&socket_addr,sizeof(socket_addr))<0){
  27.       error("bind");
  28.    }
  29.    struct sockaddr klient_addr;
  30.    socklen_t size=(socklen_t)sizeof(klient_addr);
  31.    char *buf=malloc(1*sizeof(char));
  32.    while(*buf!='\n'){
  33.    recvfrom(my_socket,buf,6,0,&klient_addr,&size);
  34.    sendto(my_socket,"complete",8,0,&klient_addr,size);
  35.    printf("%c\n",*buf);
  36.    }
  37. }
  38.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement