我需要构建一个游戏使用Socket来连接多个设备。 我尝试在cocos2dx上创建套接字时遇到问题。
void AcceptCallBack(
CFSocketRef socket,
CFSocketCallBackType type,
CFDataRef address,
const void *data,
void *info)
{
CFReadStreamRef readStream = NULL;
CFWriteStreamRef writeStream = NULL;
CFIndex bytes;
UInt8 buffer[128];
UInt8 recv_len = 0, send_len = 0;
/* The native socket, used for various operations */
CFSocketNativeHandle sock = *(CFSocketNativeHandle *) data;
/* The punch line we stored in the socket context */
char *punchline = info;
/* Create the read and write streams for the socket */
CFStreamCreatePairWithSocket(kCFAllocatorDefault, sock,
&readStream, &writeStream);
if (!readStream || !writeStream) {
close(sock);
fprintf(stderr, "CFStreamCreatePairWithSocket() failed\n");
return;
}
CFReadStreamOpen(readStream);
CFWriteStreamOpen(writeStream);
/* Wait for the client to finish sending the joke; wait for newline */
memset(buffer, 0, sizeof(buffer));
while (!strchr((char *) buffer, '\n') && recv_len < sizeof(buffer)) {
bytes = CFReadStreamRead(readStream, buffer + recv_len,
sizeof(buffer) - recv_len);
if (bytes < 0) {
// fprintf(stderr, "CFReadStreamRead() failed: %d\n", bytes);
close(sock);
return;
}
// NSLog(@"Read: %d", bytesRead);
NSLog(@"buffer: %s", buffer);
recv_len += bytes;
}
/* Send the punchline */
while (send_len < (strlen(punchline+1))) {
if (CFWriteStreamCanAcceptBytes(writeStream)) {
bytes = CFWriteStreamWrite(writeStream,
(unsigned char *) punchline + send_len,
(strlen((punchline)+1) - send_len) );
if (bytes < 0) {
fprintf(stderr, "CFWriteStreamWrite() failed\n");
close(sock);
return;
}
send_len += bytes;
}
close(sock);
CFReadStreamClose(readStream);
CFWriteStreamClose(writeStream);
return;
}
}
我在Ios应用程序上运行时一切正常(创建新项目时Xcode的默认设置)
但是当我将该源代码复制到由Cocos2dx创建的项目时。 我在这一行得到了一个错误:
char *punchline = info;
错误讯息:
Can not initialize a variable of type 'char *' with an Ivalue of type 'void *'
我不知道应该更改哪个设置来运行它。 感谢。