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);

    }

  • 相关阅读:
    面试知识点2
    面试知识点3
    面试知识记录
    JQuery手写一个简单的轮播图
    推荐一款好用的日历插件
    JQuery获取复选框的值
    JQuery手写一个简单的分页
    JQuery给一个元素绑定两次点击事件(第二次点击事件)
    懒加载预加载(图片)
    JQuery Ajax 使用FormData上传文件对象
  • 原文地址:https://www.cnblogs.com/zhouhbing/p/4837320.html
Copyright © 2011-2022 走看看