POCO C++ SOCKET

// client program
#include "Poco/Net/DatagramSocket.h"
#include "Poco/Net/SocketAddress.h"
#include "Poco/Timestamp.h"
#include "Poco/DateTimeFormatter.h"

int testUdpClient(){
	const char* ipaddr = "192.168.81.140"; //"192.168.81.140"
 	Poco::Net::SocketAddress sa(ipaddr, 5004);
	Poco::Net::DatagramSocket dgs(Poco::Net::IPAddress::IPv4);
	dgs.connect(sa);
	//dgs.bind(sa);
	std::string syslogMsg;
	Poco::Timestamp now;
	syslogMsg = Poco::DateTimeFormatter::format(now, "<14>%w %f %H:%M:%S Hello,world!");
	dgs.sendBytes(syslogMsg.data(), syslogMsg.size());

	return 0;
}

/// server program
#include "Servers.h"
#include "Poco/Net/DatagramSocket.h"
#include "Poco/Net/SocketAddress.h"
#include "Poco/Timestamp.h"
#include "Poco/DateTimeFormatter.h"
int testDatagramSocket(){
	Poco::Net::SocketAddress sa(Poco::Net::IPAddress(), 5004);
	Poco::Net::DatagramSocket dgs(sa);
	char buffer[1024]; // 1K byte
	
	while(1){
		Poco::Net::SocketAddress sender;
		int n = dgs.receiveFrom(buffer, sizeof(buffer)-1, sender);
		buffer[n] = '\0';
		std::cout << sender.toString() << ":" << buffer << std::endl;
	}

	return 0;
}



tcp

// client program
#include "Poco/Net/SocketAddress.h"
#include "Poco/Net/StreamSocket.h"
#include "Poco/Net/SocketStream.h"
#include "Poco/StreamCopier.h"
#include <iostream>

int main(int argc, char** argv){
	const char* ipaddr = "192.168.81.140"; // the server address.
	Poco::Net::SocketAddress sa(ipaddr, 5004); // the server port.
	Poco::Net::StreamSocket socket(sa);
	Poco::Net::SocketStream str(socket);

	// Writes all bytes readable from str into std::cout, using an internal buffer.
	Poco::StreamCopier::copyStream(str, std::cout);

	return 0;
}

// server program
#include "Poco/Net/ServerSocket.h"
#include "Poco/Net/StreamSocket.h"
#include "Poco/Net/SocketStream.h"
#include "Poco/Net/SocketAddress.h"

int main(int argc, char** argv){
	Poco::Net::ServerSocket srv(5004); // does bind + listen
	for(;;){
		Poco::Net::StreamSocket ss = srv.acceptConnection();
		Poco::Net::SocketStream str(ss);

		// flush() make sure the file stream is updated with the data.
		// endl() puts a newline and then uses flush().
		str << "HTTP/1.0 200 OK\r\n"
			"Content-Type: text/html\r\n"
			"\r\n"
			"<html><head><title>My 1st Web Server</title></head>"
			"<body><h1>Hello,Wordl!</h1></body></html>"
			"\r\n"
			<< std::flush;
	}
	return 0;
}


 

C++中的Socket编程是一种用于创建网络应用程序的常见方法,它基于传输控制协议(TCP)或用户数据报协议(UDP)来实现不同的通信需求。CS架构(Client-Server架构)是一种常见的网络应用程序设计模式,其中服务器端提供服务,而客户端通过Socket连接请求服务。 在C++中进行Socket编程,可以使用操作系统提供的原生Socket API或者一些封装库,如Boost.Asio、Poco、ACE等。下面是一个简单的CS架构的Socket编程示例: 服务器端代码: ```cpp #include <iostream> #include <cstring> #include <unistd.h> #include <sys/socket.h> #include <netinet/in.h> int main() { // 创建Socket int serverSocket = socket(AF_INET, SOCK_STREAM, 0); if (serverSocket == -1) { std::cerr << "Failed to create socket" << std::endl; return 1; } // 绑定IP地址和端口 sockaddr_in serverAddress{}; serverAddress.sin_family = AF_INET; serverAddress.sin_addr.s_addr = INADDR_ANY; serverAddress.sin_port = htons(12345); if (bind(serverSocket, (struct sockaddr*)&serverAddress, sizeof(serverAddress)) == -1) { std::cerr << "Failed to bind address" << std::endl; close(serverSocket); return 1; } // 监听连接请求 if (listen(serverSocket, 5) == -1) { std::cerr << "Failed to listen" << std::endl; close(serverSocket); return 1; } // 接受客户端连接 sockaddr_in clientAddress{}; socklen_t clientAddressLength = sizeof(clientAddress); int clientSocket = accept(serverSocket, (struct sockaddr*)&clientAddress, &clientAddressLength); if (clientSocket == -1) { std::cerr << "Failed to accept client connection" << std::endl; close(serverSocket); return 1; } // 接收和发送数据 char buffer[1024]; std::memset(buffer, 0, sizeof(buffer)); if (recv(clientSocket, buffer, sizeof(buffer), 0) == -1) { std::cerr << "Failed to receive data" << std::endl; close(clientSocket); close(serverSocket); return 1; } std::cout << "Received message from client: " << buffer << std::endl; const char* response = "Hello from server!"; if (send(clientSocket, response, std::strlen(response), 0) == -1) { std::cerr << "Failed to send data" << std::endl; close(clientSocket); close(serverSocket); return 1; } // 关闭连接 close(clientSocket); close(serverSocket); return 0; } ``` 客户端代码: ```cpp #include <iostream> #include <cstring> #include <unistd.h> #include <sys/socket.h> #include <netinet/in.h> #include <arpa/inet.h> int main() { // 创建Socket int clientSocket = socket(AF_INET, SOCK_STREAM, 0); if (clientSocket == -1) { std::cerr << "Failed to create socket" << std::endl; return 1; } // 连接服务器 sockaddr_in serverAddress{}; serverAddress.sin_family = AF_INET; serverAddress.sin_port = htons(12345); if (inet_pton(AF_INET, "127.0.0.1", &(serverAddress.sin_addr)) <= 0) { std::cerr << "Invalid address" << std::endl; close(clientSocket); return 1; } if (connect(clientSocket, (struct sockaddr*)&serverAddress, sizeof(serverAddress)) == -1) { std::cerr << "Failed to connect to server" << std::endl; close(clientSocket); return 1; } // 发送和接收数据 const char* message = "Hello from client!"; if (send(clientSocket, message, std::strlen(message), 0) == -1) { std::cerr << "Failed to send data" << std::endl; close(clientSocket); return 1; } char buffer[1024]; std::memset(buffer, 0, sizeof(buffer)); if (recv(clientSocket, buffer, sizeof(buffer), 0) == -1) { std::cerr << "Failed to receive data" << std::endl; close(clientSocket); return 1; } std::cout << "Received message from server: " << buffer << std::endl; // 关闭连接 close(clientSocket); return 0; } ``` 上述代码展示了一个简单的CS架构的Socket编程示例,其中服务器端监听连接请求并与客户端进行通信,客户端通过Socket连接服务器并发送消息,服务器接收消息并回复。你可以根据实际需求进行修改和扩展。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值