Linux程序设计:套接字

发布时间:2024年01月23日

Linux套接字(Socket)是在操作系统中用于进程间通信的机制。

socket函数对应于普通文件的打开操作。普通文件的打开操作返回一个文件描述字,而socket()用于创建一个socket描述符(socket descriptor),它唯一标识一个socket。这个socket描述字跟文件描述字一样,后续的操作都有用到它,把它作为参数,通过它来进行一些读写操作。

客户端代码模拟:?

#include<unistd.h>
#include<stdlib.h>
#include<stdio.h>
#include<string.h>
#include<sys/shm.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<sys/un.h>

int main(){
    int sockfd ;
    int len ;
    struct sockaddr_un address ;
    int result ;
    char ch ='A';

    sockfd = socket(AF_UNIX,SOCK_STREAM,0);

    address.sun_family = AF_UNIX ;
    strcpy(address.sun_path,"server_socket"); 
    len = sizeof(address);
    
    result = connect(sockfd,(struct sockaddr *)&address,len);
    if(result ==-1){
      printf("ops: client1 ret= %x\n",result);
      perror("oops: client1 ");
      exit(EXIT_FAILURE);
    }

    write(sockfd,&ch,1);
    read(sockfd,&ch,1);
    printf("char from server =%c\n",ch);
    close(sockfd);
    exit(0);

}

服务端代码模拟:?

#include<unistd.h>
#include<stdlib.h>
#include<stdio.h>
#include<string.h>
#include<sys/shm.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<sys/un.h>

int main(){
    int server_sockfd , client_sockfd ;
    int server_len,client_len ;
    struct sockaddr_un server_address ;
    struct sockaddr_un client_address ;

    unlink("server_socket");
    server_sockfd = socket(AF_UNIX,SOCK_STREAM,0);
    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);

    listen(server_sockfd,5);
    while (1)
    {
        char ch ;
        printf("server waiting\n");
        client_len = sizeof(client_address);
        client_sockfd = accept(server_sockfd,
        (struct sockaddr * )&client_address,(socklen_t *)&client_len);

        read(client_sockfd,&ch,1);
        ch++ ;
        write(client_sockfd,&ch,1);
        close(client_sockfd);
    }
    
}

测试入口:?

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

int main(int argc ,char *argv[]){

   char *host , **names ,**addrs ;
   struct hostent *hostinfo ;

   if(argc ==1){
     char myname[256];
     gethostname(myname,255);
     host = myname ;
   }else {
    host = argv[1] ;
   }

   hostinfo =  gethostbyname(host); 
   if(!hostinfo){
     fprintf(stderr,"cannot get info for host:%s\n",host);
     exit(1);
   }
   
    printf("result 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");

    if(hostinfo->h_addrtype != AF_INET){
        fprintf(stderr,"not an IP host \n ");
        exit(1);
    }

    addrs = hostinfo -> h_addr_list ;
    while (*addrs)
    {
       printf(" %s",inet_ntoa(*(struct in_addr *)*addrs));
       addrs++ ;
    }
    
    printf("\n");
    exit(0);
    
}

文章来源:https://blog.csdn.net/Liang123l/article/details/135771572
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。