7.UDP 네트워크 통신

4947 단어

1. 절차

 :
      socket( )
      sockaddr_in
      bind( )
      recvfrom( )/sendto( )
 :
      socket( )
      sockaddr_in
      sendto( )/recvfrom( )

2. sendto

#include 

ssize_t sendto(int socket, const void *message, size_t length,
       int flags, const struct sockaddr *dest_addr, socklen_t dest_len);
 : 
 :
    socket: 
    message: 
    length: 
    flags: , 0
    dest_addr: ( )
    dest_len:addr 
 :
     : 
     :-1

3.recvfrom

#include 
#include 

ssize_t recvfrom(int sockfd, void *buf, size_t len, int flags,
        struct sockaddr *src_addr, socklen_t *addrlen);
 : 
 :
    sockfd: 
    buf: 
    len: 
    flags: , 0
    src_addr: ( , )
    addrlen:addr 
 :
     : 
     :-1

서버 측

#include  //printf
#include  //inet_addr htons
#include 
#include  //scoket bind listen accept connect
#include  //sockaddr_in
#include  //exit
#include  //close
#include  //strcat
#include 

#define N 128
#define errlog(errmsg) do{perror(errmsg);\
                          printf("%s-->%s-->%d
", __FILE__, __func__, __LINE__);\ exit(1);\ }while(0) int main(int argc, const char *argv[]) { int sockfd; struct sockaddr_in serveraddr, clientaddr; socklen_t addrlen = sizeof(serveraddr); char buf[N] = {}; // //bzero memset bzero(&serveraddr, addrlen); bzero(&clientaddr, addrlen); if(argc < 3) { printf("the argument is too less
"); exit(1); } // : if((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) { errlog("fail to socket"); } // : //inet_addr: IP //htons: //atoi: serveraddr.sin_family = AF_INET; serveraddr.sin_addr.s_addr = inet_addr(argv[1]); serveraddr.sin_port = htons(atoi(argv[2])); // : if(bind(sockfd, (struct sockaddr *)&serveraddr, sizeof(serveraddr)) < 0) { errlog("fail to bind"); } ssize_t ret; while(1) { if((ret = recvfrom(sockfd, buf, N, 0, (struct sockaddr *)&clientaddr, &addrlen)) < 0) { errlog("fail to recvfrom"); } else { if(strncmp(buf, "quit", 4) == 0) { break; } else { printf("%s --> %d
", inet_ntoa(clientaddr.sin_addr), ntohs(clientaddr.sin_port)); printf("client:%s
", buf); strcat(buf, " *_*"); if(sendto(sockfd, buf, N, 0, (struct sockaddr *)&clientaddr, addrlen) < 0) { errlog("fail to sendto"); } } } } close(sockfd); return 0; }

클라이언트

#include  //printf
#include  //inet_addr htons
#include 
#include  //scoket bind listen accept connect
#include  //sockaddr_in
#include  //exit
#include  //close
#include  

#define N 128
#define errlog(errmsg) do{perror(errmsg);\
                          printf("%s-->%s-->%d
", __FILE__, __func__, __LINE__);\ exit(1);\ }while(0) int main(int argc, const char *argv[]) { int sockfd; struct sockaddr_in serveraddr, clientaddr; socklen_t addrlen = sizeof(serveraddr); char buf[N] = {}; if(argc < 3) { printf("the argument is too less
"); exit(1); } // : if((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) { errlog("fail to socket"); } // : //inet_addr: IP //htons: //atoi: serveraddr.sin_family = AF_INET; serveraddr.sin_addr.s_addr = inet_addr(argv[1]); serveraddr.sin_port = htons(atoi(argv[2])); while(1) { fgets(buf, N, stdin); buf[strlen(buf) - 1] = '\0'; if(sendto(sockfd, buf, N, 0, (struct sockaddr *)&serveraddr, addrlen) < 0) { errlog("fail to sendto"); } if(strncmp(buf, "quit", 4) == 0) { break; } else { if(recvfrom(sockfd, buf, N, 0, NULL, NULL) < 0) { errlog("fail to recvfrom"); } printf("server:%s
", buf); } } close(sockfd); return 0; }

좋은 웹페이지 즐겨찾기