一些伺服器用戶端的c例子

來源:互聯網
上載者:User

今天早上6點起床之後練習的一些c的網路編程的基礎例子

client1

/*  Make the necessary includes and set up the variables.  */#include <sys/types.h>#include <sys/socket.h>#include <stdio.h>#include <sys/un.h>#include <unistd.h>#include <stdlib.h>int main(){    int sockfd;    int len;    struct sockaddr_un address;    int result;    char ch = 'A';/*  Create a socket for the client.  */    sockfd = socket(AF_UNIX, SOCK_STREAM, 0);/*  Name the socket, as agreed with the server.  */    address.sun_family = AF_UNIX;    strcpy(address.sun_path, "server_socket");    len = sizeof(address);/*  Now connect our socket to the server's socket.  */    result = connect(sockfd, (struct sockaddr *)&address, len);    if(result == -1) {        perror("oops: client1");        exit(1);    }/*  We can now read/write via sockfd.  */    write(sockfd, &ch, 1);    read(sockfd, &ch, 1);    printf("char from server = %c\n", ch);    close(sockfd);    exit(0);}

  server1.c

/*  Make the necessary includes and set up the variables.  */#include <sys/types.h>#include <sys/socket.h>#include <stdio.h>#include <sys/un.h>#include <unistd.h>#include <stdlib.h>int main(){    int server_sockfd, client_sockfd;    int server_len, client_len;    struct sockaddr_un server_address;    struct sockaddr_un client_address;/*  Remove any old socket and create an unnamed socket for the server.  */    unlink("server_socket");    server_sockfd = socket(AF_UNIX, SOCK_STREAM, 0);/*  Name the socket.  */    server_address.sun_family = AF_UNIX;    strcpy(server_address.sun_path, "server_socket");    server_len = sizeof(server_address);    bind(server_sockfd, (struct sockaddr *)&server_address, server_len);/*  Create a connection queue and wait for clients.  */    listen(server_sockfd, 5);    while(1) {        char ch;        printf("server waiting\n");/*  Accept a connection.  */        client_len = sizeof(client_address);        client_sockfd = accept(server_sockfd,             (struct sockaddr *)&client_address, &client_len);/*  We can now read/write to client on client_sockfd.  */        read(client_sockfd, &ch, 1);        ch++;        write(client_sockfd, &ch, 1);        close(client_sockfd);    }}

  第二個伺服器用戶端的例子:

client

/*  Make the necessary includes and set up the variables.  */#include <sys/types.h>#include <sys/socket.h>#include <stdio.h>#include <netinet/in.h>#include <arpa/inet.h>#include <unistd.h>#include <stdlib.h>int main(){    int sockfd;    int len;    struct sockaddr_in address;    int result;    char ch = 'A';/*  Create a socket for the client.  */    sockfd = socket(AF_INET, SOCK_STREAM, 0);/*  Name the socket, as agreed with the server.  */    address.sin_family = AF_INET;    address.sin_addr.s_addr = inet_addr("127.0.0.1");    address.sin_port = htons(9734);    len = sizeof(address);/*  Now connect our socket to the server's socket.  */    result = connect(sockfd, (struct sockaddr *)&address, len);    if(result == -1) {        perror("oops: client3");        exit(1);    }/*  We can now read/write via sockfd.  */    write(sockfd, &ch, 1);    read(sockfd, &ch, 1);    printf("char from server = %c\n", ch);    close(sockfd);    exit(0);}

  server

/*  Make the necessary includes and set up the variables.  */#include <sys/types.h>#include <sys/socket.h>#include <stdio.h>#include <netinet/in.h>#include <arpa/inet.h>#include <unistd.h>#include <stdlib.h>int main(){    int server_sockfd, client_sockfd;    int server_len, client_len;    struct sockaddr_in server_address;    struct sockaddr_in client_address;/*  Remove any old socket and create an unnamed socket for the server.  */    server_sockfd = socket(AF_INET, SOCK_STREAM, 0);/*  Name the socket.  */    server_address.sin_family = AF_INET;    server_address.sin_addr.s_addr = htonl(INADDR_ANY);    server_address.sin_port = htons(9734);    server_len = sizeof(server_address);    bind(server_sockfd, (struct sockaddr *)&server_address, server_len);/*  Create a connection queue and wait for clients.  */    listen(server_sockfd, 5);    while(1) {        char ch;        printf("server waiting\n");/*  Accept a connection.  */        client_len = sizeof(client_address);        client_sockfd = accept(server_sockfd,             (struct sockaddr *)&client_address, &client_len);/*  We can now read/write to client on client_sockfd.  */        read(client_sockfd, &ch, 1);        ch++;        write(client_sockfd, &ch, 1);        close(client_sockfd);    }}

  

/*  As usual, make the appropriate includes and declare the variables.  */#include <netinet/in.h>#include <arpa/inet.h>#include <unistd.h>#include <netdb.h>#include <stdio.h>#include <stdlib.h>int main(int argc, char *argv[]){    char *host, **names, **addrs;    struct hostent *hostinfo;/*  Set the host in question to the argument supplied with the getname call,    or default to the user's machine.  */    if(argc == 1) {        char myname[256];        gethostname(myname, 255);        host = myname;    }    else        host = argv[1];/*  Make the call to gethostbyname and report an error if no information is found.  */    hostinfo = gethostbyname(host);    if(!hostinfo) {        fprintf(stderr, "cannot get info for host: %s\n", host);        exit(1);    }/*  Display the hostname and any aliases it may have.  */    printf("results for host %s:\n", host);    printf("Name: %s\n", hostinfo -> h_name);    printf("Aliases:");    names = hostinfo -> h_aliases;    while(*names) {        printf(" %s", *names);        names++;    }    printf("\n");/*  Warn and exit if the host in question isn't an IP host.  */    if(hostinfo -> h_addrtype != AF_INET) {        fprintf(stderr, "not an IP host!\n");        exit(1);    }/*  Otherwise, display the IP address(es).  */    addrs = hostinfo -> h_addr_list;    while(*addrs) {        printf(" %s", inet_ntoa(*(struct in_addr *)*addrs));        addrs++;    }    printf("\n");    exit(0);}

  

/*  Start with the usual includes and declarations.  */#include <sys/socket.h>#include <netinet/in.h>#include <netdb.h>#include <stdio.h>#include <unistd.h>#include <stdlib.h>int main(int argc, char *argv[]){    char *host;    int sockfd;    int len, result;    struct sockaddr_in address;    struct hostent *hostinfo;    struct servent *servinfo;    char buffer[128];    if(argc == 1)        host = "localhost";    else        host = argv[1];/*  Find the host address and report an error if none is found.  */    hostinfo = gethostbyname(host);    if(!hostinfo) {        fprintf(stderr, "no host: %s\n", host);        exit(1);    }/*  Check that the daytime service exists on the host.  */    servinfo = getservbyname("daytime", "tcp");    if(!servinfo) {        fprintf(stderr,"no daytime service\n");        exit(1);    }    printf("daytime port is %d\n", ntohs(servinfo -> s_port));/*  Create a socket.  */    sockfd = socket(AF_INET, SOCK_STREAM, 0);/*  Construct the address for use with connect...  */    address.sin_family = AF_INET;    address.sin_port = servinfo -> s_port;    address.sin_addr = *(struct in_addr *)*hostinfo -> h_addr_list;    len = sizeof(address);/*  ...then connect and get the information.  */    result = connect(sockfd, (struct sockaddr *)&address, len);    if(result == -1) {        perror("oops: getdate");        exit(1);    }    result = read(sockfd, buffer, sizeof(buffer));    buffer[result] = '\0';    printf("read %d bytes: %s", result, buffer);    close(sockfd);    exit(0);}

  getdate-udp

/*  Start with the usual includes and declarations.  */#include <sys/socket.h>#include <netinet/in.h>#include <netdb.h>#include <stdio.h>#include <unistd.h>#include <stdlib.h>int main(int argc, char *argv[]){    char *host;    int sockfd;    int len, result;    struct sockaddr_in address;    struct hostent *hostinfo;    struct servent *servinfo;    char buffer[128];    if(argc == 1)        host = "localhost";    else        host = argv[1];/*  Find the host address and report an error if none is found.  */    hostinfo = gethostbyname(host);    if(!hostinfo) {        fprintf(stderr, "no host: %s\n", host);        exit(1);    }/*  Check that the daytime service exists on the host.  */    servinfo = getservbyname("daytime", "udp");    if(!servinfo) {        fprintf(stderr,"no daytime service\n");        exit(1);    }    printf("daytime port is %d\n", ntohs(servinfo -> s_port));/*  Create a UDP socket.  */    sockfd = socket(AF_INET, SOCK_DGRAM, 0);/*  Construct the address for use with sendto/recvfrom...  */    address.sin_family = AF_INET;    address.sin_port = servinfo -> s_port;    address.sin_addr = *(struct in_addr *)*hostinfo -> h_addr_list;    len = sizeof(address);    result = sendto(sockfd, buffer, 1, 0, (struct sockaddr *)&address, len);    result = recvfrom(sockfd, buffer, sizeof(buffer), 0, (struct sockaddr *)&address, &len);    buffer[result] = '\0';    printf("read %d bytes: %s", result, buffer);    close(sockfd);    exit(0);}

  

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.