1、sock.h
socket常用函数
int ACCEPT(int,struct sockaddr*,socklen_t *);
int SOCKET(int,int,int);
ssize_t RECV(int,void*,size_t,int);
int CONNECT(int, const struct sockaddr *,socklen_t);
int BIND(int, const struct sockaddr *,socklen_t);
int LISTEN(int, int backlog);
ssize_t SEND(int, const void *, size_t, int);
网络初始化函数
int net_initializer(const char*,int,int);
首次连接回复函数
int first_response(client_info);
获取时间函数
int get_time(char* tm);
总代码
#ifndef _MYSOCK_
#define _MYSOCK_
#include<sys/socket.h>
#include<arpa/inet.h>
#include<string.h>
#include<netdb.h>
#include<errno.h>
#include<time.h>
#include<stdio.h>
#endiftypedef struct
{int sockfd;int port;char ip[16];
}client_info;int ACCEPT(int,struct sockaddr*,socklen_t *);
int SOCKET(int,int,int);
ssize_t RECV(int,void*,size_t,int);
int CONNECT(int, const struct sockaddr *,socklen_t);
int BIND(int, const struct sockaddr *,socklen_t);
int LISTEN(int, int backlog);
ssize_t SEND(int, const void *, size_t, int);int net_initializer(const char*,int,int);
int first_response(client_info);
int test_business(int);
int get_time(char* tm);
2、sock.c
ACCEPT()
ACCEPT(服务端socket,客户端的网络信息结构体,客户端网络信息结构体长度) ——客户端接收数据
int ACCEPT(int sockfd,struct sockaddr* addr,socklen_t * addrlen)
{int fd; if((fd=accept(sockfd,addr,addrlen))==-1){perror("accept call failed");return fd; } return fd;
}
SOCKET()
SOCKET(socket协议,)
int SOCKET(int domain,int type,int protocal)
{int fd; if((fd=socket(domain,type,protocal))==-1){perror("socket call failed");return fd; } return fd;
}
#include<sock.h>int ACCEPT(int sockfd,struct sockaddr* addr,socklen_t * addrlen)
{int fd; if((fd=accept(sockfd,addr,addrlen))==-1){perror("accept call failed");return fd; } return fd;
}int SOCKET(int domain,int type,int protocal)
{int fd; if((fd=socket(domain,type,protocal))==-1){perror("socket call failed");return fd; } return fd;
}ssize_t RECV(int sockfd,void* buf,size_t len,int flag)
{ssize_t fd; if((fd=recv(sockfd,buf,len,flag))==-1){perror("recv call failed");return fd; } return fd;
}
int CONNECT(int sockfd, const struct sockaddr *addr,socklen_t addrlen)
{int fd;if((fd=connect(sockfd,addr,addrlen))==-1){perror("connect call failed");return fd;}return fd;
}int BIND(int sockfd, const struct sockaddr *addr,socklen_t addrlen)
{int fd;if((fd=bind(sockfd,addr,addrlen))==-1){perror("bind call failed");return fd;}return fd;
}int LISTEN(int sockfd, int backlog)
{int fd;if((fd=listen(sockfd, backlog))==-1){perror("listen call failed");return fd;}return fd;
}
ssize_t SEND(int sockfd, const void *buf, size_t len, int flags)
{ssize_t fd;if((fd=send(sockfd,buf,len,flags))==-1){perror("send call failed");return fd;}return fd;
}int net_initializer(const char* ip,int port,int backlog)
{int sockfd;struct sockaddr_in addr;bzero(&addr,sizeof(addr));addr.sin_family=AF_INET;addr.sin_port=htons(port);addr.sin_addr.s_addr=inet_addr("82.157.31.74");sockfd=SOCKET(AF_INET,SOCK_STREAM,0);BIND(sockfd,(struct sockaddr*)&addr,sizeof(addr));LISTEN(sockfd,backlog);return sockfd;
}
int get_time(char* tm)
{bzero(tm,1024);time_t tp;tp=time(NULL);ctime_r(&tp,tm);tm[strlen(tm)-1]='\0';return 0;
}int first_response(client_info cf)
{char response[1500];bzero(response,1500);char tm[1024];get_time(tm);printf("Server,output info,client ip %s,client port %d\n",cf.ip,cf.port);SEND(cf.sockfd,response,strlen(response),MSG_NOSIGNAL);return 0;
}
3、生成网络动态库
将所有的.c或.cpp变成可重定位二进制文件(载入库数据时,查找可用内存,而不是固定地址)
生成可重定位的二进制文件.o
gcc sock.c -I./ -fPIC -c