zoukankan      html  css  js  c++  java
  • open_clientfd(char* hostname,int port)和open_listenfd(int port)

    #include <stdio.h>
    #include <stdlib.h>
    #include <unistd.h>
    #include <string.h>
    #include <errno.h>
    #include <netdb.h>
    #include <sys/socket.h>
    #include <netinet/in.h>
    #include <sys/types.h>
    #include <sys/wait.h>
    
    typedef struct sockaddr SA;
    
    int open_clientfd(char* hostname,int port)
    {
        int clientfd;
        struct hostent * hp;
        struct sockaddr_in serveraddr;
        char* msg;
        
        /*创建socket*/
        if((clientfd=socket(AF_INET,SOCK_STREAM,0))<0)
        {
            msg="client socket() error!
    ";
            write(STDOUT_FILENO,msg,strlen(msg));
            return -1;
        }
        msg="client socket() success!
    ";
        write(STDOUT_FILENO,msg,strlen(msg));
        
        /*填写server的ip地址和port*/
        if((hp=gethostbyname(hostname))==NULL)
        {
            msg="client gethostbyname() error!
    ";
            write(STDOUT_FILENO,msg,strlen(msg));
            return -2;
        }
        msg="client gethostbyname() success!
    ";
        write(STDOUT_FILENO,msg,strlen(msg));
        
        bzero((char*)&serveraddr,sizeof(serveraddr));
        serveraddr.sin_family=AF_INET;
        serveraddr.sin_port=htons(port);
        bcopy((char*)hp->h_addr_list[0],(char*)&serveraddr.sin_addr.s_addr,hp->h_length);
        
        /*建立连接*/
        if(connect(clientfd,(SA*)&serveraddr,sizeof(serveraddr))<0)
        {
            msg="client connect() error!
    ";
            write(STDOUT_FILENO,msg,strlen(msg));
            return -3;
        }
        msg="client connect() success!
    ";
        write(STDOUT_FILENO,msg,strlen(msg));
        
        return clientfd;
    }
    
    int open_listenfd(int port)
    {
        int listenfd,optval;
        struct sockaddr_in serveraddr;
        char* msg;
        
        /*创建socket*/
        if((listenfd=socket(AF_INET,SOCK_STREAM,0))<0)
        {
            msg="server socket() error!
    ";
            write(STDOUT_FILENO,msg,strlen(msg));
            return -1;
        }
        msg="server socket() success!
    ";
        write(STDOUT_FILENO,msg,strlen(msg));
        
        /*配置服务器使得它能被立即终止和重启*/
        if(setsockopt(listenfd,SOL_SOCKET,SO_REUSEADDR,(const void *)&optval,sizeof(int))<0)
        {
            msg="server setsockopt() error!
    ";
            write(STDOUT_FILENO,msg,strlen(msg));
            return -2;
        }
        msg="server setsockopt() success!
    ";
        write(STDOUT_FILENO,msg,strlen(msg));
        
        /*绑定服务端的ip和端口*/
        bzero((char*)&serveraddr,sizeof(serveraddr));
        serveraddr.sin_family=AF_INET;
        serveraddr.sin_port=htons((unsigned short)port);
        serveraddr.sin_addr.s_addr=htonl(INADDR_ANY);
        if(bind(listenfd,(SA*)&serveraddr,sizeof(serveraddr))<0)
        {
            msg="server bind() error!
    ";
            write(STDOUT_FILENO,msg,strlen(msg));
            return -3;
        }
        msg="server bind() success!
    ";
        write(STDOUT_FILENO,msg,strlen(msg));
        
        /*创建listen*/
        if(listen(listenfd,1024)<0)
        {
            msg="server listen() error!
    ";
            write(STDOUT_FILENO,msg,strlen(msg));
            return -3;
        }
        msg="server listen() success!
    ";
        write(STDOUT_FILENO,msg,strlen(msg));
        
        return listenfd;
    }
  • 相关阅读:
    [RN] React Native 中使用 stickyHeaderIndices 实现 ScrollView 的吸顶效果
    [PHP] layui实现多图上传,图片自由排序,自由删除
    [RN] React Native 下拉放大动画
    [RN] React Native 实现 多选标签
    [RN] React Native 让 Flatlist 支持 选中多个值,并获取所选择的值
    [RN] React Native代码转换成微信小程序代码的转换引擎工具
    【python小练】0004
    【python小练】0001
    【python小练】0000
    【LeetCode】108. Convert Sorted Array to Binary Search Tree
  • 原文地址:https://www.cnblogs.com/jiayith/p/3581215.html
Copyright © 2011-2022 走看看