我正在用 C 构建一个客户端/服务器消息传递程序。我试图让客户端的消息以不同的颜色显示。前任。在用户端,客户端 1 的文本将始终为蓝色,客户端 2 的文本将始终为红色。我相信我的代码应该可以工作,但是我在'socket.send(client.color, strlen(client.color), 0);'中的'socket'下有一个错误。错误消息是“表达式必须具有联合类型的结构,但它具有类型“int (*)(int __domain, int __type, int __protocol)”。
任何帮助将不胜感激!
// client_info struct to hold info about client
struct client_info {
int socket;
char username[MAX_USERNAME_SIZE];
char color[10];
int room;
};
// Generate a random color for the client's message
char color[10] = {
"0xFC0303", // red
"0xFC9803", // orange
"0xFCDB03", // yellow
"0x8CFC03", // neon green
"0x03FCB5", // mint
"0x03D7FC", // light blue
"0x0324FC", // dark blue
"0x9803FC", // purple
"0xFC03F8", // light pink
"0xFC037B" // magenta
};
// seed rand()
srand(time(NULL));
int n = rand() % 10;
// Create a client object with the received info
struct client_info client;
client.socket = client_socket;
strcpy(client.username, username);
strcpy(client.color, color[n]);
client.room = -1;
socket.send(client.color, strlen(client.color), 0);
我第一次尝试这个:
// Generate a random color for the client's message
char color[8];
sprintf(color, "#%06X", rand() % 0xFFFFFF);
// Create a client object with the recieved info
struct client_info client;
client.socket = client_socket;
strcpy(client.username, username);
strcpy(client.color, color);
client.room = -1;
它没有用。