Character encoding issues for Java server and C # clients

Source: Internet
Author: User

When the server interacts with the client, sometimes the server and the client are not written in the same programming language, it is necessary to pay attention to the problem of character encoding conversion. Take Java server and C # Client as an example, at this point the C # end receives the data with GBK encoding representation.

Encoding Encoding = encoding.getencoding ("GBK"); Defining GBK Encoding
returnmsg = Encoding. GetString (Memstream.getbuffer (), 0, Memstream.getbuffer (). Length);

Specific code:

usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Net;usingSystem.Net.Sockets;usingSystem.IO;namespaceClient and java{classProgram {Static stringIP ="127.0.0.1";//Define IP Address        Static intPort =9999;//Port number        Static voidMain (string[] args) {TcpClient TCP=NewTcpClient (); Tcp.              Connect (IP, Port); //TCP ConnectionsMemoryStream Memstream =NULL;//accessing the stream read back            stringReturnmsg =string.                      Empty; //establish a connection to the serverSocket socket =NewSockets (AddressFamily.InterNetwork, SocketType.Stream, protocoltype.tcp);//Defining socket InterfacesIPAddress add = Ipaddress.parse (IP);//provide IP protocolIPEndPoint ENDPT =NewIPEndPoint (add, port);//represents a network endpointSocket. Connect (ENDPT);//establishing a connection between a network endpoint and a host//Receive Data            byte[] buffer =New byte[1024x768];//The returned data is stored in buffer            intRecCount =0; Memstream=NewMemoryStream (); //receive the returned byte stream             while(RecCount = socket. Receive (buffer)) >0)//The received buffer data is greater than 0 o'clock{memstream.write (buffer,0, RecCount);//writes the buffer's data to buffer} Encoding Encoding= Encoding.GetEncoding ("GBK");//Defining GBK Encodingreturnmsg = Encoding. GetString (Memstream.getbuffer (),0, Memstream.getbuffer (). Length);//Convert in-memory flow to GBK encoded formatConsole.WriteLine (returnmsg);        Console.ReadLine (); }            }}

Character encoding issues for Java server and C # clients

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.