#include
#include
#include
#include
#include
#include
#define CHECK_RET(q) if((q) == false){return -1;}
class TcpSocket
{
public:
TcpSocket()
:_sockfd(-1)
{}
~TcpSocket(){
Close();
}
bool Socket()
{
_sockfd = socket(AF_INET , SOCK_STREAM , IPPROTO_TCP);
if(_sockfd < 0)
{
std::cerr << "scoket error\n";
return false;
}
return true;
}
bool Bind( std::string &ip,uint16_t port)
{
struct sockaddr_in addr;
addr.sin_family = AF_FILE;
addr.sin_port = htons(port);
addr.sin_addr.s_addr = inet_addr(&ip[0]);
socklen_t len = sizeof(struct sockaddr_in);
int ret = bind(_sockfd , (struct sockaddr*)&addr , len);
if(ret <0 )
{
std::cerr << "bind error\n";
return false;
}
return true;
}
bool Listen(int backlog = 5)
{
int ret = listen(_sockfd , backlog);
if(ret < 0 )
{
std::cerr << "listen error\n";
return false;
}
return true;
}
bool Connect(const std::string &ip , uint16_t port)
{
int ret;
struct sockaddr_in addr;
addr.sin_family = AF_FILE;
addr.sin_port = htons(port);
addr.sin_addr.s_addr = inet_addr(&ip[0]);
socklen_t len = sizeof(struct sockaddr_in);
ret = connect(_sockfd , (struct sockaddr*)&addr , len);
if(ret < 0)
{
std::cerr << "connect error\n";
return false;
}
return true;
}
void SetFd(int fd)
{
_sockfd = fd;
}
int GetFd(int _sockfd)
{
return _sockfd;
}
bool Accept(TcpSocket &newsock)
{
struct sockaddr_in addr;
socklen_t len = sizeof(struct sockaddr_in);
int fd = accept(_sockfd , (struct sockaddr*)&addr , &len);
if(fd < 0)
{
std::cerr << "accept error\n";
return false;
}
newsock.SetFd(fd);
return true;
}
bool Send(std::string &buf)
{
int ret = send(_sockfd , &buf[0] , buf.size() , 0);
if(ret < 0)
{
std::cerr << "send error\n";
return false;
}
return true;
}
bool Recv(std::string &buf)
{
char tmp[4096] = {0};
int ret = recv(_sockfd , tmp , 4096 , 0);
if(ret < 0)
{
std::cerr << "recvfrom error\n";
return false;
}
else if (ret == 0)
{
std::cerr << "peer shutdown\n";
return false;
}
buf.assign(tmp, ret);
return true;
}
bool Close()
{
if(_sockfd >= 0)
{
close(_sockfd);
_sockfd = -1;
}
return true;
}
private:
int _sockfd;
};
#include
#include
#include
#include
#include
#include
#include
int main(int argc , char *argv[])
{
if(argc != 3)
{
printf("Usage:./main 192.168.115.129 9000\n");
return -1;
}
int sockfd = socket(AF_FILE , SOCK_DGRAM , IPPROTO_UDP);
if(sockfd <0 )
{
perror("socket error");
return -1;
}
struct sockaddr_in addr;
addr.sin_family = AF_INET;
addr.sin_port = htons(atoi(argv[2]));
addr.sin_addr.s_addr = inet_addr("192.168.115.129");
socklen_t len = sizeof(struct sockaddr_in);
int ret = bind(sockfd , (struct sockaddr*)&addr, len);
if(ret < 0)
{
perror("bind error");
return -1;
}
while(1)
{
char buf[1024] = {0};
struct sockaddr_in cliaddr;
len = sizeof(struct sockaddr_in);
int ret = recvfrom(sockfd , buf , 1023 , 0 ,
(struct sockaddr*)&cliaddr ,&len);
if(ret < 0)
{
perror("recvfrom error\n");
close(sockfd);
return -1;
}
printf("client say : %s\n", buf);
len = sizeof(struct sockaddr_in);
memset(buf , 0x00 , 1024);
scanf("%s" , buf);
ret = sendto(sockfd , buf , strlen(buf) , 0 ,
(struct sockaddr*)&cliaddr ,len);
if(ret < 0 )
{
perror("sendto error");
close(sockfd);
return -1;
}
}
close(sockfd);
}
#include
#include
#include
#include
#include
#include
#include
#define CHECK_RET(q) if((q) ==false ){return false;}
class UdpSocket
{
public:
UdpSocket()
:_sockfd(-1)
{}
~UdpSocket()
{
Close();
}
bool Socket()
{
_sockfd = socket(AF_INET , SOCK_DGRAM , IPPROTO_UDP);
if(_sockfd < 0)
{
std::cerr << "socket error\n";
return false;
}
return true;
}
bool Bind(const std::string &ip , uint16_t port)
{
struct sockaddr_in addr;
addr.sin_family = AF_INET;
addr.sin_port = htons(port);
addr.sin_addr.s_addr = inet_addr(ip.c_str());
socklen_t len = sizeof(struct sockaddr_in);
int ret = bind(_sockfd , (struct sockaddr*)&addr , len);
if(ret < 0)
{
std::cerr << "bind error\n";
return false;
}
return true;
}
bool Recv(std::string &buf , std::string &ip , uint16_t &port)
{
char tmp[4096];
struct sockaddr_in peeraddr;
socklen_t len = sizeof(peeraddr);
int ret = recvfrom(_sockfd , tmp , 4096 , 0 , (struct sockaddr*)&peeraddr, &len);
if(ret < 0)
{
std::cerr << "recvfrom error\n";
return false;
}
buf.assign(tmp , ret);
port = ntohs(peeraddr.sin_port);
ip = inet_ntoa(peeraddr.sin_addr);
return true;
}
bool Send(std::string &data , std::string &ip , uint16_t port)
{
struct sockaddr_in addr;
addr.sin_family = AF_INET;
addr.sin_port = htons(port);
addr.sin_addr.s_addr = inet_addr(ip.c_str());
socklen_t len = sizeof(struct sockaddr_in);
int ret = sendto(_sockfd , &data[0] , data.size() , 0 , (struct sockaddr*)&addr , len);
if(ret < 0 )
{
std::cerr << "sendto error\n";
return -1;
}
return true;
}
bool Close()
{
if(_sockfd >= 0 )
{
close(_sockfd);
_sockfd = -1;
}
return true;
}
private:
int _sockfd;
};
int main(int argc , char *argv[])
{
if(argc != 3)
{
std::cout << "./udp_cli serverip serverport\n";
return -1;
}
std::string srv_ip = argv[1];
uint16_t srv_port = atoi(argv[2]);
UdpSocket sock;
CHECK_RET(sock.Socket());
while(1)
{
std::string buf;
std::cin >> buf;
CHECK_RET(sock.Send(buf ,srv_ip, srv_port));
buf.clear();
CHECK_RET(sock.Recv(buf ,srv_ip, srv_port));
std::cout << "server sat:" << buf << "\n";
}
CHECK_RET(sock.Close());
return 0;
}
#include
#include "tcpsocket.hpp"
int main(int argc, char *argv[])
{
if(argc != 3)
{
std::cout << "./tcp_cli srvip srvport\n";
return -1;
}
std::string ip = argv[1];
uint16_t port = atoi(argv[2]);
TcpSocket sock;
CHECK_RET(sock.Socket());
CHECK_RET(sock.Connect(ip, port));
while(1)
{
std::string buf;
std::cin >> buf;
bool ret = sock.Send(buf);
if(ret == false)
{
return -1;
}
buf.clear();
ret = sock.Recv(buf);
if(ret == false)
{
return -1;
}
std::cout << "server say:" << buf << "\n";
}
sock.Close();
return 0;
}
#include
#include
#include
#include "tcpsocket.hpp"
#include
void sigcb(int signo)
{
while(waitpid(-1 , NULL , WNOHANG) > 0);
}
int main(int argc , char* argv[])
{
if(argc != 3)
{
std::cout << "./tcp_srv 192.168.115.129 9000\n";
return -1;
}
signal(SIGCHLD , sigcb);
std::string ip = argv[1];
uint16_t port = atoi(argv[2]);
TcpSocket lst_sock;
CHECK_RET(lst_sock.Socket());
CHECK_RET(lst_sock.Bind(ip , port));
CHECK_RET(lst_sock.Listen());
TcpSocket newsock;
while(1)
{
bool ret = lst_sock.Accept(newsock);
if (ret == false)
{
continue;
}
if(fork() == 0)
{
while(1)
{
std::string buf;
ret = newsock.Recv(buf);
if(ret == false)
{
newsock.Close();
continue;
}
std::cout << "client say:" << buf << "\n";
buf.clear();
std::cin >> buf;
newsock.Send(buf);
}
newsock.Close();
exit(0);
}
newsock.Close();
}
lst_sock.Close();
return 0;
}