|
|
|
|
#include <stdio.h>
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
#include <unistd.h>
|
|
|
|
|
#include <arpa/inet.h>
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
|
#include <sys/socket.h>
|
|
|
|
|
#include "network.h"
|
|
|
|
|
|
|
|
|
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
NetWork* open_network(char c_or_s,int type,char* ip,uint16_t port)
|
|
|
|
|
{
|
|
|
|
|
// <20>ڶ<EFBFBD><DAB6>ϴ<EFBFBD><CFB4><EFBFBD>NetWork<72>ṹ
|
|
|
|
|
NetWork* nw = malloc(sizeof(NetWork));
|
|
|
|
|
if(NULL == nw)
|
|
|
|
|
{
|
|
|
|
|
perror("network malloc");
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// <20><><EFBFBD><EFBFBD>socket<65><74><EFBFBD><EFBFBD>
|
|
|
|
|
nw->fd = socket(AF_INET,type,0);
|
|
|
|
|
if(0 > nw->fd)
|
|
|
|
|
{
|
|
|
|
|
perror("network socket");
|
|
|
|
|
free(nw);
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// <><D7BC>ͨ<EFBFBD>ŵ<EFBFBD>ַ
|
|
|
|
|
nw->addr.sin_family = AF_INET;
|
|
|
|
|
nw->addr.sin_port = htons(port);
|
|
|
|
|
nw->addr.sin_addr.s_addr = inet_addr(ip);
|
|
|
|
|
nw->len = sizeof(nw->addr);
|
|
|
|
|
nw->type = type;
|
|
|
|
|
|
|
|
|
|
if('s' == c_or_s)
|
|
|
|
|
{
|
|
|
|
|
if(bind(nw->fd,(SP)&nw->addr,nw->len))
|
|
|
|
|
{
|
|
|
|
|
perror("network bind");
|
|
|
|
|
free(nw);
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if(SOCK_STREAM == type && listen(nw->fd,50))
|
|
|
|
|
{
|
|
|
|
|
perror("network listen");
|
|
|
|
|
free(nw);
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else if(SOCK_STREAM == type)
|
|
|
|
|
{
|
|
|
|
|
if(connect(nw->fd,(SP)&nw->addr,nw->len))
|
|
|
|
|
{
|
|
|
|
|
perror("network connect");
|
|
|
|
|
free(nw);
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return nw;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// TCP<43><50>serverר<72><D7A8>
|
|
|
|
|
NetWork* accept_network(NetWork* nw)
|
|
|
|
|
{
|
|
|
|
|
if(SOCK_STREAM != nw->type)
|
|
|
|
|
{
|
|
|
|
|
printf("network accept socket type error!\n");
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
NetWork* clinw = malloc(sizeof(NetWork));
|
|
|
|
|
if(NULL == clinw)
|
|
|
|
|
{
|
|
|
|
|
perror("network accept malloc");
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
clinw->type = nw->type;
|
|
|
|
|
clinw->len = sizeof(clinw->addr);
|
|
|
|
|
clinw->fd = accept(nw->fd,(SP)&clinw->addr,&clinw->len);
|
|
|
|
|
if(0 > clinw->fd)
|
|
|
|
|
{
|
|
|
|
|
perror("network accept");
|
|
|
|
|
free(clinw);
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return clinw;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
int nsend(NetWork* nw,void* buf,uint32_t len)
|
|
|
|
|
{
|
|
|
|
|
if(SOCK_STREAM == nw->type)
|
|
|
|
|
{
|
|
|
|
|
return send(nw->fd,buf,len,0);
|
|
|
|
|
}
|
|
|
|
|
else if(SOCK_DGRAM == nw->type)
|
|
|
|
|
{
|
|
|
|
|
return sendto(nw->fd,buf,len,0,(SP)&nw->addr,nw->len);
|
|
|
|
|
}
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
int nrecv(NetWork* nw,void* buf,uint32_t len)
|
|
|
|
|
{
|
|
|
|
|
if(SOCK_STREAM == nw->type)
|
|
|
|
|
{
|
|
|
|
|
return recv(nw->fd,buf,len,0);
|
|
|
|
|
}
|
|
|
|
|
else if(SOCK_DGRAM == nw->type)
|
|
|
|
|
{
|
|
|
|
|
return recvfrom(nw->fd,buf,len,0,(SP)&nw->addr,&nw->len);
|
|
|
|
|
}
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// <20>ر<EFBFBD><D8B1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
void close_network(NetWork* nw)
|
|
|
|
|
{
|
|
|
|
|
if(close(nw->fd))
|
|
|
|
|
{
|
|
|
|
|
perror("network close");
|
|
|
|
|
}
|
|
|
|
|
free(nw);
|
|
|
|
|
}
|