spot7.org logo
Home PHP C# C++ Android Java Javascript Python IOS SQL HTML Categories

C++ socket - wstring to Java socket


First: The name of this variable definitely is confusing

char[] bytes = new char[8192];

it's and array of char called bytes which under JAVA definitely isn't the same thing.

Second: I don't know which kind of stream you are using here. Some may read char directly some may internally translate from UTF-8 to UTF-16 etc...etc..

int in = sinput.read(bytes, 0, 8192);  

But Third: This doesn't do anything as far as I can tell:

byte[] str =
bytes.toString().getBytes("UTF-8");
ret = new String(str, "UTF-8");

You assume you have a valid String in bytes (which are actually chars) and translate it to a byte array (which is called str) in UTF-8 encoding. Then you translate it back from UTF-8 to the JAVA internal UTF-16 encoding in ret. Why not Just return bytes.toString() ?

One Possibility

You say the strings in the C-Code are wstring aka. based on UTF-16 or UCS2. for Windows as far as I know. So one assumption would be that the data you receive is UTF-16 encoded and if it's actually UCS2 you will still cover all characters in the BMP. So the first thing I would try is to directly decode it from UTF-16 like:

byte[] bytes = new byte[BUFFERSIZE];
// Actually read bytes -- don't know
// if your stream can handle that
int n sinput.read(bytes,0,BUFFERSIZE);
// silently assume n % 2 == 0 and UTF-16 doesn't
use surrogates
String str = new String(bytes,n,"UTF16-BE"
/*"UTF16-LE"*/);

However

The std::wstring format most of the time is only used for internal representations of strings since for most characters you have one string position for one character. You do not have to fight with variable length encodings. Thats fully true for UCS4 and almost true for UTF-16. That gives it a great advantage internally. But for the external representation aka. File or Internet UTF-8 is the lingua franca since it is compact, doesn't care for endianess and ASCII is still visible as ASCII without any interspersed zero bytes.. and much more.

So even without seeing the C-Code I would still assume that the external representation has a good chance of being UTF-8. In that case you would simply have to do:

byte[] bytes = new byte[BUFFERSIZE];
// Actually read bytes -- don't know
// if your stream can handle that
sinput.read(bytes,0,BUFFERSIZE);
String str = new String(bytes,BUFFERSIZE,"UTF8");

For your soutput.write(...) you also would have to call getBytes("UTF8")


Categories : Java

Related to : C++ socket - wstring to Java socket
How to get socket's buffered data when socket closed due to network loss?
The socket doesn't get closed due to network loss. Only you can close your socket. The connection can be reset due to network problems. In this case all pending data is discarded. Nothing you can do about it.

Categories : Java
Sending List Clients by Socket
This goal is doomed to failure. I don't know what you mean by "im never getting the return byte[] from the method" (you're saying the ObjectToByteArray method never returns?), but it doesn't really matter because the Socket class is not serializable. There's no way it could be in any useful way: serialization is primarily used to either persist some data (e.g. in a file) or to send some data to s

Categories : C#
What are the different between VMCI socket, Berkeley Socket and winsock?
Berkeley Sockets is the original TCP/IP API and is found in Unix-based operating systems. Winsock is a source and binary API/ABI which was created to bring order out of the confusion of multi-vendor TCP/IP on Windows in the 1990s before Microsoft clued up. It is found on Windows. VCMI is for communicating between VMWare hosts and guests, or between guests.

Categories : Sockets
Node.js, socket.io and IIS 7
From what I've read IIS 8 is the first version of IIS with WebSockets, until then longpolling is used. As for the output in your comment, you will see that because the Socket.io client is going to attempt to establish a connection by default with longpolling and then will attempt to upgrade the connection to WebSockets, unless you explicitly specify that it shouldn't. This can be done in the opti

Categories : Node Js
socket connect() vs bind()
The one liner : bind() to own address, connect() to remote address. Quoting from the man page of bind() bind() assigns the address specified by addr to the socket referred to by the file descriptor sockfd. addrlen specifies the size, in bytes, of the address structure pointed to by addr. Traditionally, this operation is called "assigning a name to a socket". and, from the same for connect()

Categories : C
Recently Add
Redirecting the output directory of 'mvn package' or 'mvn compile' command
No suitable constructor found for ProductoExtranjero
java Composite design pattern(Directory &File)
Java JTree's ui refresh after removing node from parent
First REST Spring application
How to cancel Indexing of a Solr document using Update Request Processor
PowerMock - Mock a Singleton with a Private Constructor
Calling a Postgres stored function SQL error
Where to store Morphlines Java custom command class?
Generic repository using map
How can I scroll a ScrolledComposited in Eclipse SWT Design view?
2 Frames/layout in 1 Activity
Writing a switch differently
Next button opens another activity when its reaches the array limit
Is EclipseLink MOXy capable of applying JSR-303 Bean Validation when unmarshalling XML to object?
Why my jdk can't work,and before the java_home, there is a space that is not from me
How to add List of objects in a Map
How to make notepad++ function like regular notepad in cmd?
Cell renderer and the lost focus
how can I implement iterable for LinkedList>
Disable Androids image-crunch in eclipse (run as) builds
java 8 lambda myMap.stream().count() != myMap.size() after merging myMap
Issue with Calendar calculation that spans 2 calendar years
JSF 2.0 Spring bean injection
Java Regex ReplaceAll with grouping
Getting any word and last word using sed
Clicking on link on JEditorPane throws IOException
printing out difference of two arrays
Spring Bean Alias in JavaConfig
Using Factory Method to Create Generics
© Copyright 2017 spot7.org Publishing Limited. All rights reserved.