开发者

Sending array through sockets

I am trying to send an array using send():

ch开发者_开发技巧ar* packet = new char[3];
packet[0] = 0;
packet[1] = 1;
packet[2] = 2;

And I am trying to print it like this:

char temp[3]; Recv(temp, 3);
for(unsigned int i = 0; i < 3; i++) {
    cout << temp[i] << endl;
}

But I only get 2 strange characters an the screen. What could be the problem? I use send(packet, 3).

Server code:

class Server {
    WSAData wsadata;
    int desc;
    int remoteSocket;
    SOCKADDR_IN server;
public:
    Server() {
        if (WSAStartup(MAKEWORD(1, 1), &wsadata) != 0 )
        {
            cout << "Error creating socket" << endl;
            exit(1);
        }
        wsadata.wVersion = 5;
    }
    ~Server() {
        WSACleanup();
    }
    void Initialize() {
        memset(&server, 0, sizeof(server));

        server.sin_family = AF_INET;
        server.sin_addr.s_addr = INADDR_ANY; // Where to start server?
        server.sin_port = htons(9898); // Port
    }
    void Socket() {
        desc = ::socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
        if (desc == -1) {
            cout << "Error in Socket()" << endl;
        }
    }
    void Bind() {
        if ((::bind(desc, (LPSOCKADDR)&server, sizeof(server))) == -1) {
            cout << "Error in Bind()" << endl;
        }
    }
    void Listen() {
        if ((::listen(desc, 5)) == -1) {
            cout << "Error in Listen()" << endl;
        }
    }
    void Accept() {
        SOCKADDR_IN sock;
        int intsock = sizeof(sock);
        remoteSocket = ::accept(desc, (LPSOCKADDR)&sock, &intsock);
        if (remoteSocket == -1) {
            cout << "Error in Accept()" << endl;
        }
        HandleConnection();
    }
    void HandleConnection() {
        cout << "You are connected !!!" << endl;
        char temp[3];
        Recv(temp, 3);
        for(unsigned int i = 0; i < 3; i++) {
            if (temp[i] == '2')
                cout << "I got a 2";
        }
    }
    void Send(const char* buffer, int size) {
        if ((::send(remoteSocket, buffer, size, 0)) < 0) {
            cout << "Error in Send()" << endl;
        }
    }
    void Recv(char* buffer, size_t size) {
        size_t n = 0;
        n = ::recv(remoteSocket, buffer, size, 0);
    }

};

int main() {
    Server s;
    s.Initialize();
    s.Socket();
    s.Bind();
    s.Listen();
    while(1) {
        s.Accept();
    }
    return 0;
}

Client code:

class Client {
    int desc;
    SOCKADDR_IN client;
    WSAData wsadata;
public:
    Client() {
        if (WSAStartup(MAKEWORD(1, 1), &wsadata) != 0) {
            cout << "Error creating socket" << endl;
            exit(1);
        }
        wsadata.wVersion = 5;
    }
    ~Client() {
        WSACleanup();
    }
    void Initialize() {
        memset(&client, 0, sizeof(client));
        client.sin_family = AF_INET;
        client.sin_addr.s_addr = inet_addr("127.0.0.1");
        client.sin_port = htons(9898);
    }
    void Socket() {
        desc = ::socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
        if (desc == -1) {
            cout << "Error in Socket()" << endl;
        }
    }
    void Connect() {
        if ((::connect(desc, (LPSOCKADDR)&client, sizeof(client))) > 0) {
            cout << "Error in Connect()" << endl;
        }
    }
    void Send(const char* buffer, int size) {
        int n;
        if ((n = ::send(desc, buffer, size, 0)) < 0) {
            cout << "Error in Send()" << endl;
        }
    }
    void Recv(char* buffer, size_t size) {
        size_t n = 0;
        n = ::recv(desc, buffer, size, 0);
        cout << n;
    }
};

int main() {
    Client c;
    c.Initialize();
    c.Socket();
    c.Connect();
    char packet[3];
    packet[0] = '0';
    packet[1] = '1';
    packet[2] = '2';
    c.Send(packet, 3);
    return 0;
}


When you use couture to print out the characters, it's treating them as characters and not numerical values. Since you sent 0, 1, and 2, which are all low-order ASCII values, the system can render them with whatever glyphs it wants, or perhaps not at all. In this case, I'd guess that the characters are mapping to a null character for zero (which won't display at all) and garbage characters for the other two.

To fix this, try typecasting the chars to ints befor printing them, out. That should give you the numeric values you're looking for.


I think you want 1 2 3 to get printed on screen. If so, then you have to do this:

packet[0] = '0'; //note the single quote!
packet[1] = '1';
packet[2] = '2';

Did you notice the difference?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜