嵌入式 UDP网络编程实例

来源:互联网 发布:淘宝店铺资金要多久 编辑:程序博客网 时间:2024/05/22 17:04
server.c

#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <sys/wait.h>
#include <unistd.h>
#include <arpa/inet.h>
//#include <openssl/ssl.h>
//#include <openssl/err.h>
#include <pthread.h>
#include <sys/time.h>

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <net/if.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <net/route.h>
#include <unistd.h>
#include <signal.h>

 

#include <sys/time.h>
#include <string.h>
#include <stdio.h>
#include <fcntl.h>
#include <dirent.h>
#include <sys/vfs.h>



#define SERVER_PORT 8003
#define MSG_BUF_SIZE 128

int main(void)
{
    intsockfd;
    structsockaddr_in my_addr, their_addr;
    intaddr_len, numbytes;
    charbuffer[MSG_BUF_SIZE];

    if ((sockfd= socket(AF_INET, SOCK_DGRAM, 0)) == -1)
   {
       fprintf(stderr, "socket error");
       exit(1);
   }

   my_addr.sin_family = AF_INET;
   my_addr.sin_port = htons(SERVER_PORT);
   my_addr.sin_addr.s_addr = INADDR_ANY;
   bzero(&(my_addr.sin_zero), 8);

    if(bind(sockfd, (struct sockaddr *)&my_addr, sizeof(structsockaddr)) == -1)
   {
       fprintf(stderr, "bind error");
       exit(1);
   }
    addr_len =sizeof(struct sockaddr);
    if((numbytes = recvfrom(sockfd, buffer, MSG_BUF_SIZE, 0, (structsockaddr *)&their_addr, &addr_len)) == -1)
   {
       fprintf(stderr, "recvfrom error");
       exit(1);
   }

    printf("gotpacket from %s\n",inet_ntoa(their_addr.sin_addr));
   printf("packet is %d bytes long\n", numbytes);
   buffer[numbytes] = '\0';
   printf("packet contains \"%s \"\n", buffer);
   close(sockfd);
}

client.c

#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <sys/wait.h>
#include <unistd.h>
#include <arpa/inet.h>
//#include <openssl/ssl.h>
//#include <openssl/err.h>
#include <pthread.h>
#include <sys/time.h>

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <net/if.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <net/route.h>
#include <unistd.h>
#include <signal.h>

 

#include <sys/time.h>
#include <string.h>
#include <stdio.h>
#include <fcntl.h>
#include <dirent.h>
#include <sys/vfs.h>



#define SERVER_PORT 8003

int main(int argc, char *argv[])
{
    intsockfd;
    structsockaddr_in their_addr;
    structhostent *hostname;
    intnum_bytes;

    if (argc !=3)
   {
       fprintf(stderr, "usage: talker hostnamemessage\n");
       exit(1);
   }

    if((hostname = gethostbyname(argv[1])) == NULL)
   {
       fprintf(stderr, "gethostbyname error");
       exit(1);
   }

    if ((sockfd= socket(AF_INET, SOCK_DGRAM, 0)) == -1)
   {
       fprintf(stderr, "socket");
       exit(1);
   }
   their_addr.sin_family = AF_INET;
   their_addr.sin_port = htons(SERVER_PORT);
   their_addr.sin_addr = *((struct in_addr*)hostname->h_addr);
   bzero(&(their_addr.sin_zero), 8);

    if((num_bytes = sendto(sockfd, argv[2], strlen(argv[2]), 0, (structsockaddr *)&their_addr, sizeof(struct sockaddr))) ==-1)
   {
       fprintf(stderr, "sendto error");
       exit(1);
   }

    printf("sent%d bytes to %s\n", num_bytes,inet_ntoa(their_addr.sin_addr));
   close(sockfd);
    return0;
}

编译:
gcc server.c -o server
gcc client.c -o client
执行:
打开一终端执行:./server
打开另一终端执行:./client 127.0.0.1

 

原创粉丝点击