|
@@ -1,6 +1,8 @@
|
|
|
#include "socketio.hpp"
|
|
|
#include <arpa/inet.h>
|
|
|
#include <unistd.h>
|
|
|
+#include <cinttypes>
|
|
|
+using std::size_t;
|
|
|
struct socket_exception : public std::exception{
|
|
|
std::string msg;
|
|
|
socket_exception(std::string&& _msg){
|
|
@@ -39,6 +41,12 @@ cppsocket::cppsocket(const std::string& addr, unsigned int PORT){
|
|
|
throw socket_exception("Could not reach server " + addr);
|
|
|
}
|
|
|
}
|
|
|
+int cppsocket::socket_id(){
|
|
|
+ return sock;
|
|
|
+}
|
|
|
+void cppsocket::close(){
|
|
|
+ shutdown(sock, 2);
|
|
|
+}
|
|
|
void cppsocket::write(const std::string& message){
|
|
|
for(size_t i = 0;i < message.length();i += buffersize){
|
|
|
char cs[buffersize + 1] = {0};
|
|
@@ -73,7 +81,6 @@ std::vector<char> cppsocket::receive(){
|
|
|
std::fill(buffer.begin(), buffer.end(), (char)0);
|
|
|
size_t val = read(sock, buffer.data(), buffersize + 1);
|
|
|
if(val == 0)throw socket_exception("Connection reset by peer");
|
|
|
- //std::cout << val << std::endl;
|
|
|
stor.insert(stor.end(), buffer.begin(), buffer.begin() + std::min(val, (size_t)buffersize));
|
|
|
if(buffer.data()[buffersize] == (char)0)break;
|
|
|
}
|
|
@@ -91,7 +98,7 @@ server_socket::server_socket(int _port) : port_(_port){
|
|
|
address.sin_family = AF_INET;
|
|
|
address.sin_addr.s_addr = INADDR_ANY;
|
|
|
address.sin_port = htons(port());
|
|
|
- if(bind(server_fd, (struct sockaddr *)&address, sizeof(address))<0){
|
|
|
+ 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?");
|
|
|
}
|
|
|
if (listen(server_fd, 16) < 0){
|