Prechádzať zdrojové kódy

UDP constructed but not working

manuel 6 rokov pred
rodič
commit
610559cc10
2 zmenil súbory, kde vykonal 84 pridanie a 2 odobranie
  1. 1 1
      socket_impl/socketio_posix.cpp
  2. 83 1
      socket_impl/udpsocket.cpp

+ 1 - 1
socket_impl/socketio_posix.cpp

@@ -103,7 +103,7 @@ server_socket::server_socket(int _port) : port_(_port){
 	address.sin_addr.s_addr = INADDR_ANY;
 	address.sin_port = htons(port());
 	if(bind(server_fd, (struct sockaddr *)&address, sizeof(address)) < 0){
-		throw socket_exception("Couldn't bind to port " + std::to_string(port()) + ", is it already in use?"); 
+		throw socket_exception("Couldn't bind to port " + std::to_string(port()) + ", is it already in use?");
 	}
 	if (listen(server_fd, 16) < 0){
 		throw socket_exception("Error in listen() call, no clue why");

+ 83 - 1
socket_impl/udpsocket.cpp

@@ -1,5 +1,87 @@
+#include <arpa/inet.h>
 #include <iostream>
+#include <stdexcept>
+#include <stdio.h>  //printf
+#include <stdlib.h> //exit(0);
+#include <cstring> //memset
+#include <sys/socket.h>
+#include <unistd.h>
+#include <vector>
 
-int main(){
+#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) {
+  if ((s = socket(AF_INET, SOCK_DGRAM, 0)) < 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);
 
+  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){
+  o.s = 0;
+}
+udpsocket& udpsocket::operator=(udpsocket&& o){
+  addr = o.addr;
+  s = o.s;
+  o.s = 0;
+  return *this;
+}
+void udpsocket::write(const std::string& msg, const std::string& dest, int port){
+  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){
+  struct sockaddr_in _dest;
+  std::memset((char*)&_dest, 0, sizeof(sockaddr_in));
+  _dest.sin_family = AF_INET;
+  _dest.sin_port = htons(PORT);
+
+  if (inet_aton(dest.c_str(), &_dest.sin_addr) == 0) {
+    throw std::logic_error(std::string("inet_aton() failed: ") + strerror(errno));
+  }
+  if (sendto(s, msg.data(), msg.size(), 0, (struct sockaddr*)&dest, slen) < 0) {
+    throw std::logic_error(std::string("Could not send packet: ") + strerror(errno));
+  }
+}
+std::vector<char> udpsocket::receive(){
+  std::vector<char> ret(1024);
+  if (read(s, ret.data(), 1024) <= 0) {
+    throw std::logic_error(std::string("Could not receive packet: ") + strerror(errno));
+  }
+  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);
+  sock.write("Hallo", "127.0.0.1",5555);
+  std::cin.get();
+  return 0;
 }