zoukankan      html  css  js  c++  java
  • Linux C socket 基于 UDP

    /*************************************************************************
        > File Name: server.c
        > Author: Stomach_ache
        > Mail: 1179998621@qq.com 
        > Created Time: 2014年02月25日 星期二 08时17分13秒
        > Propose: 
     ************************************************************************/
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    #include <sys/types.h>
    #include <sys/socket.h>
    #include <linux/in.h>


    #define KEY ("request!")
    #define MAX_SIZE 1024


    int 
    main(int argc, char **argv)
    {
    int sfp;
    struct sockaddr_in s_add, c_add;
    int sin_size;
    int recbytes;
    char buffer[MAX_SIZE];
    unsigned short portnum = 0x8888;

    printf("Hello, welcome to my server! ");

    //创建一个套接字,类型使用:SOCK_DGRAM,它的默认协议是UDP

    sfp = socket(AF_INET, SOCK_DGRAM, 0);
    if (-1 == sfp) {
    printf("socket fail! ");
    return -1;
    }
    printf("socket ok! ");

    //好像比较推荐使用memset而不是bzero
    bzero(&s_add, sizeof(struct sockaddr_in));
    s_add.sin_family = AF_INET;
    s_add.sin_addr.s_addr= htonl(INADDR_ANY);
    s_add.sin_port = htons(portnum);

    //套接字和地址绑定
    if (-1 == bind(sfp, (struct sockaddr *)(&s_add), sizeof(struct sockaddr))) {
    printf("bind fail! ");
    return -1;
    }
    printf("bind ok! ");


    int len = sizeof(c_add);


    while (1) {


    if (-1 == (recbytes = recvfrom(sfp, buffer, 1024, 0,
      (struct sockaddr*)&c_add,
      &len))) {
    printf("read data fail! ");
    return -1;
    }
    //printf("read ok! REC: ");
    buffer[recbytes] = '';
    /* printf("%s ", buffer); */
    if (0 == strcmp(buffer, KEY)) {
    printf("key is right! ");
    if (-1 == sendto(sfp, "you are an idiot! ", 32, 0,
    (struct sockaddr*)&c_add,
    sizeof(c_add))) {
    printf("write fail! ");
    return -1;
    }
    printf("write ok! ");
    }
    /* close(nfp); */
    }

    close(sfp);
    return 0;

    }



    /*************************************************************************
        > File Name: client.c
        > Author: Stomach_ache
        > Mail: 1179998621@qq.com 
        > Created Time: 2014年02月25日 星期二 08时42分12秒
        > Propose: 
     ************************************************************************/
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    #include <sys/socket.h>
    #include <linux/in.h>
    #include <sys/types.h>


    #define KEY ("request!")
    #define MAX_SIZE 1024
    #define TIME_INTERVAL 3
    int
    main(int argc, char **argv) 
    {
    int cfd;
    int recbytes;
    int sin_size;
    char buffer[MAX_SIZE] = {''};
    struct sockaddr_in s_add;
    unsigned short portnum = 0x8888;

    printf("Hello, welcome to client! ");


    cfd = socket(AF_INET, SOCK_DGRAM, 0);
    if (-1 == cfd) {
    printf("socket fail! ");
    return -1;
    }
    printf("socket ok! ");


    bzero(&s_add, sizeof(struct sockaddr_in));
    s_add.sin_family = AF_INET;
    s_add.sin_addr.s_addr = inet_addr("127.0.0.1");
    s_add.sin_port = htons(portnum);


    printf("s_addr = %#x, port : %#x ", s_add.sin_addr.s_addr, s_add.sin_port);

    //客户端每隔3秒发送一次请求
    while (1) {
       /* sleep(TIME_INTERVAL); */


    if (-1 == sendto(cfd, KEY, sizeof(KEY), 0,
    (struct sockaddr*)&s_add,
    sizeof(s_add))) {
    printf("write fail! ");
    return -1;
    }
    printf("write ok! ");


       /* sleep(TIME_INTERVAL); */


    char revmssg[MAX_SIZE];
    int len = sizeof(s_add);
    if (-1 == recvfrom(cfd, revmssg, MAX_SIZE, 0,
      (struct sockaddr*)&s_add,
      &len)) {
    printf("read fail! ");
    return -1;
    }
    printf("read ok! ");
    printf("revmssg is: %s ", revmssg);
    sleep(TIME_INTERVAL);
    }


    close(cfd);


    return 0;
    }


  • 相关阅读:
    Ubuntu搭建FTP服务器
    【wireshark】wireshark 出现There are no interfaces on which a capture can be done.的解决方法
    那些强悍的PHP一句话后门
    Ubuntu下SSH设置
    Servlet&JSP中的知识点
    String s=new String("abc")创建了几个对象?
    JavaEE的13种核心技术
    ++i和i++
    构造方法
    TCP/IP
  • 原文地址:https://www.cnblogs.com/Stomach-ache/p/3703271.html
Copyright © 2011-2022 走看看