CFWriteStreamWrite 在完成之前停止

发布于 2024-12-10 14:48:58 字数 6274 浏览 0 评论 0原文

我正在尝试使用 CFWriteStreamWrite 将数据从 iPhone 发送到运行 macox 的笔记本电脑。

在笔记本电脑(macOS)上,接收代码为

int sockfd, newsockfd, portno;
socklen_t clilen;
struct sockaddr_in serv_addr, cli_addr;
int n;
if (argc < 2) {
    fprintf(stderr,"ERROR, no port provided\n");
    exit(1);
}
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd < 0)
    error("ERROR opening socket");
bzero((char *) &serv_addr, sizeof(serv_addr));
portno = atoi(argv[1]);
serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = INADDR_ANY;
serv_addr.sin_port = htons(portno);
if (bind(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0)
    error("ERROR on binding");
PacketSize = 672629;
char *blob = (char *)malloc(PacketSize*sizeof(char));
listen(sockfd,5);
newsockfd = accept(sockfd, (struct sockaddr *) &cli_addr, &clilen);
if (newsockfd < 0) error("ERROR on accept");

uint64_t ByteToRead = PacketSize;
char *buffer= blob;

/*
n=recv(newsockfd, blob, PacketSize, MSG_WAITALL);
*/
while (ByteToRead>0)
{
    n = recv(newsockfd, buffer, ByteToRead, 0);
    //n = read(newsockfd,buffer,min(1024, ByteToRead));
    if (n < 0)
        error("ERROR reading from socket");
    ByteToRead -= n;
    buffer += n;
    printf ("just read %d, %d left\n", n, ByteToRead);
}

在手机端(ios5)上,我使用 runloop 方法:

NSString *iHostname = [NSString stringWithFormat:@"192.168.1.66"];
NSString *iPort = [NSString stringWithFormat:@"2553"];

static const CFOptionFlags kWriteNetworkEvents = kCFStreamEventEndEncountered |
kCFStreamEventErrorOccurred |
kCFStreamEventCanAcceptBytes |
kCFStreamEventOpenCompleted |
kCFStreamEventNone;
CFStreamClientContext ctxt = {0,(void*)self,NULL,NULL,NULL};
CFHostRef hostRef = CFHostCreateWithName(kCFAllocatorDefault,(CFStringRef)iHostname);

CFStreamCreatePairWithSocketToCFHost(kCFAllocatorDefault, hostRef, [iPort intValue],
                                     NULL, &writeStream);

CFWriteStreamSetClient(writeStream, kWriteNetworkEvents, MyWriteStreamClientCallBack, &ctxt);

CFWriteStreamScheduleWithRunLoop(writeStream, CFRunLoopGetCurrent(), kCFRunLoopDefaultMode);
CFWriteStreamOpen(writeStream);

回调函数为

static void MyWriteStreamClientCallBack( CFWriteStreamRef stream, CFStreamEventType type, void *clientCallBackInfo ) 
{
UploadTableViewController *self = [[(UploadTableViewController *)clientCallBackInfo retain] autorelease];

NSLog(@"in call back");
switch (type)
{
    case kCFStreamEventEndEncountered:
    {
        NSLog(@"in CallBack: kCFStreamEventEndEncounteredd");
        CFWriteStreamClose(stream);
        break;
    }
    case kCFStreamEventErrorOccurred:
    {
        NSLog(@"in CallBack: kCFStreamEventErrorOccurred");
        //CFStreamError error = CFWriteStreamGetError(stream);
        //NSLog([[self errorFromCFStreamError:error] domain]);
        //NSLog(@"%d",[[self errorFromCFStreamError:error] code] );
        break;

    }
    case kCFStreamEventCanAcceptBytes:
    {            
        NSLog(@"in CallBack: kCFStreamEventCanAcceptBytes, %llu bytes to send", self->ByteToSent);

        CFIndex ByteSent = CFWriteStreamWrite(stream,  self->WriteStart, (CFIndex)(self->ByteToSent));
        if (ByteSent > 0)
        {
            self->ByteToSent -= ByteSent;
            self->WriteStart += ByteSent;
            NSLog(@"just wrote %ld bytes, %lld left", ByteSent, self->ByteToSent);
        }
        else
            NSLog(@"CFWriteStreamWrite returned %ld", ByteSent);


    }
    case kCFStreamEventNone:
    {
        NSLog(@"in CallBack: kCFStreamEventNone");
        break;
    }
    case kCFStreamEventOpenCompleted:
    {
        NSLog(@"in CallBack: kCFStreamEventNone");
        break;
    }
}

问题是发送在中间停止。例如,在电话上我从 XCODE 得到了这个:

2011-10-19 18:26:04.062 whereamI[1818:707] in CallBack: kCFStreamEventNone
2011-10-19 18:26:26.736 whereamI[1818:707] in call back
2011-10-19 18:26:26.738 whereamI[1818:707] in CallBack: kCFStreamEventNone
2011-10-19 18:26:26.739 whereamI[1818:707] in call back
2011-10-19 18:26:26.740 whereamI[1818:707] in CallBack: kCFStreamEventCanAcceptBytes,         672629 bytes to send
2011-10-19 18:26:26.745 whereamI[1818:707] just wrote 131768 bytes, 540861 left
2011-10-19 18:26:26.746 whereamI[1818:707] in CallBack: kCFStreamEventNone
2011-10-19 18:26:26.753 whereamI[1818:707] in call back
2011-10-19 18:26:26.755 whereamI[1818:707] in CallBack: kCFStreamEventCanAcceptBytes, 540861 bytes to send
2011-10-19 18:26:26.756 whereamI[1818:707] CFWriteStreamWrite returned -1
2011-10-19 18:26:26.757 whereamI[1818:707] in CallBack: kCFStreamEventNone
2011-10-19 18:26:26.758 whereamI[1818:707] in call back
2011-10-19 18:26:26.759 whereamI[1818:707] in CallBack: kCFStreamEventErrorOccurred
2011-10-19 18:45:49.101 whereamI[1818:707] in call back
2011-10-19 18:45:49.102 whereamI[1818:707] in CallBack: kCFStreamEventNone
2011-10-19 18:45:49.104 whereamI[1818:707] in call back
2011-10-19 18:45:49.105 whereamI[1818:707] in CallBack: kCFStreamEventCanAcceptBytes, 672629 bytes to send
2011-10-19 18:45:49.110 whereamI[1818:707] just wrote 131768 bytes, 540861 left
2011-10-19 18:45:49.111 whereamI[1818:707] in CallBack: kCFStreamEventNone
2011-10-19 18:45:49.120 whereamI[1818:707] in call back
2011-10-19 18:45:49.121 whereamI[1818:707] in CallBack: kCFStreamEventCanAcceptBytes, 540861 bytes to send
2011-10-19 18:45:49.123 whereamI[1818:707] just wrote 2896 bytes, 537965 left
2011-10-19 18:45:49.124 whereamI[1818:707] in CallBack: kCFStreamEventNone

列表实际上很长,这只是结尾部分。

在笔记本电脑上,输出是

max@machine:~/socket$ ./newserver.x 2553
just read 1448, 671181 left
ERROR reading from socket: Socket operation on non-socket

在 C 代码中,我也尝试过

n=recv(newsockfd, blob, PacketSize, MSG_WAITALL);

没有循环和

n = read(newsockfd,buffer,min(1024, ByteToRead));

循环。两者也无法完成传输。

在电话上,我也尝试仅使用 BSD 套接字:

n = write(sockfd, &PackageSize, sizeof(PackageSize));

进行配对

n=recv(newsockfd, blob, PacketSize, MSG_WAITALL);

,并且传输确实成功完成。

所以,请帮忙。

谢谢。

I'm trying to use CFWriteStreamWrite to send data from iPhone to a laptop running macox.

On the laptop (macOS), the receiving code is

int sockfd, newsockfd, portno;
socklen_t clilen;
struct sockaddr_in serv_addr, cli_addr;
int n;
if (argc < 2) {
    fprintf(stderr,"ERROR, no port provided\n");
    exit(1);
}
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd < 0)
    error("ERROR opening socket");
bzero((char *) &serv_addr, sizeof(serv_addr));
portno = atoi(argv[1]);
serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = INADDR_ANY;
serv_addr.sin_port = htons(portno);
if (bind(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0)
    error("ERROR on binding");
PacketSize = 672629;
char *blob = (char *)malloc(PacketSize*sizeof(char));
listen(sockfd,5);
newsockfd = accept(sockfd, (struct sockaddr *) &cli_addr, &clilen);
if (newsockfd < 0) error("ERROR on accept");

uint64_t ByteToRead = PacketSize;
char *buffer= blob;

/*
n=recv(newsockfd, blob, PacketSize, MSG_WAITALL);
*/
while (ByteToRead>0)
{
    n = recv(newsockfd, buffer, ByteToRead, 0);
    //n = read(newsockfd,buffer,min(1024, ByteToRead));
    if (n < 0)
        error("ERROR reading from socket");
    ByteToRead -= n;
    buffer += n;
    printf ("just read %d, %d left\n", n, ByteToRead);
}

And on the phone side (ios5), I use the runloop method:

NSString *iHostname = [NSString stringWithFormat:@"192.168.1.66"];
NSString *iPort = [NSString stringWithFormat:@"2553"];

static const CFOptionFlags kWriteNetworkEvents = kCFStreamEventEndEncountered |
kCFStreamEventErrorOccurred |
kCFStreamEventCanAcceptBytes |
kCFStreamEventOpenCompleted |
kCFStreamEventNone;
CFStreamClientContext ctxt = {0,(void*)self,NULL,NULL,NULL};
CFHostRef hostRef = CFHostCreateWithName(kCFAllocatorDefault,(CFStringRef)iHostname);

CFStreamCreatePairWithSocketToCFHost(kCFAllocatorDefault, hostRef, [iPort intValue],
                                     NULL, &writeStream);

CFWriteStreamSetClient(writeStream, kWriteNetworkEvents, MyWriteStreamClientCallBack, &ctxt);

CFWriteStreamScheduleWithRunLoop(writeStream, CFRunLoopGetCurrent(), kCFRunLoopDefaultMode);
CFWriteStreamOpen(writeStream);

The callback function is

static void MyWriteStreamClientCallBack( CFWriteStreamRef stream, CFStreamEventType type, void *clientCallBackInfo ) 
{
UploadTableViewController *self = [[(UploadTableViewController *)clientCallBackInfo retain] autorelease];

NSLog(@"in call back");
switch (type)
{
    case kCFStreamEventEndEncountered:
    {
        NSLog(@"in CallBack: kCFStreamEventEndEncounteredd");
        CFWriteStreamClose(stream);
        break;
    }
    case kCFStreamEventErrorOccurred:
    {
        NSLog(@"in CallBack: kCFStreamEventErrorOccurred");
        //CFStreamError error = CFWriteStreamGetError(stream);
        //NSLog([[self errorFromCFStreamError:error] domain]);
        //NSLog(@"%d",[[self errorFromCFStreamError:error] code] );
        break;

    }
    case kCFStreamEventCanAcceptBytes:
    {            
        NSLog(@"in CallBack: kCFStreamEventCanAcceptBytes, %llu bytes to send", self->ByteToSent);

        CFIndex ByteSent = CFWriteStreamWrite(stream,  self->WriteStart, (CFIndex)(self->ByteToSent));
        if (ByteSent > 0)
        {
            self->ByteToSent -= ByteSent;
            self->WriteStart += ByteSent;
            NSLog(@"just wrote %ld bytes, %lld left", ByteSent, self->ByteToSent);
        }
        else
            NSLog(@"CFWriteStreamWrite returned %ld", ByteSent);


    }
    case kCFStreamEventNone:
    {
        NSLog(@"in CallBack: kCFStreamEventNone");
        break;
    }
    case kCFStreamEventOpenCompleted:
    {
        NSLog(@"in CallBack: kCFStreamEventNone");
        break;
    }
}

The problem is the sending stops in the middle. For example, on the phone i got this from XCODE:

2011-10-19 18:26:04.062 whereamI[1818:707] in CallBack: kCFStreamEventNone
2011-10-19 18:26:26.736 whereamI[1818:707] in call back
2011-10-19 18:26:26.738 whereamI[1818:707] in CallBack: kCFStreamEventNone
2011-10-19 18:26:26.739 whereamI[1818:707] in call back
2011-10-19 18:26:26.740 whereamI[1818:707] in CallBack: kCFStreamEventCanAcceptBytes,         672629 bytes to send
2011-10-19 18:26:26.745 whereamI[1818:707] just wrote 131768 bytes, 540861 left
2011-10-19 18:26:26.746 whereamI[1818:707] in CallBack: kCFStreamEventNone
2011-10-19 18:26:26.753 whereamI[1818:707] in call back
2011-10-19 18:26:26.755 whereamI[1818:707] in CallBack: kCFStreamEventCanAcceptBytes, 540861 bytes to send
2011-10-19 18:26:26.756 whereamI[1818:707] CFWriteStreamWrite returned -1
2011-10-19 18:26:26.757 whereamI[1818:707] in CallBack: kCFStreamEventNone
2011-10-19 18:26:26.758 whereamI[1818:707] in call back
2011-10-19 18:26:26.759 whereamI[1818:707] in CallBack: kCFStreamEventErrorOccurred
2011-10-19 18:45:49.101 whereamI[1818:707] in call back
2011-10-19 18:45:49.102 whereamI[1818:707] in CallBack: kCFStreamEventNone
2011-10-19 18:45:49.104 whereamI[1818:707] in call back
2011-10-19 18:45:49.105 whereamI[1818:707] in CallBack: kCFStreamEventCanAcceptBytes, 672629 bytes to send
2011-10-19 18:45:49.110 whereamI[1818:707] just wrote 131768 bytes, 540861 left
2011-10-19 18:45:49.111 whereamI[1818:707] in CallBack: kCFStreamEventNone
2011-10-19 18:45:49.120 whereamI[1818:707] in call back
2011-10-19 18:45:49.121 whereamI[1818:707] in CallBack: kCFStreamEventCanAcceptBytes, 540861 bytes to send
2011-10-19 18:45:49.123 whereamI[1818:707] just wrote 2896 bytes, 537965 left
2011-10-19 18:45:49.124 whereamI[1818:707] in CallBack: kCFStreamEventNone

The list is actually long and this is only the ending part.

While on the laptop, the output is

max@machine:~/socket$ ./newserver.x 2553
just read 1448, 671181 left
ERROR reading from socket: Socket operation on non-socket

In the C code I've also tried

n=recv(newsockfd, blob, PacketSize, MSG_WAITALL);

without a loop, and

n = read(newsockfd,buffer,min(1024, ByteToRead));

in the loop. Both also can't finish the transfer.

On the phone I've also tried using just BSD socket:

n = write(sockfd, &PackageSize, sizeof(PackageSize));

to pair with

n=recv(newsockfd, blob, PacketSize, MSG_WAITALL);

And the transfer DOES finish successfully.

So, please help.

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

ゃ人海孤独症 2024-12-17 14:48:58

这行是错误的:

char *buffer= &blob;

应该这样说:

char *buffer = blob;

This line is wrong:

char *buffer= &blob;

It should say this:

char *buffer = blob;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文