欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

网络编程client和server

程序员文章站 2022-05-11 17:49:30
翻译了那么多章《beej’s guide network to programming》我们有必要自己动手写一个简单的客户端和服务器端的程序!   &...

翻译了那么多章《beej’s guide network to programming》我们有必要自己动手写一个简单的客户端和服务器端的程序!

 


        client side:

                           getaddrinfo() -> socket() -> connect() -> recv()


        code:
#include <stdio.h>  
#include <string.h>  
#include <sys/types.h>  
#include <sys/socket.h>  
#include <arpa/inet.h>  
#include <netdb.h>  
 
#define servip   "192.168.56.112"  
#define servport "2349"  
 
int main(int argc, char *argv[]) 

    struct addrinfo hints, *res; 
    int status; 
    int sockfd; 
    int connsta; 
     
    int recvsta; 
    char buffer[1024]; 
    int len; 
     
    char ipstr[inet_addrstrlen]; 
    void *addr; 
     
    if (argc != 3) { 
        fprintf(stderr, "usage: not server ip and port!"); 
        return 1; 
    } 
     
    memset(&hints, 0, sizeof(hints)); 
    hints.ai_family = af_unspec; 
    hints.ai_socktype = sock_stream; 
     
    status = getaddrinfo(argv[1], argv[2], &hints, &res); 
     
    if (status != 0) { 
        fprintf(stderr, "error, server ip and port!"); 
        return 2; 
    } 
     
    sockfd = socket(res->ai_family, res->ai_socktype, res->ai_protocol); 
     
    if (sockfd == 0) { 
        fprintf(stderr, "create, socket fail!"); 
        return 3;    
    } 
     
    connsta = connect(sockfd, res->ai_addr, res->ai_addrlen); 
     
    if (connsta != 0) { 
        fprintf(stderr, "connect, remote host fail!"); 
        return 4; 
    } 
    else { 
        struct sockaddr_in *ipv4 = (struct sockaddr_in *)res->ai_addr; 
        addr = &(ipv4->sin_addr); 
 
        inet_ntop(res->ai_family, addr, ipstr, sizeof(ipstr)); 
             
        printf("linked: host = %s, port = %d\n", ipstr, ntohs(ipv4->sin_port));   
    } 
     
     
    recvsta = recv(sockfd, buffer, sizeof(buffer), 0); 
     
    printf("\n%s\n", buffer); 
     
    freeaddrinfo(res); 
    close(sockfd); 
     
    return 0; 

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

#define servip   "192.168.56.112"
#define servport "2349"

int main(int argc, char *argv[])
{
 struct addrinfo hints, *res;
 int status;
 int sockfd;
 int connsta;
 
 int recvsta;
 char buffer[1024];
 int len;
 
 char ipstr[inet_addrstrlen];
 void *addr;
 
 if (argc != 3) {
  fprintf(stderr, "usage: not server ip and port!");
  return 1;
 }
 
 memset(&hints, 0, sizeof(hints));
 hints.ai_family = af_unspec;
 hints.ai_socktype = sock_stream;
 
 status = getaddrinfo(argv[1], argv[2], &hints, &res);
 
 if (status != 0) {
  fprintf(stderr, "error, server ip and port!");
  return 2;
 }
 
 sockfd = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
 
 if (sockfd == 0) {
  fprintf(stderr, "create, socket fail!");
  return 3; 
 }
 
 connsta = connect(sockfd, res->ai_addr, res->ai_addrlen);
 
 if (connsta != 0) {
  fprintf(stderr, "connect, remote host fail!");
  return 4;
 }
 else {
  struct sockaddr_in *ipv4 = (struct sockaddr_in *)res->ai_addr;
  addr = &(ipv4->sin_addr);

  inet_ntop(res->ai_family, addr, ipstr, sizeof(ipstr));
   
  printf("linked: host = %s, port = %d\n", ipstr, ntohs(ipv4->sin_port)); 
 }
 
 
 recvsta = recv(sockfd, buffer, sizeof(buffer), 0);
 
 printf("\n%s\n", buffer);
 
 freeaddrinfo(res);
 close(sockfd);
 
 return 0;
}

          server side:

                               getaddrinfo() -> socket() -> bind() -> listen() -> accept() -> send()

         code:

         #include <stdio.h>  
#include <string.h>  
#include <sys/types.h>  
#include <sys/socket.h>  
#include <netdb.h>  
#include <arpa/inet.h>  
 
#define servport "2349"  
 
int main(int argc, char *argv[]) 

    struct addrinfo hints, *res; 
    int status; 
    int sockfd; 
     
    int connfd; 
    struct sockaddr_in cliaddr; 
     
    int sendsta; 
    char *msg; 
     
    if (argc != 2) { 
        fprintf(stderr, "usage: not found read file"); 
        return 1; 
    } 
     
    memset(&hints, 0, sizeof(hints)); 
    hints.ai_family = af_unspec; 
    hints.ai_socktype = sock_stream; 
    hints.ai_flags = ai_passive; 
     
    status = getaddrinfo(null, servport, &hints, &res); 
     
    if (status != 0) { 
        fprintf(stderr, "getaddrinfo, fail!"); 
        return 2; 
    } 
     
    sockfd = socket(res->ai_family, res->ai_socktype, res->ai_protocol); 
     
    bind(sockfd, res->ai_addr, res->ai_addrlen); 
     
    listen(sockfd, 5); 
     
    printf("======== please wait client =========\n"); 
     
    while(1) { // loop forever!  
        char ipstr[inet_addrstrlen]; 
        void *addr; 
         
        int len = sizeof(cliaddr); 
        connfd = accept(sockfd, (struct sockaddr *)&cliaddr, &len); 
         
        /* view client ip */ 
        struct sockaddr_in *ipv4 = (struct sockaddr_in *)&cliaddr; 
        addr = &(ipv4->sin_addr); 
     
        inet_ntop(af_inet, addr, ipstr, sizeof(ipstr)); 
        printf("client: %s\n", ipstr); 
         
        /* copy data */ 
        msg = "hello world!"; 
        sendsta = send(connfd, msg, strlen(msg), 0); 
         
        close(connfd); 
    } 
     
    close(sockfd); 
    return 0; 

 摘自 xiaobin_hlj80的专栏