|
hi,
first please put all the code part in the try and catch block.
then you easy to find the correct Error.
You are telling "when I add the line to Read from the data (ExecuteReader) I get the following exception error:
"
Then definitely it is not connection error ..It is a Error in the query you try to execute.
please check wheter the Table name and every thing is coeect in the Query.
regards,
pubudu
|
|
|
|
|
I have to make for school a project in C# that does the work of router.
I mean that i have to check on certain ports for an incomming connections and then check if there is a rule made for this port. If there is rule made for this then i have to forward this connection to a certain ip address or block it.
My user interface is done and i even created a service which will do the handling of the connections.
But i am stuck now with how will i forward a connection.
This is some of the function i made:
This is the function to create the socket:
{
LocalSocket = new Socket(AddressFamily.InterNetwork, SocketType.Raw, ProtocolType.IP);
LocalSocket.Blocking = false;
LocalSocket.Bind(new IPEndPoint(IPAddress.Parse(GetLocalIP()),
LocalRole.GetSourcePort()));
}
after that i call this function:
public void Start()
{
IAsyncResult ar = LocalSocket.BeginReceive(receive_buf_bytes, 0, len_receive_buf, SocketFlags.None, new AsyncCallback(CallReceive), this);
}
and this is the function that should forward the connection but i am not sure how to complete it:
private void CallReceive(IAsyncResult ar)
{
int received_bytes;
received_bytes = LocalSocket.EndReceive(ar);
//Receive(receive_buf_bytes, received_bytes);
if(LocalRole.GetSourceIp() != "0.0.0.0")
{
if(LocalRole.GetSourceIp() == ((IPEndPoint)(LocalSocket.RemoteEndPoint)).Address.ToString())
{
}
Start();
}
Could someone explain me how i should make this function to forward the connection?
thx
cyrus
|
|
|
|
|
Here is a generic Proxy class that forwards connections
class Proxy
{
private string appName = "HSMSProxy";
[STAThread]
static void Main(string[] args)
{
new Proxy().Start();
}
public void Start()
{
TcpListener listener = new TcpListener(5001);
listener.Start();
TcpClient activeEntity = listener.AcceptTcpClient();
TcpClient passiveEntity = new TcpClient("TargetMachine", 5002);
NetworkStream activeStream = activeEntity.GetStream();
NetworkStream passiveStream = passiveEntity.GetStream();
new InterStreamWriter(activeStream, passiveStream).Start();
new InterStreamWriter(passiveStream, activeStream).Start();
Console.ReadLine();
}
private void Log(string message)
{
Console.WriteLine(message);
}
}
class InterStreamWriter
{
NetworkStream readStream;
NetworkStream writeStream;
public InterStreamWriter(NetworkStream readStream, NetworkStream writeStream)
{
this.readStream = readStream;
this.writeStream = writeStream;
}
public void Start()
{
new System.Threading.Thread(new System.Threading.ThreadStart(ThreadFunc)).Start();
}
private void ThreadFunc()
{
while (true)
{
ArrayList byteList = new ArrayList();
byte[] myReadBuffer = new byte[1024];
int numberOfBytesRead = 0;
do
{
numberOfBytesRead = readStream.Read(myReadBuffer, 0, myReadBuffer.Length);
for (int i = 0; i<numberOfBytesRead; ++i)
byteList.Add(myReadBuffer[i]);
}
while(readStream.DataAvailable);
byte[] totalBuffer = (byte[])byteList.ToArray(typeof(byte));
writeStream.Write(totalBuffer, 0, totalBuffer.Length);
writeStream.Flush();
byteList.Clear();
}
}
}
Regards
Senthil
_____________________________
My Blog | My Articles | WinMacro
|
|
|
|
|
will it work when a udp packet arrives ?
and can it work when set more than of ths objects and will it wait until a connection is made ?
|
|
|
|
|
You can make it work for UDP packets too. I'm not sure what you meant by your second question. It'll wait till a connection is made and will then connect to the target Machine:IP. After that, it is two way communication, either machine can send and receive data.
Regards
Senthil
_____________________________
My Blog | My Articles | WinMacro
|
|
|
|
|
Sorry to bother again your code are already a great help.
But could you tell me how to make it work for udp .
Because im not sure how to do since there is no udplistener class .
thx for the help
Cyrus
|
|
|
|
|
I'm having a hard time handling logical palette's in C#. What I'm trying to do, is extract a palette from a 256 color bitmap, place it into a logical palette, select the logical palette as the current system palette, realize the new palette, and then draw the 256 color bitmap to the screen using the colors in it's palette (which is now the current system palette). Everything seems to be working great up to the point where I start drawing the bitmap. When I'm running the desktop in 256 color mode, it will only draw the bitmap in 16 colors (Windows standard colors) even though the palette is selected and realized correctly. I've included the code I've used to implement this. If anyone knows how to get this to work correctly, let me know. Thanks in advance! Anyway, here is the code I'm using.
<br />
IntPtr hWindowDC = IntPtr.Zero;<br />
<br />
public class User<br />
{<br />
[DllImport("user32.dll")]<br />
public static extern IntPtr GetDC(IntPtr hWnd);<br />
<br />
[DllImport("user32.dll")]<br />
public static extern void ReleaseDC(IntPtr hWnd, IntPtr hDC);<br />
}<br />
<br />
public class Gdi<br />
{<br />
[DllImport("gdi32.dll")]<br />
public static extern IntPtr SelectPalette(IntPtr hDC, IntPtr hPalette, bool bForceBackground);<br />
<br />
[DllImport("gdi32.dll")]<br />
public static extern int RealizePalette(IntPtr hDC);<br />
<br />
[DllImport("gdi32.dll")]<br />
public static extern int GetDeviceCaps(IntPtr hDC, int nIndex);<br />
<br />
[DllImport("gdi32.dll")]<br />
public static extern IntPtr CreatePalette([In] IntPtr lpLogPalette);<br />
<br />
public const int PC_RESERVED = 0x01;
public const int PC_EXPLICIT = 0x02;
public const int PC_NOCOLLAPSE = 0x04;
}<br />
<br />
[StructLayout(LayoutKind.Sequential)]<br />
public struct LOGPALETTE<br />
{<br />
public ushort palVersion;<br />
public ushort palNumEntries;<br />
public PALETTEENTRY palPalEntry;<br />
<br />
public LOGPALETTE(ushort entries)<br />
{<br />
this.palVersion = 0x300;<br />
this.palNumEntries = entries;<br />
this.palPalEntry = new PALETTEENTRY(0,0,0,0);<br />
}<br />
<br />
public int Length<br />
{<br />
get<br />
{<br />
return Marshal.SizeOf(palVersion) + Marshal.SizeOf(palNumEntries) + (this.palNumEntries * 4);<br />
}<br />
}<br />
}<br />
<br />
[StructLayout(LayoutKind.Sequential)]<br />
public struct PALETTEENTRY<br />
{<br />
public byte peRed;<br />
public byte peGreen;<br />
public byte peBlue;<br />
public byte peFlags;<br />
<br />
public PALETTEENTRY(byte red, byte green, byte blue, byte flags)<br />
{<br />
this.peRed = red;<br />
this.peGreen = green;<br />
this.peBlue = blue;<br />
this.peFlags = flags;<br />
}<br />
<br />
public override string ToString()<br />
{<br />
return string.Format("{0} {1} {2}", Convert.ToString(peRed, 10), Convert.ToString(peGreen, 10), Convert.ToString(peBlue, 10));<br />
}<br />
<br />
}<br />
<br />
<br />
private static void LoadImagePalette()<br />
{<br />
Assembly exeAssembly = Assembly.GetExecutingAssembly();<br />
<br />
Stream imgStream = null;<br />
<br />
imgStream = exeAssembly.GetManifestResourceStream("MyPalette.image256.bmp");<br />
if( !(null==imgStream) )<br />
{<br />
Image image = Bitmap.FromStream( imgStream );
LOGPALETTE logPal = new LOGPALETTE((ushort)(image.Palette.Entries.Length));<br />
<br />
byte []paletteBytes = new byte[image.Palette.Entries.Length*4];<br />
<br />
for (int i=0; i < image.Palette.Entries.Length; i++)<br />
{<br />
paletteBytes[(i*4)] = image.Palette.Entries[i].R;
paletteBytes[(i*4)+1] = image.Palette.Entries[i].G;
paletteBytes[(i*4)+2] = image.Palette.Entries[i].B;
paletteBytes[(i*4)+3] = 0;
}<br />
<br />
IntPtr ptrPal = Marshal.AllocHGlobal(logPal.Length);<br />
Marshal.StructureToPtr(logPal, ptrPal, true);<br />
<br />
IntPtr palPalEntryAddress = new IntPtr(ptrPal.ToInt32() + Marshal.SizeOf(logPal.palVersion) + Marshal.SizeOf(logPal.palNumEntries) );<br />
Marshal.Copy(paletteBytes, 0, palPalEntryAddress, paletteBytes.Length);<br />
<br />
IntPtr hPalette = Gdi.CreatePalette(ptrPal);<br />
<br />
Gdi.SelectPalette(hWindowDC, hPalette, false);
Gdi.RealizePalette(hWindowDC);<br />
<br />
Graphics newGraphics = Graphics.FromHdc(hWindowDC);<br />
newGraphics.DrawImage(image, 0, 0);<br />
<br />
if (ptrPal != IntPtr.Zero)<br />
Marshal.FreeHGlobal(ptrPal);<br />
<br />
imgStream.Close();<br />
<br />
}<br />
}<br />
|
|
|
|
|
I'm creating an application in C# which is using the word automation. But i don't exactly understand how you can select the printer tray.
Also does anyone have a good tutorial on word automation on C# and printing
Master.Blaster
|
|
|
|
|
Help ! I'm utterly confused on Remoting....
I need my Client Proxy Class to hook to a running instance of a Server Object and to know the state of the object. Anyone please got any ideas on how this is done (in simple language if possible, I'm very new to c#)..
I'm tearing whats left of my hair out trying to find a tutorial for this. I can instantiate a class and get it to do things remotely but I dont just want that I want the class to be a hook to the app and know if its connected to a database etc etc....
Any ideas PLEASE...PLEASE...I'm going nuts with this one...
Cheers
Chubby.
|
|
|
|
|
Maybe you're looking for the RemotingServices.Marshal method? You can pass an instance of the server object to it and all clients will be able to access that object.
Regards
Senthil
_____________________________
My Blog | My Articles | WinMacro
|
|
|
|
|
That's exactly what I was looking for. With your help I managed to prove the concept of hooking to my Server EXE.
Cheers.
Chubby..
|
|
|
|
|
Hello everyone
I have some experience in programming C++ and am quite new at C#. Now my question is: Since in C# there aren't pointer like in C++ anymore, how would I implement an aggregation relationship compared to a composition relationship between two classes?
In C++ I would point through a pointer to the other class to implement a aggreation relationship. How do I do this in C#? Could somebody send me a code snipped for let's say the classes 'airplane', 'cockpit' and 'wheel'.
Airplane and cockpit are supposed to represent a composition.
Airplane and wheel are supposed to represent an aggregation.
Thanks for you help in advance!!
Diego
|
|
|
|
|
All objects in C# are "by reference".
So, in its simplest form:
public class Airplane
{
public Cockpit cockpit;
public Wheel[] wheels;
public Airplane()
{
cockpit=new Cockpit();
wheels=new Wheel[] {new Wheel(), new Wheel(), new Wheel(), new Wheel()};
}
}
Now, one would never see that kind of code in real life, I would hope.
Marc
My website
Latest Articles:
Undo/Redo Buffer
Memento Design Pattern
|
|
|
|
|
|
I think it would be kinda difficult to implement true aggregation because of the garbage collected environment. I assume that aggregation implies that the aggregated entity can outlive the aggregating entity.
In Marc's example, if the GC decides to collect an instance of Airplane and the Wheel array is referenced only by that instance, then it will also be GC'ed. Unless of course, you specifically store a reference to that array somewhere else. Again, if someone else is holding on to a reference to cockpit, it won't get GC'ed along with Airplane.
In C++, you're in control of reclaiming memory, so you can accurately delete the Wheels when needed.
Regards
Senthil
_____________________________
My Blog | My Articles | WinMacro
|
|
|
|
|
S. Senthil Kumar wrote:
In Marc's example, if the GC decides to collect an instance of Airplane and the Wheel array is referenced only by that instance, then it will also be GC'ed.
If this were the case, why would you want to keep the Wheels? In C++ this would cause a memory leak if you got rid of the Aeroplane (or Airplane if you prefer) without also deleting the Wheels.
My: Blog | Photos
WDevs.com - Open Source Code Hosting, Blogs, FTP, Mail and More
|
|
|
|
|
But isn't that the point of aggregation? If the Wheels go off when Airplane goes off, then it's composition.
You're right though, a live Wheels object is of no use if it can be referenced by anyone. Your point also means that for composition to work, you must not return references to inner objects.
Regards
Senthil
_____________________________
My Blog | My Articles | WinMacro
|
|
|
|
|
Hi All,
Is it a Bug or what? whenever we write Onvalidating() event of some contorl. It works fine with all controls but toolbar buttons doesn't receive focus so they doesn't trigger the causes validation as well.
Kindly tell me soem way so that these buttons also validate the controls.
Thanx in advance.
sorry for my bad English.
|
|
|
|
|
I ran into this problem sometime ago. I have several MDI children, each with a toolbar. So I added this routine to the base class, and call it as the first thing on th ButtonClick handler for the toolbar.
protected void ValidateFocusedControl()
{
foreach(Control ctl in Controls)
if(ValidateFocusedControlRecursive(ctl))
break;
}
bool ValidateFocusedControlRecursive(Control ctl)
{
if(ctl.Focused)
{
if(ctl.CausesValidation)
{
TextBox txtBox = new TextBox();
txtBox.Bounds = new Rectangle(-100, -100, 1, 1);
txtBox.Parent = ctl.Parent;
txtBox.Focus();
ctl.Focus();
txtBox.Parent = null;
txtBox.Dispose();
}
return true;
}
foreach(Control ctl2 in ctl.Controls)
if(ValidateFocusedControlRecursive(ctl2))
return true;
return false;
}
It basically finds which is the focused control, creates a new TextBox , gives it focus, and sets focus back to the original control.
Good luck!
-- LuisR
Luis Alonso Ramos
Intelectix - Chihuahua, Mexico
Not much here: My CP Blog!
|
|
|
|
|
Oops! sorry about the XML comments. They are the summary , param , and remarks tags.
-- LuisR
Luis Alonso Ramos
Intelectix - Chihuahua, Mexico
Not much here: My CP Blog!
|
|
|
|
|
Hi,
I got a solution as well which does same...
toolbarButton_Click(...)<br />
{<br />
this.toolbar1.Focus();<br />
if(!this.toolbar1.Focused())<br />
return;<br />
}
isn't it a smaller and better solution? it simply focuses the toolbar and checks that whether it is focuses or not, if it doesnt get focus then it means that focus is still on the control beign validated. Just try it.
sorry for my bad English.
|
|
|
|
|
When you click on a toolbar button, the control that has the focus doesn't lose it, and thus Validating/Validated events are not generated.
My solution goes to great length to ensure that the focus is given back to the control that has it, so that it acts the same as you.
Your solution simply takes focus away from the currently focused control, and doesn't give it back. I did try something similar before getting to my recursive solution, but I don't remember the details.
I took your code and gave it a try, and this is what I came up with:
private void toolBar1_ButtonClick(object sender, ToolBarButtonClickEventArgs e)
{
if(!ValidateFocusedControl())
return;
if(e.Button == toolBarButton1)
MessageBox.Show(this, "Button 1 clicked!");
else if(e.Button == toolBarButton2)
MessageBox.Show(this, "Button 2 clicked!");
} The heart of if is that call to ValidateFocusedControl:
protected bool ValidateFocusedControl()
{
bool result = true;
Control ctl = ActiveControl;
toolBar1.Focus();
if(!toolBar1.Focused)
result = false;
ctl.Focus();
return result;
} I gave it a try and it works as expected, even with controls inside tabpages for example.
-- LuisR
Luis Alonso Ramos
Intelectix - Chihuahua, Mexico
Not much here: My CP Blog!
|
|
|
|
|
its alitle hard to explane what i wont.. but i will try any way.
lets say i got 2 classes:
public class person
{
private string firstname;
private string lastname;
private ArrayList exames; // (or HashTable) contaning Exame objects
public string FirstName
{
get{return firstname;}
set{firstname = value;}
}
public string LastName
{
get{return lastname;}
set{lastname = value;}
}
public ArrayList Exames
{
get(return exames;}
set{exames = value;}
}
}
public Class Exame
{
private string examename;
private int grade;
public string ExameName
{
get{return examename;}
set(examename = value;}
}
public int Grade
{
get{return grade;}
set{grade = value;}
}
}
so far so good..
but now i want to bind the person object to a datagrid. and i want the datagrid to show the next information:
person mike = new person();
mike.FirstName = "mike";
mike.LastName = "james;
mike.Exames.Add(new Exame());
((Exame)mike.Exames[0]).ExameName = "english";
((Exame)mike.Exames[0]).Grade = 100;
mike.Exames.Add(new Exame());
((Exame)mike.Exames[1]).ExameName = "math";
((Exame)mike.Exames[1]).Grade = 90;
in the next way in the grid:
FirstName LastName english math (thouse are the column names)
"mike" "james" 100 90 (the data itself)
(P.S ignore the problem when a persion doesnt have a serten exame.. and all other small error's i know how to overcome them. i just need to know how to show it like this)
|
|
|
|
|
how display line and column on status bar
what do you method to make it
hello everybody ! good luck to you!
|
|
|
|
|
is there anyway to get all the text in a ListBox as an array of strings (string[]) ?
thanks!
|
|
|
|
|