iphone - bsd套接字连接超时iPhone

标签 iphone c sockets

如何编写指定 BSD 套接字 connect 系统调用超时的代码?我编写 iPhone 应用程序,需要等待很长时间才能从 connect 系统调用获得响应。有例子吗? 谢谢

现在我有这样的东西:

host_name = NULL ;
host_name = gethostbyname([[host_value hostname] UTF8String]) ; 
if (host_name != NULL)
{
    struct sockaddr_in sin;
    sock = socket(AF_INET, SOCK_STREAM, 0);
    sin.sin_family = AF_INET;
    sin.sin_port = htons([[host_value port_number] intValue]);
    memcpy((char *)&sin.sin_addr,(char*)host_name->h_addr,host_name->h_length);
    if (connect(sock, (struct sockaddr*)(&sin),sizeof(struct sockaddr_in)) != 0) 
    {
       /* code */
    }
}

最佳答案

s = socket(AF_INET, SOCK_STREAM, 0);  
int x;
x=fcntl(s,F_GETFL,0);
fcntl(s,F_SETFL,x | O_NONBLOCK);

来自:http://rhoden.id.au/doc/sockets2.html

关于iphone - bsd套接字连接超时iPhone,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3893520/

相关文章:

ios - 如何在iphone静态库中包含资源?

c++ - 如何警告 C 中 void 指针参数的类型不兼容?

node.js - 带有 socket.io 的 EC2

c# - 通过 PHP 与 .net 通信

asp.net - .NET 中的 Tcp/Ip 套接字连接

iphone - iOS 5 : UITableView cells are not scrolling smooth

iphone - 将 UITabbarController 与 pushViewController 一起使用

iphone - 使用 Parse 移动平台在 iOS 中集成 Facebook

c - 当 sizeof(char) != 1 时,C 中的字节精度指针算法

c - 我可以读到文件中有 *are* 个字符,但不能将它们打印到另一个文件