0

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.

user1782638
  • 130
  • 1
  • 13
  • Please clarify. You can't send sockets. You can send data *via* a socket. – user207421 Feb 16 '13 at 23:53
  • Yes, sorry by that! I'm trying to send data through a socket ... The program SocketTest receives the data sent from C# through socket but not my iOS app .. But my iOS app receives data from a socket sent with SocketTest ... So I know the data is being sent and that my iOS receives data ... The question is that the data is not being sent properly for being read by the iOS app or the iOS app is not handling properly the data sent trough my C# app and I don't know how to make it work properly... It is strange since SocketTest can receive from C# and send to iOS without problem... – user1782638 Feb 17 '13 at 12:52

1 Answers1

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

user1782638
  • 130
  • 1
  • 13