简体   繁体   中英

How can I send clients IP address with data from server to another client?

I have developed a program which works like client-server-client model.

I have achieved everything and successfully have a running code for me to:

  • send information from one client to another with multiple clients connected to the server

  • Server acts as a man-in-the-middle and

  • clients interact with each other

I want to know an efficient way to transfer clients(sender) ip address through server to client(receiver) with data(images, audio, etc) so that the receiving client can know who sent the specific data.

FYI there will be multiple clients who will continuously send data to a specific client at the same time so I need to distinguish between the sender of the data and have a record of which client sent specific data.

MY Code:

Server:

try
   {
    // receive data
    byte[] buffer = new byte[300000];
    current.Receive(buffer, buffer.Length, SocketFlags.None);

    Console.WriteLine("Received: " + current.Receive(buffer, buffer.Length, SocketFlags.None));
    ImageConverter convertData = new ImageConverter();
    Image image = (Image)convertData.ConvertFrom(buffer);
    image.Save("image.png");
    Console.WriteLine("Image saved");
     }
    catch (Exception ex)
    {
         Console.WriteLine("Image not saved");
         logFileWrite("Image not saved");
         Console.WriteLine(ex.ToString());
    }

Client:

try
{
    //send the file
    Bitmap bm = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height);
    Graphics g = Graphics.FromImage(bm);
    g.CopyFromScreen(0, 0, 0, 0, bm.Size);
    MemoryStream ms = new MemoryStream();
    bm.Save(ms, ImageFormat.Jpeg);

    byte[] buffer = ms.ToArray();

    ClientLiveScreenSocket.Send(buffer, 0, buffer.Length, SocketFlags.None);
    Console.WriteLine("Send success " + buffer.Length + " kb");
    logFileWrite("Send success " + buffer.Length + " kb");
    Thread.Sleep(500);
    }
    catch(Exception ex)
    {
        Console.WriteLine(ex.ToString());
    }

After (or before) image having sent, send IPAddress 's wired representation:

var wireFormat = sourceClientAddress.GetAddressBytes();

current.Send(wireFormat, 0, wireFormat.Length, SocketFlags.None);

On client side you just receive these 4(16) bytes and reconstruct that address via:

// pseudo code
byte[] clientBytes = GetAccordingToMyProtocol();

//real code
var sourceClientAddress = new IPAddress(clientBytes);

So, any sequence of socket.Receive and socket.Send calls (both on client side and server side) according to your application logic is called application protocol . Often these entities become separate programming abstractions in big codebases.

UPDATE

Check this repo. There is a lot of space to refactoring and there is a bug - image is not passed, but it is matter of time to fix it.

So, there are 3(4) projects in ImageRetranslation folder, run them in turn (i mainly ran them in debug mode, there can be bugs without debugger exactly, i know):

  1. Retranslation server
  2. Retranslation receiver
  3. And just once (without debug) - ImageSourceClient - actually sends image to server.

So, despite of bug with image transferring the feature you requested for - passing host:port works well.

See ya!

UPDATE 2

Bugs fixed, repo is viable - check it out.

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