0

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..
Prateek Shukla
  • 593
  • 2
  • 7
  • 27
  • Please provide an example of byte difference. – 273K Mar 22 '19 at 05:19
  • 1
    [Description of Java Modified UTF-8](https://stackoverflow.com/questions/7921016/what-does-it-mean-to-say-java-modified-utf-8-encoding) – ProgrammingLlama Mar 22 '19 at 05:21
  • `outputBuffer[0]= content.length() & 0xFF00;` That's broken on typical systems with 8 bit chars if the intent is to prepend the length of the string to `outputBuffer`, btw. – Shawn Mar 22 '19 at 05:29
  • 2
    The robust and maintainable solution is to not use `DataOutputStream`, which is meant for serialization of Java objects and not for general platform-neutral communication. (Also, don't rely on "third parties" to provide you code that you then use without understanding it.) – molbdnilo Mar 22 '19 at 05:33
  • 1
    Consider using [`TcpClient`](https://learn.microsoft.com/en-us/dotnet/api/system.net.sockets.tcpclient?view=netframework-4.7.2) class as it is less fiddly than dealing with straight-up `Socket` –  Mar 22 '19 at 05:35
  • The C++ one is not doing a conversion to UTF-8. Perhaps the variable `content` already contains UTF-8 encoded data. – Ben Voigt Mar 22 '19 at 05:38

0 Answers0