Selaa lähdekoodia

UDP Header + Object

mawinkle 6 vuotta sitten
vanhempi
commit
b006b6930b
5 muutettua tiedostoa jossa 44 lisäystä ja 242 poistoa
  1. 29 0
      include/udpsocket.hpp
  2. 0 49
      server.cpp
  3. 0 49
      servertest.cpp
  4. 15 49
      socket_impl/udpsocket_posix.cpp
  5. 0 95
      test.cpp

+ 29 - 0
include/udpsocket.hpp

@@ -0,0 +1,29 @@
+#include <arpa/inet.h>
+#include <iostream>
+#include <stdexcept>
+#include <netdb.h>
+#include <stdlib.h>
+#include <cstring>
+#include <sys/socket.h>
+#include <unistd.h>
+#include <netinet/in.h>
+#include <sys/types.h>
+#include <vector>
+
+class udpsocket {
+	struct sockaddr_in addr;
+	int s, slen = sizeof(addr);
+	int m_port;
+	public:
+	udpsocket(int port);
+	udpsocket(const udpsocket&) = delete;
+	udpsocket& operator=(const udpsocket&) = delete;
+	udpsocket(udpsocket&&);
+	udpsocket& operator=(udpsocket&&);
+	void write(const std::string&,const std::string& dest, int port)const;
+	void write(const std::vector<char>&,const std::string& dest, int port)const;
+	std::vector<char> receive()const;
+	void close();
+	int port()const;
+};
+

+ 0 - 49
server.cpp

@@ -1,49 +0,0 @@
-#include "socketio.hpp"
-#include <iostream>
-#include <exception>
-#include <random>
-template<typename T>
-std::ostream& operator<<(std::ostream& out, std::vector<T> o){
-	out << "(";
-	for(unsigned int i = 0;i < o.size();i++){
-		if(o[i] == '\r')continue;
-		out << o[i];
-		if(i < (o.size() - 1))
-			out << ", ";
-	}
-	return out << ")";
-}
-template<>
-std::ostream& operator<< <char>(std::ostream& out, std::vector<char> o){
-	for(unsigned int i = 0;i < o.size();i++){
-		out << o[i];
-	}
-	return out;
-}
-int main(){
-	std::uniform_int_distribution<unsigned char> dis(0,20);
-	std::mt19937_64 gen;
-	server_socket ssock(80);
-	while(true){
-		cppsocket sock = ssock.accept_connection();
-		try{
-			while(true){
-				std::vector<char> vec = sock.receive();
-				std::cout << "Received: " << vec << std::endl;
-				std::string resp(100, 'a');
-				for(unsigned int i = 0;i < resp.size();i++){
-					resp[i] = dis(gen) + 'a';
-				}
-				sock.write("Hallo " + resp);
-			}
-			sock.close();
-			std::cout << "Closed\n";
-		}
-		catch(const std::exception& e){
-			std::cout << e.what() << std::endl;
-		}
-		catch(...){
-			std::cout << "Unidentified exception" << std::endl;
-		}
-	}
-}

+ 0 - 49
servertest.cpp

@@ -1,49 +0,0 @@
-#include <socketio.hpp>
-#include <iostream>
-#include <exception>
-#include <random>
-template<typename T>
-std::ostream& operator<<(std::ostream& out, std::vector<T> o){
-	out << "(";
-	for(unsigned int i = 0;i < o.size();i++){
-		if(o[i] == '\r')continue;
-		out << o[i];
-		if(i < (o.size() - 1))
-			out << ", ";
-	}
-	return out << ")";
-}
-template<>
-std::ostream& operator<< <char>(std::ostream& out, std::vector<char> o){
-	for(unsigned int i = 0;i < o.size();i++){
-		out << o[i];
-	}
-	return out;
-}
-int main(){
-	std::uniform_int_distribution<unsigned char> dis(0,20);
-	std::mt19937_64 gen;
-	server_socket ssock(80);
-	while(true){
-		cppsocket sock = ssock.accept_connection();
-		try{
-			while(true){
-				std::vector<char> vec = sock.receive();
-				std::cout << "Received: " << vec << std::endl;
-				std::string resp(100, 'a');
-				for(unsigned int i = 0;i < resp.size();i++){
-					resp[i] = dis(gen) + 'a';
-				}
-				sock.write("Hallo " + resp);
-			}
-			sock.close();
-			std::cout << "Closed\n";
-		}
-		catch(const std::exception& e){
-			std::cout << e.what() << std::endl;
-		}
-		catch(...){
-			std::cout << "Unidentified exception" << std::endl;
-		}
-	}
-}

+ 15 - 49
socket_impl/udpsocket.cpp → socket_impl/udpsocket_posix.cpp

@@ -1,59 +1,34 @@
-#include <arpa/inet.h>
-#include <iostream>
-#include <stdexcept>
-#include <netdb.h>  //printf
-#include <stdlib.h> //exit(0);
-#include <cstring> //memset
-#include <sys/socket.h>
-#include <unistd.h>
-#include <netinet/in.h>
-#include <sys/types.h>
-#include <vector>
-
-#define SERVER "127.0.0.1"
-#define BUFLEN 1024 // Max length of buffer
-#define PORT 8888   // The port on which to send data
-class udpsocket {
-	struct sockaddr_in addr;
-	int s, slen = sizeof(addr);
-	public:
-	udpsocket(int port);
-	udpsocket(const udpsocket&) = delete;
-	udpsocket& operator=(const udpsocket&) = delete;
-	udpsocket(udpsocket&&);
-	udpsocket& operator=(udpsocket&&);
-	void write(const std::string&,const std::string& dest, int port);
-	void write(const std::vector<char>&,const std::string& dest, int port);
-	std::vector<char> receive();
-	void close();
-};
-udpsocket::udpsocket(int port) {
+#include "udpsocket.hpp"
+udpsocket::udpsocket(int port) : m_port(port){
 	if ((s = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0) {
 		throw std::logic_error(std::string("Socket creation failed: ") + strerror(errno));
 	}
 	std::memset((char*)&addr, 0, sizeof(addr));
 	addr.sin_family = AF_INET;
 	addr.sin_addr.s_addr = htonl(INADDR_ANY);
-	addr.sin_port = htons(port);
+	addr.sin_port = htons(this->port());
 
 	if (bind(s, (struct sockaddr*)&addr, sizeof(addr)) < 0) {
 		throw std::logic_error(std::string("Socket binding failed: ") + strerror(errno));
 	}
 }
-udpsocket::udpsocket(udpsocket&& o) : addr(o.addr),s(o.s) {
+udpsocket::udpsocket(udpsocket&& o) : addr(o.addr),s(o.s),m_port(o.m_port) {
 	o.s = 0;
+	o.m_port = 0;
 }
 udpsocket& udpsocket::operator=(udpsocket&& o){
 	addr = o.addr;
 	s = o.s;
+	m_port = o.m_port;
 	o.s = 0;
+	o.m_port = 0;
 	return *this;
 }
-void udpsocket::write(const std::string& msg, const std::string& dest, int port){
+void udpsocket::write(const std::string& msg, const std::string& dest, int port)const{
 	std::vector<char> _msg(msg.begin(), msg.end());
 	write(_msg, dest, port);
 }
-void udpsocket::write(const std::vector<char>& msg, const std::string& dest, int port){
+void udpsocket::write(const std::vector<char>& msg, const std::string& dest, int port)const{
 	hostent *server_host;
 	errno = 0;
 	server_host = gethostbyname(dest.c_str());
@@ -66,31 +41,22 @@ void udpsocket::write(const std::vector<char>& msg, const std::string& dest, int
 	memcpy(&server_addr.sin_addr, server_host->h_addr,
 			sizeof(struct in_addr)); server_addr.sin_port = htons(port);
 	/* send a message */
-	while(true)
 	if(sendto(s, msg.data(), msg.size(), 0,(const sockaddr*)&server_addr, sizeof(server_addr)) < 0){
 		throw std::logic_error(std::string("Could not send packet: ") + strerror(errno));
 	}
 }
-std::vector<char> udpsocket::receive(){
+std::vector<char> udpsocket::receive()const{
 	std::vector<char> ret(1024);
-	if (read(s, ret.data(), 1024) <= 0) {
+	int l;
+	if ((l = read(s, ret.data(), 1024)) <= 0) {
 		throw std::logic_error(std::string("Could not receive packet: ") + strerror(errno));
 	}
+	ret.resize(l);
 	return ret;
 }
 void udpsocket::close(){
 	shutdown(s, 2);
 }
-void die(const std::string& s) {
-	perror(s.c_str());
-	exit(1);
-}
-
-int main() {
-	udpsocket sock(5566);
-	std::vector<char> d(512,'a');
-	while(true)
-	sock.write(d.data(), "8.8.8.8" ,5555);
-	std::cin.get();
-	return 0;
+int udpsocket::port()const {
+	return this->m_port;
 }

+ 0 - 95
test.cpp

@@ -1,95 +0,0 @@
-#include <socketio.hpp>
-#include <crypt/BigInt64.hpp>
-#include <xoshiro.hpp>
-#include <iostream>
-#include <chrono>
-#include <algorithm>
-#include <numeric>
-xoshiro_256 gen(42);
-unsigned long long nanoTime(){
-	using namespace std;
-	using namespace std::chrono;
-	return duration_cast<nanoseconds>(high_resolution_clock::now().time_since_epoch()).count();
-}
-template<typename T>
-std::ostream& operator<<(std::ostream& out, std::vector<T> o){
-	out << "[";
-	for(unsigned int i = 0;i < o.size();i++){
-		if(o[i] == '\r')continue;
-		out << o[i];
-		if(i < o.size() - 1)
-			out << ", ";
-	}
-	return out << "]";
-}
-template<>
-std::ostream& operator<< <char>(std::ostream& out, std::vector<char> o){
-	for(unsigned int i = 0;i < o.size();i++){
-		out << o[i];
-	}
-	return out;
-}
-unsigned long long multTest(size_t s){
-	BigInt a(gen,s);
-	BigInt b(gen,s);
-	std::vector<unsigned long long> times(0);
-	for(int o = 0;o < 4;o++){
-		auto t1 = nanoTime();
-		for(int i = 0;i < 1000;i++){
-			a = a.mult(b);
-			a.cut(s);
-		}
-		auto t2 = nanoTime();
-		times.push_back((t2 - t1));
-	}
-	std::sort(times.begin(), times.end());
-	/*for(auto t : times){
-		std::cout << t/1000 << ", ";
-	}*/
-	//std::cout << std::endl;
-	return std::accumulate(times.begin(), times.end(), 0ULL);
-}
-
-int mailn(){
-	BigInt a("4528437659827");
-	BigInt b(gen, 31);
-	BigInt res = a.modPow(b, secure_prime);
-	std::cout << res.toString() << std::endl;
-	//return 0;
-	multTest(50);
-	//multTest(100);
-	//multTest(200);
-	std::cout << multTest(50 ) / 1000 / 1000 << " ms" << std::endl;
-	std::cout << multTest(100) / 1000 / 1000 << " ms" << std::endl;
-	std::cout << multTest(200) / 1000 / 1000 << " ms" << std::endl;
-	std::cout << multTest(400) / 1000 / 1000 << " ms" << std::endl;
-	//std::cout << multTest(800) / 1000 / 1000 << " ms" << std::endl;
-}
-int main(){
-	cppsocket sock("vmp.ethz.ch", 80);
-	//cppsocket sock("127.0.0.1", 80);
-	std::vector<char> toSend(10);
-	for(unsigned int i = 0;i < toSend.size();i++){
-		toSend[i] = (char)(i * i) % 10 + 'a';
-	}
-	//std::cout << errno << std::endl;
-	try{
-		for(int i = 0;i < 100;i++){
-			sock.write(toSend);
-			std::cout << "Rec" << std::endl;
-			std::cout << sock.receive() << std::endl;
-			//std::this_thread::sleep_for(std::chrono::milliseconds(1));
-		}
-	}
-	catch(std::exception& e){
-		std::cout << e.what() << std::endl;
-	}
-	//std::cout << "Sent "  << std::to_string(toSend.size()) << ", Receiving..." << std::endl;
-	//sock.write(std::vector<char>(1,1));
-	//std::vector<char> vec = sock.receive();
-	//std::cout << (vec == toSend);
-	//std::cout << vec << std::endl;
-	//vec = sock.receive();
-	//std::cout << vec << std::endl;
-	return 0;
-}