-2
  1. I create a TCP server, I always get following error when I was run. How I get rid of this error, I do not understand where wrong?

  2. Also it say data received while connection refused, I do not run client. Why?

Error: Socket Created Error connect to server: Connection refused Error to accept: Invalid argument Data received

Server

#include <iostream>
#include "serverh.hpp"
#include "glimpsepackages.h"
#include "soupbintcppackages.h"



int main() {
    //soupBinTCP structures
    DebugPacket D;                               //"+"
    LoginAcceptedPacket A;
    LoginRejectPacket J;
    SequencedDataPacket S;
    ServerHeartBeatPacket H;
    EndOfSessionPacket Z;

    //glimpse structures

    SecondsMessage T;
    CombinationOrderBookDelivery M;
    TickSizeTableEntryPackage L;
    OrderBookDirectoryPackage R;
    OrderBookStatePackage O;
    AddOrderMessageNoMPIDPackage A1;
    AddOrderMessageMPIDPackage F;

    std::string ip="127.0.0.1";
    int port=7888;
    ssize_t size=1024;

    void *data={};

    TcpServer server(ip, port);

    server.connectToClient();
    server.accept();

    for (;;) {

        server.receivingData(size);   

        switch (server.pop()) {
            case '+':
                //  D.length=server.pop();
                D.code = server.pop8();
                D.text = server.pop();
                break;

            case 'A' :
                A.to_little_endian();
                A.code = server.pop8();
                A.sequence_number = server.pop32();

                for (int i = 0; i < 10; i++) {
                    A.session[i] = server.pop();
                }

                break;

            case 'J':
                    J.code = server.pop8();
                    J.rejectreasoncode = server.pop();
                break;

            case 'S':
                S.code = server.pop8();
                S.message = server.pop();
                break;

            case 'H':
                H.code = server.pop8();

            case 'Z':
                Z.code = server.pop8();

            default:
                return 0;
        }
    }

serverh.cpp

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <arpa/inet.h>
#include <string>
#include "serverh.hpp"
#include "util.h"
#include <iostream>


TcpServer::TcpServer(std::string ip, int port):_ip(ip),
_port(port),  _addrSize(sizeof _serverAddress) 
{
    _sockFd=-1;
};


bool TcpServer::connectToClient(){
    _sockFd=socket(AF_INET,SOCK_STREAM,0);
    if(_sockFd==-1)
        perror("Error creating socket");
    else
        std::cerr<<"Socket Created\n";

    _serverAddress.sin_addr.s_addr=inet_addr(_ip.c_str());
    _serverAddress.sin_family=AF_INET;
    _serverAddress.sin_port=htons(_port);


    if(::connect(_sockFd,(struct sockaddr *)&_serverAddress,   sizeof(_serverAddress))<0) {
        perror("Error connect to server");
        return false;
    }
    else
        std::cerr<<"Connected\n";

    if(::bind(_sockFd,(struct sockaddr *)&_serverAddress,_addrSize)<0)
        perror("Error bind to server");
    else
        std::cerr<<"Socket bind\n";

    if(::listen(_sockFd,5)==0)
        std::cerr<<"Listining\n";
    else
        perror("Error on Listening");

    return true;
}

bool TcpServer::accept(){
    if(::accept(_sockFd,(struct sockaddr *) &_serverAddress, &_addrSize)<0)
        perror("Error to accept");
    else
        std::cerr<<"Accepted\n";

    return  true;
}

void *TcpServer::receivingData(ssize_t size){
    char *buffer=new char[size];
    if(::recv(_sockFd,buffer, size,0)>0);
    std::cerr<<"Data received\n";

    return buffer;
}

void TcpServer::incOffset(int rest_length) {
    void *restbuff = alloca(rest_length);
    if (::recv(_sockFd, restbuff, rest_length, 0) < 0)
        std::cerr << "Data received\n";
}

char TcpServer::pop(){
    char ch;
    if(::recv(_sockFd, &ch, 1, 0)>0)
        return ch;
}

int8_t TcpServer::pop8() {
    int8_t ch;
    if (::recv(_sockFd, &ch, 8, 0) > 0)
        return getLeValue(ch);      // func include endian functions
}

int16_t TcpServer::pop16(){
    int16_t ch;
    if(::recv(_sockFd, &ch, 16, 0)>0)
        return getLeValue(ch);
}

int32_t TcpServer::pop32(){
    int32_t ch;
    if(::recv(_sockFd, &ch, 32, 0)>0)
        return getLeValue(ch);
}

int64_t TcpServer::pop64() {
    int64_t ch;
    if (::recv(_sockFd, &ch, 64, 0) > 0)
        return getLeValue(ch);
}

Serverh.hpp

#ifndef SERVERH_H
#define SERVERH_H

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <string>

class TcpServer{
    public:
        TcpServer(const std::string _ip,int _port);

        bool connectToClient();
        bool accept();
        void *receivingData(ssize_t size);
        bool sendToClient();
        char pop();
        int8_t pop8();
        int16_t pop16();
        int32_t pop32();
        int64_t pop64();

        void incOffset(int rest_length);

    private:

        std::string _ip;
        int _port;
        int _sockFd;
        sockaddr_in _serverAddress;
        socklen_t _addrSize;
};

#endif
HDJEMAI
  • 9,436
  • 46
  • 67
  • 93
  • Down't look like you're breaking them but if you're going to use preceeding underscores, best to know the rules: http://stackoverflow.com/questions/228783/what-are-the-rules-about-using-an-underscore-in-a-c-identifier – user4581301 Feb 27 '17 at 01:32

1 Answers1

0

The problem

Error: Socket Created Error connect to server: Connection refused Error to accept: Invalid argument Data received

Two errors here, a failure to connect because the other address is not accepting connections and a failure to accept because one or more of the input parameters are bad.

The failure to connect is understandable. Clients are rarely listening on a port for connections. Clients usually initiate the connection.

The failure to accept is caused by connectToClient exiting after failing to connect. It never binds and listens, so the socket has not been set up for accept.

Fixing this

A server should not attempt to connect. It should listen for attempts to connect to it. This makes connectToClient bizarre. It tries to connect to a client and then, on the same socket, bind and listen. You can't do both, so I'd discard

if(::connect(_sockFd,(struct sockaddr *)&_serverAddress,   sizeof(_serverAddress))<0) {
    perror("Error connect to server");
    return false;
}
else
    std::cerr<<"Connected\n";

and

reconsider

_serverAddress.sin_addr.s_addr=inet_addr(_ip.c_str());

as this will only listen to one incoming address which might be what you want, but isn't usually. I would also rename the function to reflect the change in purpose.

Note:

if(::accept(_sockFd,(struct sockaddr *) &_serverAddress, &_addrSize)<0)

passes in a pointer to the server address. This location will be overwritten with the address of the client. It's harmless, but you probably don't want to do this. You aren't using the returned address as far as I can see, so you might as well just pass in NULLs and pass on it.

if(::accept(_sockFd, NULL, NULL)<0)
Community
  • 1
  • 1
user4581301
  • 33,082
  • 7
  • 33
  • 54