jfalexvijay
jfalexvijay

Reputation: 3711

(TCP) Server/Client program

I want to write (TCP) Server Client program. Client code should be in Objetive-C. Server side code should be in C#.

I have written some code. It is working. But the issue is; I am sending the message from Client to Server. Server is receiving the message. But the received message is looking like "Encrypted" data.

code:

Objective-C
const uint8_t *rawdata = (const uint8_t*)[@"Welcome..." UTF8String];
[outputStream write:rawdata maxLength:strlen((const char *)rawdata)];

C#
char[] chara = new char[data.Length / sizeof(char)];
System.Buffer.BlockCopy(data, 0, chara, 0, chara.Length);
String content = new String(chara);
Console.WriteLine("Received data : " + content);

Please help me to fix the issue.

Upvotes: 0

Views: 664

Answers (2)

antf
antf

Reputation: 3222

I do the following, I hope it helps you:

On the client (Objective C) I send the data as follows

const uint8_t *str = (uint8_t *) [messageToSend cStringUsingEncoding:NSUTF8StringEncoding];

On the C# Server I don't use char[] I use byte[] and I read the data from the Socket then I convert them to string as follows (assume that the byte array is named data):

string messageReceived = Encoding.UTF8.GetString(data, 0, bytesRead);

bytesRead is an integer that holds the size of bytes read (I get it from the Stream since I use TCP Sockets).

I hope this helps.

Upvotes: 2

Alexei Levenkov
Alexei Levenkov

Reputation: 100620

It looks like you are sending data as UTF-8, but trying to read it as UTF-16. You need to convert using Encoding.UTF8:

    var content = new UTF8Encoding(true,true).GetString(data)

Upvotes: 1

Related Questions