#include <ctime>
#include <iostream>
#include <string>
#include <boost/asio.hpp>
using boost::asio::ip::tcp;
std::string make_daytime_string()
{
using namespace std;
time_t now = time(0);
return ctime(&now);
}
int _tmain(int argc, _TCHAR* argv[])
{
try
{
boost::asio::io_service io_service;
//新建一个asio::ip::tcp::acceptor对象来监听新的连接.我们监听TCP端口13,IP版本为V4
/*
*以下构造函数相当于以下步骤
* basic_socket_acceptor<tcp> acceptor(io_service);
* acceptor.open(tcp::v4());
* acceptor.bind(tcp::endpoint(13));
* acceptor.listen(0);//default
*/
tcp::acceptor acceptor(io_service,tcp::endpoint(tcp::v4(), 13));
//basic_endpoint(const Protocol& protocol, unsigned short port_num)
//此构造函数一般作为服务器接受连接使用,ip地址即为INADDR_ANY
//这是一个iterative server,也就是说同一时间只能处理一个连接.
//建立一个socket来表示一个和客户端的连接, 然后等待客户端的连接
for(;;)
{
tcp::socket socket(io_service);
acceptor.accept(socket);
std::string message = make_daytime_string();
boost::asio::write(socket,boost::asio::buffer(message),
boost::asio::transfer_all(),boost::asio::ignore_error());
}
}
catch (std::exception& e)
{
std::cerr << e.what() << std::endl;
}
return 0;
}
#include <iostream>
#include <string>
#include <boost/asio.hpp>
using boost::asio::ip::tcp;
std::string make_daytime_string()
{
using namespace std;
time_t now = time(0);
return ctime(&now);
}
int _tmain(int argc, _TCHAR* argv[])
{
try
{
boost::asio::io_service io_service;
//新建一个asio::ip::tcp::acceptor对象来监听新的连接.我们监听TCP端口13,IP版本为V4
/*
*以下构造函数相当于以下步骤
* basic_socket_acceptor<tcp> acceptor(io_service);
* acceptor.open(tcp::v4());
* acceptor.bind(tcp::endpoint(13));
* acceptor.listen(0);//default
*/
tcp::acceptor acceptor(io_service,tcp::endpoint(tcp::v4(), 13));
//basic_endpoint(const Protocol& protocol, unsigned short port_num)
//此构造函数一般作为服务器接受连接使用,ip地址即为INADDR_ANY
//这是一个iterative server,也就是说同一时间只能处理一个连接.
//建立一个socket来表示一个和客户端的连接, 然后等待客户端的连接
for(;;)
{
tcp::socket socket(io_service);
acceptor.accept(socket);
std::string message = make_daytime_string();
boost::asio::write(socket,boost::asio::buffer(message),
boost::asio::transfer_all(),boost::asio::ignore_error());
}
}
catch (std::exception& e)
{
std::cerr << e.what() << std::endl;
}
return 0;
}
下面是winsock api 所写:
#include <iostream>
#include <string>
#include <ctime>
#include <winsock2.h>
#pragma comment(lib,"ws2_32.lib")
std::string make_daytime_string()
{
using namespace std;
time_t now = time(0);
return ctime(&now);
}
int main(int argc, char* argv[])
{
WSADATA wsaData;
if(WSAStartup(MAKEWORD(2,2),&wsaData) != 0)
{
std::cout << " lib error ";
return -1;
}
SOCKET listenSocket;
if((listenSocket = socket(AF_INET, SOCK_STREAM,0)) == INVALID_SOCKET)
{
std::cout << "socket error ";
return -1;
}
SOCKADDR_IN addr;
memset(&addr,0, sizeof(addr));
addr.sin_family = AF_INET;
addr.sin_port = htons(13);
addr.sin_addr.s_addr = htonl(ADDR_ANY);
if(bind(listenSocket,(sockaddr*)&addr,sizeof(addr)) == SOCKET_ERROR)
{
std::cout << "bind error ";
}
listen(listenSocket,5);
for(;;)
{
SOCKET socket = accept(listenSocket,0,0);
std::string timeStr = make_daytime_string();
send(socket,timeStr.c_str(),timeStr.length(),0);
#include <string>
#include <ctime>
#include <winsock2.h>
#pragma comment(lib,"ws2_32.lib")
std::string make_daytime_string()
{
using namespace std;
time_t now = time(0);
return ctime(&now);
}
int main(int argc, char* argv[])
{
WSADATA wsaData;
if(WSAStartup(MAKEWORD(2,2),&wsaData) != 0)
{
std::cout << " lib error ";
return -1;
}
SOCKET listenSocket;
if((listenSocket = socket(AF_INET, SOCK_STREAM,0)) == INVALID_SOCKET)
{
std::cout << "socket error ";
return -1;
}
SOCKADDR_IN addr;
memset(&addr,0, sizeof(addr));
addr.sin_family = AF_INET;
addr.sin_port = htons(13);
addr.sin_addr.s_addr = htonl(ADDR_ANY);
if(bind(listenSocket,(sockaddr*)&addr,sizeof(addr)) == SOCKET_ERROR)
{
std::cout << "bind error ";
}
listen(listenSocket,5);
for(;;)
{
SOCKET socket = accept(listenSocket,0,0);
std::string timeStr = make_daytime_string();
send(socket,timeStr.c_str(),timeStr.length(),0);
closesocket(socket);
}
}
}
}