我在我的应用程序中使用以下代码但是当我设置ip地址时出现错误,如 192.168.0.1(十六进制格式)
代替< b> INADDR_ANY / 0x7f000001(127.0.0.1)
BAction)createSocket:(id)sender {
CFSocketContext CTX = {0, "this is server", NULL, NULL, NULL};
tcpServer = CFSocketCreate(NULL,0, 0, 0,kCFSocketAcceptCallBack, 0, &CTX);
if (tcpServer == NULL)
NSLog(@"server isn't created");
else
NSLog(@"sever is successfully created");
struct sockaddr_in addr;
memset(&addr, 0, sizeof(addr));
addr.sin_len = sizeof(addr);
addr.sin_family = AF_INET;
addr.sin_port = htons(2048);
addr.sin_addr.s_addr = htonl(0x7f000001);
NSData *address = [ NSData dataWithBytes: &addr length: sizeof(addr) ];
if (CFSocketSetAddress(tcpServer, (__bridge CFDataRef) address) != kCFSocketSuccess) {
NSLog(@"socket address not set");
}
else
{
NSLog(@"socket address is successfully added");
}
我可以添加其他IP地址或任何限制使用其他IP吗?
如果我们可以怎么样?
我想使用其他ip,因为我使用三个iphone互相连接。
答案 0 :(得分:0)
您的本地IP地址不是192.168.0.1。那是你的路由器地址。绑定地址必须是本地主机中NIC的IP地址,或INADDR_ANY或127.0.0.1。