const unsigned int MySOCKET_DEFAULT_PORT= 4096;
mylinuxsocket::mylinuxsocket()
{
m_bstate_connect = false;
address_family=AF_INET;
socket_type=SOCK_STREAM;
protocol_family=IPPROTO_TCP;
port_number = MySOCKET_DEFAULT_PORT;
mysocket = -1;
socket_error = MySOCKET_NO_ERROR;
bytes_send = 0;
bytes_read
= 0;
mylinuxsocket::mylinuxsocket()
{
}
//构造函数:指定端口,套接字类型,端口名,创建套接字
mylinuxsocket::mylinuxsocket(int port, int stype, char* strhostname)
{
bytes_send = 0;
bytes_read
= 0;
m_bstate_connect = false;
socket_error=MySOCKET_NO_ERROR;
address_family=AF_INET;
port_number=port;
if (stype == SOCK_STREAM)
//判断套接字的类型
{
socket_type=SOCK_STREAM;
protocol_family=IPPROTO_TCP;
}
//构造函数:指定端口,套接字类型,端口名,创建套接字
mylinuxsocket::mylinuxsocket(int port, int stype, char* strhostname)
{
}
//构造函数:指定协议家族, 地址家族,端口,套接字类型,端口名,创建套接字
mylinuxsocket::mylinuxsocket(sa_family_t af,int stype,int pro_family,
int port,char *strhostname)
{
bytes_send = 0;
bytes_read
= 0;
address_family = af;
socket_type = stype;
protocol_family = pro_family;
port_number = port;
sockaddr_sin.sin_family = address_family;
socket_error = MySOCKET_NO_ERROR;
m_bstate_connect = false;
if (strhostname) {
struct hostent *hostentnm = gethostbyname(strhostname);
if(hostentnm==(struct hostent *) 0) {
socket_error=MySOCKET_HOSTNAME_ERROR;
return -1;
}
sockaddr_sin.sin_addr.s_addr=*((unsigned long*) hostnm->h_addr);
//构造函数:指定协议家族, 地址家族,端口,套接字类型,端口名,创建套接字
mylinuxsocket::mylinuxsocket(sa_family_t af,int stype,int pro_family,
{
}
//创建初始化套接字函数,创建套接字
int mylinuxsocket::socket_create(void)
{
mysocket = =socket(address_family, socket_type, protocol_family);
if (mysocket<0)
{
socket_error=MySOCKET_INIT_ERROR;
return -1;
}
return mysocket;
int mylinuxsocket::socket_create(void)
{
}
//服务端套接字绑定监听函数, 返回值
-1:失败
0:成功
unsigned long mylinuxsocket::bindserver_listen(int maxconn_num)
{
if(bind(mysocket,(struct sockaddr*)&sockaddr_sin, sizeof(struct sockaddr*)) == -1)
{
socket_error=MySOCKET_BIND_ERROR;
return -1;
}
if(listen(Mysocket,maxconn_num) == -1)
{
socket_error=MySOCKET_LISTEN_ERROR;
return -1;
}
return 0;
unsigned long mylinuxsocket::bindserver_listen(int maxconn_num)
{
}
//客户端套接字连接函数
unsigned int mylinuxsocket::sock_connect()
{
if(connect(mysocket,(struct sockaddr*)&sin,sizeof(sin)) == -1)
{
socket_error=MySOCKET_CONNECT_ERROR;
m_bstate_connect = false;
return -1;
}
else
{
m_bstate_connect = true;
return 1;
}
unsigned int mylinuxsocket::sock_connect()
{
}
//服务端接受函数
unsigned int mylinuxsocket::sock_accept()
{
socklen_t addr_size = (socklen_t)sizeof(remote_addrsin);
conn_socket = accept(mysocket,(struct sockaddr*)&remote_addrsin , &addr_size);
if(conn_socket < 0)
{
socket_error = MySOCKET_ACCEPT_ERROR;
return -1;
}
return conn_socket;
}
unsigned int mylinuxsocket::sock_accept()
{
}
//服务端轮询监听接收操作
int mylinuxsocket::Read_Select(int maxsock,int seconds,int useconds)
{
struct timeval timeout;
fd_set readfds;
FD_ZERO(&readfds);
FD_SET(maxsock, &readfds);
timeout.tv_sec = seconds;
timeout.tv_usec = useconds;
return select(maxsock+1, &readfds, 0 ,0, &timeout);
}
int mylinuxsocket::Read_Select(int maxsock,int seconds,int useconds)
{
}
//TCP客户端接收函数
int mylinuxsocket::client_recv(void *buf, int bytes, int flags)
{
bytes_read = 0;
int num_reads = 0;
int num_requestrecv = bytes;
//需要接收的字节数
char *preceivebuffer = (char*)buf;
//循环接收需要接收的字节数
while(bytes_read < bytes){
if((num_reads = recv(mysocket, preceivebuffer, num_requestrecv - bytes_read, flags))>0)
{
bytes_read += num_read;
preceivebuffer += num_read;
}
if (num_read<0){
socket_error=MySOCKET_RECEIVE_ERROR;
return -1;
}
}
return bytes_read;
int mylinuxsocket::client_recv(void *buf, int bytes, int flags)
{
}
//TCP服务端接收函数
int mylinuxsocket::server_recv(int socket, void *buf, int bytes,int seconds,int useconds,int flags)
{
bytes_read=0;
int num_read = 0;
int num_requestrecv = (int)bytes;
char*preceivebuffer = (char*)buf;
while(bytes_read < bytes)
{
if(!ReadSelect(socket, seconds,useconds)){
socket_error=MySOCKET_REQUEST_TIMEOUT;
return -1;
}
if((num_read = recv(socket, p, num_requestrecv - bytes_read, flags))>0){
bytes_read += num_read;
preceivebuffer += num_read;
}
if(num_read < 0){
socket_error = MySOCKET_RECEIVE_ERROR;
return -1;
}
}
return bytes_read;
}
int mylinuxsocket::server_recv(int socket, void *buf, int bytes,int seconds,int useconds,int flags)
{
}
//TCP发送函数
int mylinuxsocket::sock_send(int soket,const void *buf,int bytes,int flags)
{
bytes_send = 0;
int mylinuxsocket::sock_send(int soket,const void *buf,int bytes,int flags)
{
}
//服务端UDP接收函数
int mylinuxsocket::udprecvfrom(void *buf, int bytes, int seconds, int useconds,int flags)
{
int mylinuxsocket::udprecvfrom(void *buf, int bytes, int seconds, int useconds,int flags)
{
}
//客户端UDP接收函数
int mylinuxsocket::udprecvfrom(void *buf, int bytes, int flags)
{
int mylinuxsocket::udprecvfrom(void *buf, int bytes, int flags)
{
}
//udp发送函数
int mylinuxsocket::udpSendTo(void *buf, int bytes, int flags)
{
int mylinuxsocket::udpSendTo(void *buf, int bytes, int flags)
{
}
enum MySocketError{
};
#include"mysocket.h"
#include <unistd.h>
#include <sys/types.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <sys/time.h>
#include <string.h>
#include <fcntl.h>
#include <netdb.h>
#include <unistd.h>
#include <sys/types.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <sys/time.h>
#include <string.h>
#include <fcntl.h>
#include <netdb.h>
class mylinuxsocket
{
protected:
sa_family_t address_family;
int socket_type;
int port_number;
int protocol_family;
{
public:
mylinuxsocket();
virtual ~mylinuxsocket();
mylinuxsocket(int port, int stype, char* strhostname);
mylinuxsocket(sa_family_t af,int stype,int pro_family,
int port,char *strhostname);
private:
}