Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <sys/types.h>
- #include <sys/types.h>
- #include <sys/socket.h>
- #include <netinet/in.h>
- #include<unistd.h>
- #include<stdlib.h>
- #include<stdio.h>
- #include<arpa/inet.h>
- void error(const char *msg){
- perror(msg);
- exit(1);
- }
- int main(int argc,char *argv[]){
- int my_socket;
- my_socket=socket(AF_INET,SOCK_DGRAM,0);
- if(my_socket<0){
- error("mysocket");
- }
- /*создали сокет сервера*/
- struct sockaddr_in socket_addr;
- socket_addr.sin_family=AF_INET;
- socket_addr.sin_port=htons(12345);
- socket_addr.sin_addr.s_addr=htons(INADDR_ANY);
- /*заполнили структуру адреса*/
- if(bind(my_socket,(struct sockaddr*)&socket_addr,sizeof(socket_addr))<0){
- error("bind");
- }
- struct sockaddr klient_addr;
- socklen_t size=(socklen_t)sizeof(klient_addr);
- char *buf=malloc(1*sizeof(char));
- while(*buf!='\n'){
- recvfrom(my_socket,buf,6,0,&klient_addr,&size);
- sendto(my_socket,"complete",8,0,&klient_addr,size);
- printf("%c\n",*buf);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement