• TCP/UDP



    UDP


    ---------------


    #include <stdio.h> /* These are the usual header files */
    #include <string.h>
    #include <unistd.h> /* for close() */
    #include <sys/types.h>
    #include <sys/socket.h>
    #include <stdlib.h>
    #include <netinet/in.h>
    #include <arpa/inet.h>

    #define PORT 1234 /* Port that will be opened */
    #define MAXDATASIZE 100 /* Max number of bytes of data */

    main()
    {
    int sockfd; /* socket descriptors */
    struct sockaddr_in server; /* server's address information */
    struct sockaddr_in client; /* client's address information */
    socklen_t sin_size;
    int num;
    char recvmsg[MAXDATASIZE]; /* buffer for message */
    char sendmsg[MAXDATASIZE];
    char condition[] = "quit";
    /* Creating UDP socket */
    if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) == -1) {
    /* handle exception */
    perror("Creating socket failed.");
    exit(1);
    }

    bzero(&server,sizeof(server));
    server.sin_family=AF_INET;
    server.sin_port=htons(PORT);
    server.sin_addr.s_addr = htonl (INADDR_ANY);
    if (bind(sockfd, (struct sockaddr *)&server, sizeof(struct sockaddr)) == -1) {
    /* handle exception */
    perror("Bind error.");
    exit(1);
    }

    sin_size=sizeof(struct sockaddr_in);
    while (1) {
    num = recvfrom(sockfd,recvmsg,MAXDATASIZE,0,(struct sockaddr *)&client,&sin_size);

    if (num < 0){
    perror("recvfrom error ");
    exit(1);
    }

    recvmsg[num] = '0';
    printf("You got a message (%s) from %s ",recvmsg,inet_ntoa(client.sin_addr) ); /* prints client's IP */
            if(strcmp(recvmsg,condition)==0) break;
       int i=0;
    for(i = 0 ; i < num ; i ++)
    {
       sendmsg[i] = recvmsg[num-1-i];
    }
    sendmsg[num]='0';
    sendto(sockfd,sendmsg,strlen(sendmsg),0,(struct sockaddr *)&client,sin_size);

    }

    close(sockfd); /* close listenfd */
    }


    ---------------------------------------------------------
    TCP



    #include <stdio.h>
    #include <stdlib.h>
    #include <unistd.h>
            #include <strings.h>
    #include <string.h>
    #include <sys/types.h>
    #include <sys/socket.h>
    #include <netinet/in.h>
    #include <netdb.h> /* netbd.h is needed for struct hostent =) */

    #define PORT 1234 /* Open Port on Remote Host */
    #define MAXDATASIZE 100 /* Max number of bytes of data */

    int main(int argc, char *argv[])
    {
    int fd, numbytes; /* files descriptors */
    char recvbuf[MAXDATASIZE]; /* buf will store received text */
            char sendbuf[MAXDATASIZE];
    struct hostent *he; /* structure that will get information about remote host */
    struct sockaddr_in server,client; /* server's address information */

    if (argc !=2) { /* this is used because our program will need two argument (IP address and a message */
    printf("Usage: %s <IP Address> ",argv[0]);
    exit(1);
    }

    if ((he=gethostbyname(argv[1]))==NULL){ /* calls gethostbyname() */
    printf("gethostbyname() error ");
    exit(1);
    }

    if ((fd=socket(AF_INET, SOCK_DGRAM, 0))==-1){ /* calls socket() */
    printf("socket() error ");
    exit(1);
    }

    bzero(&server,sizeof(server));
    server.sin_family = AF_INET;
    server.sin_port = htons(PORT); /* htons() is needed again */
    server.sin_addr = *((struct in_addr *)he->h_addr); /*he->h_addr passes "*he"'s info to "h_addr" */

            socklen_t len;
             len=sizeof(struct sockaddr_in);
    while (1) {
       printf("input message:");
             fgets(sendbuf,40,stdin);
    sendto(fd,sendbuf,strlen(sendbuf),0,(struct sockaddr *)&server,len);
    if ((numbytes=recvfrom(fd,recvbuf,MAXDATASIZE,0,(struct sockaddr *)&server,&len)) == -1){ /* calls recvfrom() */
    printf("recvfrom() error ");
    exit(1);
    }
    recvbuf[numbytes]='0';
    printf("Server Return Reverse Message: %s ",recvbuf); /* it prints server's welcome message */

    }

    close(fd); /* close fd */
    }
  • 相关阅读:
    unitest 测试集 实例
    python3 设置滚动条
    python3 mail
    wordpress +window 走起~
    获取在线python 文档
    chrome 自动加载flash
    报错 hint: Updates were rejected because the remote contains work that you do 解决方法
    Bitcode
    Autorelease
    atomic
  • 原文地址:https://www.cnblogs.com/li-baibo/p/3227787.html
Copyright © 2020-2023  润新知