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());
}
CodePudding user response:
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.
CodePudding user response:
You should use X-Forwarded-For header:
The X-Forwarded-For (XFF) request header is a de-facto standard header for identifying the originating IP address of a client connecting to a web server through a proxy server.