简体   繁体   中英

How to transfer a string over LAN?

I have two applications which are running on computers on a LAN. I need to transfer a string between them but I can't do this because the Socket.Send method doesn't accept a string. Is there any way to do this?

transfer your String into a Byte-array with the following function:

Encoding.UTF8.GetBytes(str)

ByteArrays can be handled by the Socket.send function.

On the other side, convert your Byte-array into a string again:

Encoding.UTF8.GetString(buffer)

You can convert your string into a byte array on the fly as you send it:

_socket.Send( System.Text.Encoding.UTF8.GetBytes( datastring ) );

At the receiving end, you convert it back into a string like this:

datastring = System.Text.Encoding.UTF8.GetString(
               bytesBuffer, 0, numberOfBytesReceived );

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM