Wojciech Ketrzynski
Wojciech Ketrzynski

Reputation: 2651

How to send JPG over TCP from one Windows Phone to another? I already managed sending text

How to send JPG over TCP from one Windows Phone to another?

I found that in the other SO topic(below) showing how to send text over TCP, but how to turn JPG to bytes and send it? Most of jpges are bigger than 4kb how to deal with that?:

private void sendMessage() {
    connectArgs = new SocketAsyncEventArgs { RemoteEndPoint = new DnsEndPoint(localIP, Int32.Parse(port)) };
    connectArgs.Completed += connectArgs_Completed;
    connection = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
    connection.ConnectAsync(connectArgs);
}

void connectArgs_Completed(object sender, SocketAsyncEventArgs e) {
    if (e.SocketError == SocketError.Success && firstLoop) {
        firstLoop = false;
        var sendArgs = new SocketAsyncEventArgs();
        var buffer = Encoding.UTF8.GetBytes("MESSAGE STRING" + Environment.NewLine);
        sendArgs.SetBuffer(buffer, 0, buffer.Length);
        sendArgs.Completed += sendArgs_Completed;
        e.ConnectSocket.SendAsync(sendArgs);
    } else {
        //blad
    }
}

Upvotes: 0

Views: 270

Answers (2)

aevitas
aevitas

Reputation: 3833

TCP doesn't limit the size of the packets to 4KB, so that shouldn't be a problem for you. You simply need to read all the image bytes either from file using File.ReadAllBytes or a stream reader, and you then send those bytes across to the receiver.

Alternatively, if you have the image as an object, you could use Marshal.StructureToPtr on the sending end and then use Marshal.PtrToStructure to re-create the image object from the bytes you have received, if you want to show the image directly for example.

Upvotes: 1

user2684182
user2684182

Reputation:

You should probably read the raw image data and send that, along with a special "code" or prefix, and then on the receiving end, save the raw image data to a .jpg file and display that. You can do that with any file.

Upvotes: 1

Related Questions