无法使用CFWriteStream写入



我创建了一个UDP CFSocket,我可以在不使用任何文件句柄的情况下发送数据。但是,当我创建CFReadStreamRef, CFWriteStreamRef并使用它们来发送数据时,它失败了。控件没有返回。有人能帮我解决这个问题吗?代码如下:

-(void) CFSocketType2
{

CFSocketRef s = CFSocketCreate(kCFAllocatorDefault, PF_INET,
                               SOCK_DGRAM , IPPROTO_UDP,
                               0,
                               NULL,
                               NULL);
struct sockaddr_in      sin;
struct hostent           *host;
host = gethostbyname("localhost");
memset(&sin, 0, sizeof(sin));
memcpy(&(sin.sin_addr), host->h_addr,host->h_length);
sin.sin_family = AF_INET;
sin.sin_port = htons(5527);
CFDataRef address, data;
UInt8 message[] = "Hello world 123";
address = CFDataCreate(NULL, (UInt8 *)&sin, sizeof(sin));
data = CFDataCreate(NULL, message, sizeof(message));
//Sending data without using file handles
CFSocketConnectToAddress(s, address, 0);
CFSocketSendData(s, NULL, data, 0);

// The problem starts here
CFReadStreamRef readStream = NULL;
CFWriteStreamRef writeStream = NULL;

CFStreamCreatePairWithSocket(kCFAllocatorDefault, CFSocketGetNative(s), &readStream, &writeStream);

if (readStream && writeStream)
{
    CFReadStreamSetProperty(readStream, kCFStreamPropertyShouldCloseNativeSocket, kCFBooleanTrue);
    CFWriteStreamSetProperty(writeStream, kCFStreamPropertyShouldCloseNativeSocket, kCFBooleanTrue);
    if (! CFReadStreamOpen(readStream) || ! CFWriteStreamOpen(writeStream))
    {
        NSLog(@"Could not initialize streams!");
    }
    else
    {
        UInt8 msg [] ="Hello World";
        CFIndex a =  CFWriteStreamWrite(writeStream, msg, sizeof(msg));
        NSLog(@"Return value is %d", a);
    }
}
}

我在UDP套接字工作了一段时间。您的代码中的问题是,在您可以使用套接字之前,您没有连接服务器。

我之前写的代码如下。希望能有所帮助

-(void) BSDSocketType
{
int sockfd;
struct sockaddr_in servaddr;
char sendline[] = "hello world";
char addr[] = "127.0.0.1";

sockfd=socket(AF_INET,SOCK_DGRAM,0);
bzero(&servaddr,sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr=inet_addr(addr);
servaddr.sin_port=htons(5527);

// All these are foundation classes
CFReadStreamRef readStraem = NULL;
CFWriteStreamRef writeStream = NULL;

int status =0;
// Create CFSocket
status = -3;
status = fcntl(sockfd, F_SETFL, O_NONBLOCK);
if (status == -1)
{
    NSLog(@"Not able to create Non blocking socket");
}
int reuseaddr = 1;
status = setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &reuseaddr, sizeof(reuseaddr));
if (status == -1)
{
    NSLog(@"Error 2");
}
int nosigpipe = 1;
status = setsockopt(sockfd, SOL_SOCKET, SO_NOSIGPIPE, &nosigpipe, sizeof(nosigpipe));
if (status == -1)
{
    NSLog(@"Error");
}
CFStreamCreatePairWithSocket(NULL, (CFSocketNativeHandle)sockfd, &readStraem, &writeStream);
if (!readStraem || !writeStream)
{
    NSLog(@"Error");
}
NSLog(@"OK");

int n = connect(sockfd, (struct sockaddr *)&servaddr,sizeof(servaddr));
if ( n < 0)
{
    printf("Connect error with port %sn", strerror(errno));
}

CFReadStreamSetProperty(readStraem, kCFStreamPropertyShouldCloseNativeSocket, kCFBooleanFalse);
CFWriteStreamSetProperty(writeStream, kCFStreamPropertyShouldCloseNativeSocket, kCFBooleanFalse);
CFReadStreamOpen(readStraem);
CFWriteStreamOpen(writeStream);
NSLog(@"%ld",CFWriteStreamGetStatus(writeStream));
NSLog(@"%ld",CFReadStreamGetStatus(readStraem));
if(CFWriteStreamCanAcceptBytes(writeStream))
{
    NSLog(@"Ok");
    UInt8 msg [] ="CF STReamHello World from cftype 2";
    CFIndex a =  CFWriteStreamWrite(writeStream, msg, sizeof(msg));
    NSLog(@"%ld",a);
}
else
    NSLog(@"Cannot accept bytes");
NSLog(@"Ok");
NSInputStream * inputStream = (__bridge NSInputStream *)(readStraem);
NSOutputStream * outputStream = (__bridge NSOutputStream *)(writeStream);
[inputStream open];
[outputStream open];
UInt8 msg [] ="NSStream Hello World from cftype 2";
int a;
if ([outputStream hasSpaceAvailable])
{
    a = [outputStream write:msg maxLength:sizeof(msg)];
            NSLog(@"Ok");
}
NSLog(@"%d",a);

}

相关内容

  • 没有找到相关文章

最新更新