0

我在将字符串从 C# 应用程序发送到 iOS 应用程序时遇到一些问题。我尝试使用来自 C# 的套接字将数据发送到 SocketTest 并接收,我已经使用套接字从 Socktest 将数据发送到我的 iOS 应用程序,一切顺利!问题是当我尝试使用套接字从 C# 向 iOS 发送数据时......它连接和断开但不发送消息(或者我的 iOS 不读取它......)

要在 C# 中使用套接字发送数据,我正在使用:

Socket soc = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
System.Net.IPAddress ipAdd = System.Net.IPAddress.Parse("192.168.0.10");
System.Net.IPEndPoint remoteEP = new IPEndPoint(ipAdd, 2525);
soc.Connect(remoteEP);
byte[] byData = System.Text.Encoding.ASCII.GetBytes("TESTESTTES");
soc.Send(byData);
soc.Disconnect(false);

为了在 iOS 上接收它,我使用了 AsyncSocket 库(我认为这是要发布的相关代码):

-(void)onSocket:(AsyncSocket *)sock didConnectToHost:(NSString *)host port:(UInt16)port
{
    [self logInfo:FORMAT(@"Accepted client %@:%hu", host, port)];   
    NSString *welcomeMsg = @"Welcome to the AsyncSocket Echo Server\r\n";
    NSData *welcomeData = [welcomeMsg dataUsingEncoding:NSUTF8StringEncoding];
    [sock writeData:welcomeData withTimeout:-1 tag:WELCOME_MSG];
    [sock readDataToData:[AsyncSocket CRLFData] withTimeout:READ_TIMEOUT tag:0];
}

- (void)onSocket:(AsyncSocket *)sock didWriteDataWithTag:(long)tag
{
    if(tag == ECHO_MSG)
    {
    [sock readDataToData:[AsyncSocket CRLFData] withTimeout:READ_TIMEOUT tag:0    ];
    }
}

有人可以帮我吗?也许我以错误的方式发送数据?除了 ASCII 之外,我还尝试了其他编码,但没有成功。

4

1 回答 1

0

I've managed to do it! It seems that all was going smoothly but I've forgot one detail: On the iOS side I was expecting some CRLF data that I wasn't sending! I've corrected it simply by adding "\r\n" to string that I encode before sending. :)

于 2013-02-19T11:49:09.740 回答