I have to develop an application that connect to Java server listening over a port, third party gave me the snippet of Java code to send and receive data over the port . String request = "request";
try (Socket soc = new Socket("localhost", 1234)) {
DataOutputStream output = new DataOutputStream(soc.getOutputStream());
output.writeUTF(request);
output.flush();
DataInputStream input = new DataInputStream(soc.getInputStream());
String response = input.readUTF(); }
I have to write or convert same in C# so as to use by other users and as per company needs. I wrote a code in C# as follows, but it does differently while it encodes the request-
string Request = "request";
byte[] data = new byte[1024];
IPAddress ipAdress = IPAddress.Parse("127.0.0.1");
IPEndPoint ipEndpoint = new IPEndPoint(ipAdress, 1234);
Socket client = new Socket(ipAdress.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
try
{
client.Connect(ipEndpoint);
byte[] sendmsg = Encoding.UTF8.GetBytes(ComRequest);
int n = client.Send(sendmsg);}
Bytes written over port by C# code is different than the Java code. Is there any equivalent to writeUTF and readUTF function of Java in C#?
Third party also gave me the function of C++ which performs the same operation of string to UTF8 conversion, but I am bad in C++, don't know what c_str() function does? C++ code-
void convertStringToModifiedUTF8(string content, char* outputBuffer){
strcpy(outputBuffer+2, content.c_str());
outputBuffer[0]= content.length() & 0xFF00;
outputBuffer[1]= content.length() & 0x00FF;
}
Can anyone help in either way to have Java equivalent function or C# code equivalent to above C++ code?
I am highlighting some of the bytes changes-
Java-
0000 02 00 00 00 45 00 00 f8 4c 4e 40 00 80 06 00 00 ....E...LN@..... 0010 7f 00 00 01 7f 00 00 01 c9 c0 16 e3 2f ac ea cf ............/... 0020 8b 6a 2e d1 50 18 01 00 58 71 00 00 00 ce 7b 22 .j..P...Xq....
C# -
0000 02 00 00 00 45 00 00 **fa** 4c **5f** 40 00 80 06 00 00 ....E...L_@..... 00 10 7f 00 00 01 7f 00 00 01 c9 dd 16 e3 81 e5 cd 90 ................ 0020 87 47 fb 46 50 18 01 00 76 64 00 00 7b 20 22 6a .G.FP...vd..