C-socklib.c

/* socklib.c
 * This file contains functions used lots when writing internet Client/Server programs. The two main function here are:
 * -------int make_server_socket(portnum) return a server socket or -1 if error
 * -------int make_server_socket_q(portnum,backlog)
 * -------int connect_to_server(char * hostname,int portnum) return a connected socket or -1 if error
 */
 #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 <strings.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 our 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)
         return -1;

     /* build address andbind it to socket */
     bzero((void *)&saddr,sizeof(saddr));    /* clear out struct */
     gethostname(hostname,HOSTLEN);            /* where am I ? */
     hp = gethostbyname(hostname);            /* get infoabout host fill in host part */

     bcopy((void *)hp -> h_addr,(void *)&saddr.sin_addr,hp->h_length);
     saddr.sin_port = htons(portnum);        /* fill in socket port */
     saddr.sin_family = AF_INET;                /* fill in addr family */
     if(bind(sock_id,(struct sockaddr *)&saddr,sizeof(saddr)) != 0)
         return -1;

     /* arrange for incoming calls */
     if(listen(sock_id,backlog) != 0)
         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)
         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)
         return -1;
     return sock;
 }
时间: 2024-10-07 18:15:24

C-socklib.c的相关文章

Linux_C socket server.c clinet.c功能分开写

socklib.h1 int make_server_socket(int ); 2 int connect_to_server(char* , int ); 3 int process_request(int ); 4 int talk_with_server(int ); socklib.c 1 /* socklib.c 2 * This file contains functions used lots when writing internet 3 * client/ server pr

27 GroupSock概述(一)——live555源码阅读(四)网络

27 GroupSock概述(一)——live555源码阅读(四)网络 27 GroupSock概述(一)——live555源码阅读(四)网络 简介 1.网络通用数据类型定义 2.Tunnel隧道封装 本文由乌合之众 lym瞎编,欢迎转载 blog.cnblogs.net/oloroso 本文由乌合之众 lym瞎编,欢迎转载 my.oschina.net/oloroso 简介 group是组/群的意思,socket是网络接口的代名词了.这个部分很庞大,主要是与网络相关的.而live555的网络模