在套接字上发送C风格的字符串

时间:2013-03-30 07:25:17

标签: c++ sockets

我想在套接字上发送一个旧式的C风格字符串,但我没有太多运气..

char name[16];
strcpy(name, "jim");

char name[] = "jim";

最终只是另一端的垃圾。

如何在套接字上发送字符序列&最后在另一端有相同的字符序列?

编辑:相关代码。 填充字符串和其他内容的数据包数据:

struct s_MessagingPacket {
    int action;

    unsigned int player_id, player_count, player_max;

    char level[16];
    int gamemode;

    s_MessagingPacket(e_MessagingActions a, unsigned int pid) {
        this->action = (int) a;

        this->player_id = pid;
        this->player_count = g_num_players;
        this->player_max = g_max_players;

        char def[] = "lev.lvl";
        strncpy(level, def, 16);
        this->gamemode = 0;
    }
};

我发送数据的部分:

// NOTIFY EVERYONE OF CONNECTION. ALSO NOTIFIES NEW GUY OF THEIR PID AND THE LEVEL
ENetPacket* pc = enet_packet_create(new s_MessagingPacket(ACTION_CONNECT, *idc), sizeof(s_MessagingPacket), ENET_PACKET_FLAG_RELIABLE);
enet_host_broadcast(g_server, CHANNEL_MESSAGING, pc);

如果您还需要其他任何内容,请发表评论。

0 个答案:

没有答案