通过套接字发送数组

时间:2011-01-30 10:37:05

标签: c++ sockets

我正在尝试使用send()发送数组:

char* packet = new char[3];
packet[0] = 0;
packet[1] = 1;
packet[2] = 2;

我试图像这样打印出来:

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

但我只能在屏幕上看到2个奇怪的角色。可能是什么问题呢?我使用send(packet, 3)

服务器代码:

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;
}

客户代码:

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;
}

2 个答案:

答案 0 :(得分:3)

当您使用时装打印出字符时,它会将它们视为字符而不是数字值。由于您发送的是0,1和2,它们都是低阶ASCII值,因此系统可以使用它想要的任何字形渲染它们,或者根本不渲染它们。在这种情况下,我猜这些字符映射到零字符(根本不显示)和其他两个字符的垃圾字符。

要解决此问题,请尝试将字符类型转换为打印出来的字符。这应该为您提供您正在寻找的数值。

答案 1 :(得分:1)

我认为您希望1 2 3能够在屏幕上打印出来。如果是这样,那么你必须这样做:

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

您是否注意到了差异?