|
To be honest, given what you're trying to do, I would look to use RX to observe property changes. In fact, that's exactly what I have done for some of my clients.
This space for rent
|
|
|
|
|
|
I tend to use a variation of code that builds on something like this[^].
This space for rent
|
|
|
|
|
|
Thank you, Kenneth! I'll take a look at it.
If the brain were so simple we could understand it, we would be so simple we couldn't. — Lyall Watson
|
|
|
|
|
when i click a button just like i click a "Category" it auto generate all the category values button.
|
|
|
|
|
It would help to know what UI you are building for...
Have a collection of categories in the code behind
Instead of buttons use a list control (listbox with a button as the item content) and bind the category collection to the list control (DataSource or ItemSSource).
Never underestimate the power of human stupidity
RAH
|
|
|
|
|
it is also an perfect but i want to build and application to touch screen.
|
|
|
|
|
Touch screen does not dictate the structure. You may be interested in some of these articles[^]
Never underestimate the power of human stupidity
RAH
|
|
|
|
|
the above c sharp code will send client desktop images to server . it works for single client . we want to implement this for multiple client. please provide help to implement it.
SERVER code:-
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Runtime.Remoting;
using System.Linq;
using System.Text;
using System.Net .Sockets ;
using System.Windows.Forms;
using System.Threading ;
using System.Runtime.Serialization.Formatters.Binary;
using System.Net;
namespace simpleserver
{
public partial class Form2 : Form
{
private readonly int port;
private TcpClient client;
private TcpListener server;
private NetworkStream mainstream;
private readonly Thread Listening;
private readonly Thread Getimg;
public Form2(int Port)
{
port = Port;
client = new TcpClient();
Listening = new Thread(StartListening);
Getimg = new Thread(ReceiveImage);
InitializeComponent();
}
private void StartListening()
{
while (!client.Connected)
{
server.Start();
client = server.AcceptTcpClient();
}
Getimg.Start();
}
private void stop()
{
server.Stop();
client = null;
if (Listening .IsAlive )Listening .Abort ();
if ( Getimg .IsAlive )Getimg .Abort ();
}
private void ReceiveImage()
{
BinaryFormatter binformatter = new BinaryFormatter();
while (client.Connected)
{
mainstream = client.GetStream();
pictureBox1.Image =(Image ) binformatter.Deserialize(mainstream);
}
}
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
server = new TcpListener(IPAddress .Any ,port );
Listening .Start ();
}
}
}
CLIENT CODE:-
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Imaging;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net.Sockets;
using System.Runtime.Serialization.Formatters.Binary;
namespace sampleclient
{
public partial class Form1 : Form
{
private readonly TcpClient client = new TcpClient();
private NetworkStream mainStream;
private int portNumber;
private static Image GetDesktop()
{
Rectangle bounds = Screen.PrimaryScreen.Bounds;
Bitmap screenshot = new Bitmap(bounds .Width ,bounds .Height,PixelFormat .Format32bppArgb );
Graphics g = Graphics.FromImage(screenshot);
g.CopyFromScreen(bounds.X, bounds.Y, 0, 0, bounds.Size, CopyPixelOperation.SourceCopy);
return screenshot;
}
private void SendDesktop()
{
BinaryFormatter binformatter = new BinaryFormatter ();
mainStream = client.GetStream();
binformatter.Serialize (mainStream, GetDesktop());
}
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
portNumber = int.Parse(textBox2.Text);
try
{
client.Connect(textBox1.Text, portNumber);
MessageBox.Show("connected success");
timer1.Start();
}
catch (Exception)
{
MessageBox.Show("fail");
}
}
private void timer1_Tick(object sender, EventArgs e)
{
SendDesktop ();
}
}
}
modified 23-Jan-16 12:12pm.
|
|
|
|
|
Apart from that being terrible code - VS default names for everything, commented out blocks, assumptions that users never type mistakes, ignoring exception detail when you do catch them, and so forth - we have no idea what you mean by "make it work for multiple clients", or what you problem with doing it yourself is.
And when you add that we aren't here to do your job for you I think it's very unlikely that you will get what you want with this question.
Instead, sit down and work out exactly what you are trying to do, and work out what you need to implement in order to do it. Then make a list of what you do and don;t know how to do, and implement the stuff you do.
For the stuff you don't start with google, and ask specific questions when you can't find what you need, or don't understand whatyou find.
But just saving "provide help to implement it" isn't going to get you very far...
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
|
|
|
|
|
First of all, have you considered using
code pre tags when posting code? It makes it much easier to read.
Second, what have you attempted so far?
"I've seen more information on a frickin' sticky note!" - Dave Kreskowiak
|
|
|
|
|
|
As I suppose you have found, a point-to-point socket connection is not what you want for this.
You will likely find success by using a Web Service or similar, but I expect a database connection would be even simpler because you wouldn't actually need to implement anything on the server.
|
|
|
|
|
PIEBALDconsult wrote: but I expect a database connection would be even simpler
Isn't it not good to expose the database across the network like that?
The difficult we do right away...
...the impossible takes slightly longer.
|
|
|
|
|
I assume it's a local network, but if not, then a Web Service may provide better security.
|
|
|
|
|
Well, even though as others as pointed out, this is not good code... One easy answer is simply that each client code append say a block of a given length before the data for identification purpose (assuming this is for educative purpose).
Philippe Mori
|
|
|
|
|
Context for the solution helps. So is this a commercial solution, something for school, experimenting, etc.
As others have noted there are standard idioms that you can research for doing client/server setups.
If you want to roll you own then
1. Separate out communication from processing. Different classes. So you need classes that 'send' a message and classes that 'receive' a message. Then different classes that package a 'desktop image' as a message
2. On the server side when a server accepts a client connection then the server spins up a new thread to handle that request (for messages between the original client and the server.) It does that for each client and perhaps for each message that a unique client sends. A quick glance at your code suggests that you are not creating a new thread for each request.
|
|
|
|
|
I am aware of two "fake data" generators for C#, Faker and Faker.NET, and I have posted a message with information about them on the "Free Tools" forum today: [^].
These generators are both libraries of static functions that return strings. Faker.Net (available through NuGet) has some interesting coding techniques.
I am curious if anyone knows of a generator that spits out DataTables.
thanks, Bill
«Tell me and I forget. Teach me and I remember. Involve me and I learn.» Benjamin Franklin
|
|
|
|
|
BillWoodruff wrote: I am curious if anyone knows of a generator that spits out DataTables. Shouldn't be too hard to build..
Then again, what use would an app be that generates random tables with random relations? Makes more sense to fill an existing table-structure with fake or random data.
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
|
|
|
|
|
Hi Eddy,
I have a need for some huge auto-generated DataTables ... for stress-testing a UI generator... and, indeed, I am going to write one, and have started writing one, but wanted to see if there was one already out there. Right now, I am using Faker.Net for a random-data-by-Type provider, and building on top of that.
Being free of life$tyle constraints enables me to pursue such esoteric frivolities.
cheers, Bill
«Tell me and I forget. Teach me and I remember. Involve me and I learn.» Benjamin Franklin
modified 22-Jan-16 21:25pm.
|
|
|
|
|
BillWoodruff wrote: stress-testing Wouldn't call that frivolities
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
|
|
|
|
|
I have already asked this question on the MSDN Community, but I haven't gotten an answer yet, so I hope the people here can help me.
As a little test project I wrote an ICustomMarshaler, but it's not behaving as I expected. The complete project can be downloaded as zip from my OneDrive.
I'm trying to work with 2 functions from Crypt32.dll which I import in my code:
[DllImport("Crypt32.dll", SetLastError = true)]
private static extern bool CryptProtectData([MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(ByteArrayToDataBlobMarshaler))] byte[] pDataIn, [Optional] string szDataDescr, [MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(ByteArrayToDataBlobMarshaler)), Optional] byte[] pOptionalEntropy, IntPtr pvReserved, [Optional] IntPtr pPromptStruct, int dwFlags, [In, MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(ByteArrayToDataBlobMarshaler)), Out] byte[] pDataOut);
[DllImport("Crypt32.dll", SetLastError = true)]
private static extern bool CryptUnprotectData([MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(ByteArrayToDataBlobMarshaler))] byte[] pDataIn, [Optional] string ppszDataDescr, [MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(ByteArrayToDataBlobMarshaler)), Optional] byte[] pOptionalEntropy, IntPtr pvReserved, [Optional] IntPtr pPromptStruct, int dwFlags, [In, MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(ByteArrayToDataBlobMarshaler)), Out] byte[] pDataOut);
Marshaling into native code works fine, marshaling back seems to work too when using the debugger to step through the MarshalNativeToManaged method:
public object MarshalNativeToManaged(IntPtr pNativeData)
{
if (pNativeData == IntPtr.Zero) { return null; }
int length = Marshal.ReadInt32(pNativeData);
byte[] data = new byte[length];
IntPtr dataPtr = Marshal.ReadIntPtr(pNativeData, IntPtr.Size);
Marshal.Copy(dataPtr, data, 0, length);
return data;
}
When arriving at the 'return data;' point, the array actually has the correct data in it.
The data is called from the following code:
byte[] clearBytes = Encoding.Unicode.GetBytes(clearText);
byte[] cipherBytes = new byte[0];
if (CryptProtectData(clearBytes, null, null, IntPtr.Zero, IntPtr.Zero, 0, cipherBytes))
After the call to this function, cipherBytes is still an empty array. According to standard C# rules this is to be expected, as i'm not sending cipherBytes as ref or out. So why do I actually expect it to be updated? Because the Marshal class can also do it.
I rewrote the code a little bit and used a class with StructLayout.Sequential and sent it to the native code, no matter how I send the parameters, Marshal completely ignores if it's an In, Out or InOut parameter and Always reads pDataIn, and always overwrites pDataOut, I expected the same from ICustomMarshaler.
The imports for sending it as a struct have errors in them, intentional, as I was trying to see how the In and Out attributes are used when marshaling as UnmanagedType.LPStruct. Turns out they're ignored.
[DllImport("Crypt32.dll", SetLastError = true, EntryPoint = "CryptProtectData")]
private static extern bool CryptProtectData4([MarshalAs(UnmanagedType.LPStruct), Out] DataBlob pDataIn, [Optional] string szDataDescr, [MarshalAs(UnmanagedType.LPStruct), Optional] DataBlob pOptionalEntropy, IntPtr pvReserved, [Optional] IntPtr pPromptStruct, int dwFlags, [MarshalAs(UnmanagedType.LPStruct), Out] DataBlob pDataOut);
[DllImport("Crypt32.dll", SetLastError = true, EntryPoint = "CryptUnprotectData")]
private static extern bool CryptUnprotectData4([MarshalAs(UnmanagedType.LPStruct), Out] DataBlob pDataIn, [Optional] string ppszDataDescr, [MarshalAs(UnmanagedType.LPStruct), Optional] DataBlob pOptionalEntropy, IntPtr pvReserved, [Optional] IntPtr pPromptStruct, int dwFlags, [MarshalAs(UnmanagedType.LPStruct), Out] DataBlob pDataOut);
The class is actually longer, has a nice destructor for cleaning up the unmanaged memory and changing the data in the array, but trying to keep the snippets short.
[StructLayout(LayoutKind.Sequential)]
internal class DataBlob
{
[MarshalAs(UnmanagedType.U4)]
private int Length;
private IntPtr Data;
}
The code calling it:
string clearText = "TestString";
byte[] clearBytes = Encoding.Unicode.GetBytes(clearText);
DataBlob clearBlob = new DataBlob(clearBytes);
DataBlob cipherBlob = new DataBlob();
CryptProtectData4(clearBlob, null, null, IntPtr.Zero, IntPtr.Zero, 0, cipherBlob);
byte[] cipherBytes = cipherBlob.GetData();
Even when sending pDataOut decorated with an In attribute, this code manages to change my private fields (not properties), so why can't my ICustomMarshaler change stuff?
|
|
|
|
|
Hi,
I have been using P/Invoke for many years (and wrote a few articles on it), but I always stayed well away from complex marshaling, so I'm no expert on that whatsoever.
Nonetheless, I am confident that the C# compiler seeing these two lines in a method:
byte[] cipherBytes = new byte[0];
if (CryptProtectData(clearBytes, ..., cipherBytes))...
will always consider cipherBytes as a zero-length byte array since you didn't tell CryptProtectData is allowed to modify it, so it will be passed by value (which for an object is an address, not a pointer to an address). Whatever other code does can't change that.
|
|
|
|
|
I have to agree on that according to the normal calling conventions in C#. But as said, the Marshal class can change private fields in a class marshaled as LPStruct. Seeing this I hoped that using the Marshal class with an ICustomMarshaler I had the same options. Guess not.
I'm guessing that's because of unsafe pointers to the private members which are both blittable in my case. I can imagine Microsoft doesn't want random programmers to do those crazy things, and the array I'm trying to Marshal as an unmanaged struct is far from blittable.
|
|
|
|
|