zoukankan      html  css  js  c++  java
  • 回环网卡通信

    #include <stdio.h>
    #include <sys/socket.h>
    #include <netinet/in.h>
    #include <arpa/inet.h>
    #include <netdb.h>
    #include <stdlib.h>
    #include <string.h>
    #include <ctype.h>
    #include <unistd.h>


    int port = 8000;

    int main() {
    struct sockaddr_in sin;
    struct sockaddr_in pin;
    int sock_descriptor;
    int temp_sock_descriptor;
    socklen_t address_size;
    char buf[16384];
    int i, len;

    sock_descriptor = socket(AF_INET, SOCK_STREAM, 0);
    if (sock_descriptor == -1) {
    perror("call to socket");
    exit(1);
    }

    bzero(&sin, sizeof(sin));
    sin.sin_family = AF_INET;
    sin.sin_addr.s_addr = INADDR_ANY;
    sin.sin_port = htons(port);

    if (bind(sock_descriptor, (struct sockaddr *)&sin, sizeof(sin)) == -1) {
    perror("call to bind");
    exit(1);
    }

    if (listen(sock_descriptor, 20) == -1) {
    perror("call to listen");
    exit(1);
    }

    printf("Accepting connections ... ");

    while(1) {
    temp_sock_descriptor =
    accept(sock_descriptor, (struct sockaddr *)&pin,
    &address_size);
    if (temp_sock_descriptor == -1) {
    perror("call to accept");
    exit(1);
    }

    if (recv(temp_sock_descriptor, buf, 16384, 0) == -1) {
    perror("call to recv");
    exit(1);
    }

    printf("received from client:%s ", buf);

    // for this server example, we just convert the
    // characters to upper case:

    len = strlen(buf);
    printf("len = %d ", len);
    for (i=0; i<len; i++) buf[i] = toupper(buf[i]);

    if (send(temp_sock_descriptor, buf, len, 0) == -1) {
    perror("call to send");
    exit(1);
    }

    close(temp_sock_descriptor);

    }
    return 0;
    }

    #include <stdio.h>
    #include <sys/socket.h>
    #include <netinet/in.h>
    #include <arpa/inet.h>
    #include <netdb.h>
    #include <string.h>

    char * host_name = "127.0.0.1"; // local host
    int port = 8000;

    main(int argc, char *argv[])
    {
    char buf[8192];
    int sd;
    struct sockaddr_in pin;
    struct hostent *server_host_name;
    char *str = "A default test string";

    if (argc < 2) {
    printf("Usage: 'text "Any test string" '");
    printf("We will send a default test string. ");
    }
    else {
    str = argv[1];
    printf("argv_str = %d ", strlen(argv[1]));
    str[strlen(argv[1])] = '';
    printf("str_len = %d ", strlen(str));
    }

    //返回对应于给定主机名的包含主机名字和地址信息的hostent结构指针
    if ((server_host_name = gethostbyname(host_name)) == 0) {
    printf("Error resolving local host ");
    exit(1);
    }

    bzero(&pin, sizeof(pin));
    pin.sin_family = AF_INET;
    pin.sin_addr.s_addr = htonl(INADDR_ANY);
    pin.sin_addr.s_addr = ((struct in_addr *)(server_host_name->h_addr))->s_addr;
    pin.sin_port = htons(port);

    if ((sd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
    printf("Error opening socket ");
    exit(1);
    }

    if (connect(sd, (void *)&pin, sizeof(pin)) == -1) {
    printf("Error connecting to socket ");
    exit(1);
    }

    // NOTE: must send a carriage return at end of message:
    printf("Sending message %s to web_server... ", str);

    if (send(sd, str, strlen(str)+1, 0) == -1) {
    printf("Error in send ");
    exit(1);
    }

    printf("..sent message.. wait for response... ");

    if (recv(sd, buf, 8192, 0) == -1) {
    printf("Error in receiving response from NLPserver ");
    exit(1);
    }

    printf(" Response from NLPserver: %s ", buf);

    close(sd);

    }

  • 相关阅读:
    mysql-Invalid use of group function-聚合函数不能直接使用在where后面-使用exists,外查询与子查询应条件关联
    python-数据库之pymysql模块(连接对象-游标对象-执行-获取值或者提交事务)
    python作业-99乘法表作业,注意制表符合print结束符以及外层和里层怎么确定的,以及闰年
    python学习笔记-if_while_for_break_continue
    python-python中的Debug大法
    python-常用的几种格式化方法
    python学习一周总结
    python学习笔记-列表,字典,以及函数返回值
    python-注释,变量命名和规范笔记
    OpenJudge 求重要逆序对数
  • 原文地址:https://www.cnblogs.com/zhouhbing/p/4837320.html
Copyright © 2011-2022 走看看