辅助函数:Linux TCP socket

Linux下辅助函数 TCP socket:
/* socklib.c
 *
 * This file contains functions lots when writing internat
 * client/server programs. The two main functions here are:
 *
 * int make_srver_socket(portnum) return server socket
 *
 * int make_server_socket_q(portnum,backlog)
 *
 * int connect_to_server(char *hostname,int portnum)
 
*/

#include 
< stdio.h >
#include 
< unistd.h >
#include 
< sys / types.h >
#include 
< sys / socket.h >
#include 
< netinet / in .h >
#include 
< netdb.h >
#include 
< time.h >
#include 
< string .h >

#define  HOSTLEN 256
#define  BACKLOG 1

int  make_server_socket_q( int , int );

int  make_server_socket( int  portnum)  {
    
return make_server_socket_q(portnum,BACKLOG);
}


int  make_server_socket_q( int  portnum, int  backlog)  {
    
struct sockaddr_in saddr;            // build out address here
    struct hostent *hp;                // this is part of our
    char hostname[HOSTLEN];                // address
    int sock_id;                    // the socket

    sock_id 
= socket(PF_INET,SOCK_STREAM,0);    // get a socket
    if(sock_id == -1{
        perror(
"Creare socket error");
        
return -1;
    }


    
// build address and build it to socket
    bzero((void *)&saddr,sizeof(saddr));        // clear out struct
    gethostname(hostname,HOSTLEN);            // where am I?
    hp = gethostbyname(hostname);            // get info about host

    bcopy((
void *)hp -> h_addr,(void *)&saddr.sin_addr,
        hp 
-> h_length);
    saddr.sin_port 
= htons(portnum);        // fille in socket port
    if(bind(sock_id,(struct sockaddr *)&saddr,
        
sizeof(saddr)) != 0{
        perror(
"Bind socket error");
        
return -1;
    }

    
    
// arrange for incoming calls
    if(listen(sock_id,backlog) != 0{
        perror(
"Listen socker error");
        
return -1;
    }

    
    
return sock_id;
}


int  connect_to_server( char   * host, int  portnum)  {
    
int sock;                    
    
struct sockaddr_in servadd;            // the number to call
    struct hostent *hp;                // used to get number

    
// Step 1:Get a socket
    sock = socket(AF_INET,SOCK_STREAM,0);
    
if(sock == -1{
        perror(
"Create socket error");
        
return -1;
    }

    
    
// Step 2:connect to server
    bzero(&servadd,sizeof(servadd));        // zero the address
    hp = gethostbyname(host);            // lookup host's ip #
    if(hp == NULL)
        
return -1;
    bcopy(hp 
-> h_addr,(struct sockaddr *)&servadd.sin_addr,
        hp 
-> h_length);
    servadd.sin_port 
= htons(portnum);        // fill in port number
    servadd.sin_family = AF_INET;            // fill in socket type

    
if(connect(sock,(struct sockaddr *)&servadd,
        
sizeof(servadd)) != 0{
        perror(
"Connect socket error");
        
return -1;
    }

    
    
return sock;
}

 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值