Search code examples
socketsasyncsocket

Send socket from C# to iOS app


I'm having some problems sending a string from my C# app to my iOS app. I tried to send the data using a socket from C# to SocketTest and it receives, I've sent the data using a socket from Socktest to my iOS app and all goes well! The problem is when I try to send the data using a socket from the C# to iOS ... It connects and disconects but doesn't send the message (or my iOS don't read it...)

To send the data using socket in C# I'm using:

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);

And to receive it on iOS I'm using the AsyncSocket library (I think this is the relevant code to post):

-(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    ];
    }
}

Can anyone help me with it? Perhaps I'm sending the data in a wrong way? I've tried also other encodings besides the ASCII but no success.


Solution

  • 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. :)