|
We've got a multi-threaded server (we are very early in the project, trying to uncover some risks), and we want to send data using XML.
The class ServerAdapter is supposed to connect to the server (which works fine), to send data, but on the same stream be able to read data (which is causing problems).
In the function "bool Connect()", we want to instantiate the XmlTextReader with the stream that we got from the 'TcpClient client', but somehow that goes wrong. No exception is cast, but the problem definately has something to do with the stream that we get from the call 'client.GetStream()'.
When we try to do the exact same thing, with a stream NOT from a TcpClient, there is no problem. We've checked to see if it had something to do with the access-property on the connection (the 'Socket') on the serverside, but it came up negative. I am pretty much out of ideas, so if you can help me out, it'd be great (first time here, so I dont know the standarts)
We've checked how far it gets with text-outputs, so we KNOW that the XmlTextReader causes the problem.
Here is the part of code that fails.
public class ServerAdapter : IServerAdapter
{
private IServerEventHandler serverEventHandler;
private TcpClient client;
private NetworkStream stream;
private Thread readThread;
private XmlTextReader streamReader;
private XmlTextWriter streamWriter;
private BinaryReader reader;
private XmlValidatingReader validatingReader;
private XmlSchemaCollection schemaCollection;
public ServerAdapter()
{
}
public ServerAdapter( IServerEventHandler serverEventHandler ):base()
{
this.serverEventHandler = serverEventHandler;
}
public bool Connect()
{
bool connected = false;
try
{
client = new TcpClient();
client.Connect( "10.0.55.12", 4312 );
stream = client.GetStream();
streamReader = new XmlTextReader( stream );
streamWriter = new XmlTextWriter( stream, new System.Text.UTF8Encoding() );
blablabla....
Thanks in advance
...Borgbjerg
|
|
|
|
|
You're opening a reader and writer on the same stream without closing either one first. Because the stream can only send or receive data at one time, you need to close the reader before using the writer, and vice versa.
Also, have you looked into .NET Remoting? This seems like a fairly good scenario to implement remoting and you can still use a TcpChannel (either in a configuration file or programmatically) to send and receive data over TCP. The nice thing is that you can work with instances of objects rather than just data back and forth, making this a good OO design in terms of communication.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
We figured it out... It is no problem to read/write the same stream at the same time.
The problem is that the call to XmlTextReader's constructor was blocking the whole thread. So what we did, was more or less to start a new thread for the Reader, so it could block all it wanted. However, the weird thing is that we didn't call reader.Read(), and that it was still blocking. But thanks for the help
And have fun
|
|
|
|
|
I can't really see how that'd work with a NetworkStream , but I guess it works for you. Good.
One thing I would recommend, though, is that you use the encoding of the stream you get from the server (from XmlTextReader.Encoding ) so as not to mix encodings, unless you already know that the stream will be UTF-8 encoded.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
I need to know how can we grant permission to access the Msysobjects through C# coding.
Any kind of suggestion is highly appreciated!
|
|
|
|
|
|
I'm trying to add an image into an Access database.
I have added an image as a binary Byte array which you can deserialize into an image again when extracting this from the database.
I'm however looking into adding an image directly to the database, which in my mind should be possible to do programatically, since you can add a picture directly into the database as an OLEobject.
I'm stuck on this problem, and I really hope that someone has an idea about this.
Thanks.
Bengt
|
|
|
|
|
You have to read a FileStream into byte[] then you can set this value into your DataSet in the image field. This is the way:
System.IO.FileStream stream = new System.IO.FileStreammybmp.bmp", System.IO.FileMode.Open, System.IO.FileAccess.Read);
byte[] buffer = new byte[stream.Length];
stream.Read(buffer, 0, (int)stream.Length);
stream.Close();
dataSet11.Tables["Employees"].Rows[index]["Photo"] = buffer;
Mazy
"Improvisation is the touchstone of wit." - Molière
|
|
|
|
|
Hi everyone,
I have to delete a directory in my program. I use Directory.Delete method. However, there could be some read-only files in the directory. In that case, the method fails. How can I delete the directory inclusing the read-only files without iterating through the files and changing its attributes.
Thanks,
Pankaj
Without struggle, there is no progress
|
|
|
|
|
You iterationist!
EuroCPian Spring 2004 Get Together[^]
"You can have everything in life you want if you will just help enough other people get what they want." --Zig Ziglar
"Get in touch with your Inner Capitalist - I wish you much success!" -- Christopher Duncan, Lounge 9-Feb-2004
|
|
|
|
|
Cannot help it! I love STL
Pankaj
Without struggle, there is no progress
|
|
|
|
|
I think your best shot is to iterate. There really is no way to get around the fact you are manipulating a set of files when you fail. FileInfo.Attributes will have the flags of the file you point it too.
FileInfo info = new FileInfo(thepathtofile);
info.Attributes &= ~FileAttributes.ReadOnly;
It will remove the Read-Only flag from the file if it is set (and do nothing if it isn't).
Of course this insn't foolproof either. If you don't have permissions to change this on a previlaged filesystem then you'll get an SecurityException toggling the flag.
In general terms, you just have to deal with the fact permissions aren't set correctly. Maybe the correct course of action for when attempting a Directory.Delete where you have Read-Only files is to catch the exception and abort. Altert the user and don't attempt to fix it on your own. There might be a good reason why a Read-Only file is in a directory.
|
|
|
|
|
Hi Tom,
Thanks for the reply. I guess I have to iterate.
The thing is that the user itself creates the read-only file! Let me just tell you a bit more about the whole context. The system is basically a report generator that generates excel files dynamically in a particular folder that the user has read/write access to. However, the user should not be able to generate the report and make manual changes to it (save over it). This is why I chose to create the file read only. Of course, it definitely is not fool proof. The user can always go and remove the read only flag. This is an intranet system and the read only flag is there to avoid accidental overwrites rather than a comprehensive data protection measure.
Thanks again for the reply.
Pankaj
Without struggle, there is no progress
|
|
|
|
|
Dear all,
i have to connect two computes on two seperate phoneline through dial up connection. both computers have Windows 2000 professional installed on them.
by doing this i want to access the remote database(Sql Server 7.0) from one computer to the other. To do this i have dialed from one computer to the other and connection was established, but after some time like 1, 2 or three minutes the connection lost due to some problem which i could not be able to identify. could any anybody help me in this regard that what problem is there.
Thanking you all.....
Muhammad Sheraz Khan Pakistan.
|
|
|
|
|
Sounds like a question for the "Operating Systems/SysAdmin" forum.
|
|
|
|
|
Hello,
Any one who could give me an advice on how to deep copy a Hashtable in as short time as possible?
My first implementation is:
protected void CopyHash(ref Hashtable src, ref Hashtable dst)
{
dst.Clear();
Array copy = Array.CreateInstance(typeof(DictionaryEntry), src.Count);
src.CopyTo(copy,0);
foreach (DictionaryEntry Entry in copy)
{
dst.Add(Entry.Key, Entry.Value);
}
}
Thanks
Rickard
|
|
|
|
|
First of all, you don't need to ref a Hashtable , it's already a reference Type! Second, your method above isn't a deep copy either because it doesn't actually copy the objects stored in the table.
An easy way would be to serialize it to a MemoryStream and deserialize to get the new object. Without an instance manager like .NET Remoting uses, the deserialized objects shouldn't be hooked-up to the ones that they were before. The only stipulation is that the keys and values are serializable, which means they must be attributed with SerializableAttribute , even if they implement ISerializable .
If you wanted to use your code above, make sure that you check each key/value for an impementation of ICloneable and call Clone when possible.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
Thanks!
My new implementation:
protected void CopyHash(ref Hashtable src, ref Hashtable dst)
{
MemoryStream ms= new MemoryStream();
BinaryFormatter bf = new BinaryFormatter();
//serialize source to a stream
bf.Serialize(ms,src);
//deserialize it to the copy
ms.Seek(0,0);
dst = (Hashtable)bf.Deserialize(ms);
}
A comment: I had to ref the destination Hashtable otherwise the result of the function was an empty Hashtable.
Rickard
|
|
|
|
|
You don't need the ref on the input Hashtable , though, which is what I was trying to get at. And actually, since the deserialization process creates a new Hashtable , you'd be better off using out for the second parameter, that way you don't have to initialize an extraneous instance of a Hashtable that's going to be a new instance after deserialization anyway. So your method signature would look like this:
protected void CopyHash(Hashtable src, out Hashtable dst);
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
OleDbConnection conn = new OleDbConnection(string.Format("Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0};Mode=ReadWrite",txtDbPath.Text));
OleDbCommand cmd = conn.CreateCommand();
cmd.CommandText = "SELECT Name FROM MSysObjects WHERE Type = 1";
OleDbDataReader reader = null;
try
{
conn.Open();
reader = cmd.ExecuteReader();
while (reader.Read())
{
Console.WriteLine(reader.GetString(0));
lstTables.Items.Add (reader.GetName(0).ToString());
}
}
catch (Exception ex)
{
// Providing something like this in your ...(continued)
Console.Error.WriteLine("An error occured: {0}", ex.Message);
MessageBox.Show(ex.Message.ToString());
}
finally
{
if (reader != null)
reader.Close();
conn.Close();
}
Error is
An error occured: Record(s) cannot be read; no read permission on 'MSysObjects'.
|
|
|
|
|
try:
Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0};User Id=Admin;Password=
or maybe with double quotes on the password, but i dont think you need them, just let it end at the =
Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0};User Id=Admin;Password=""
or with all default options:
Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0};User Id=Admin;Password="";Mode=Share Deny None;Extended Properties="";Jet OLEDB:System database="";Jet OLEDB:Registry Path="";Jet OLEDB:Database Password="";Jet OLEDB:Engine Type=5;Jet OLEDB:Database Locking Mode=1;Jet OLEDB:Global Partial Bulk Ops=2;Jet OLEDB:Global Bulk Transactions=1;Jet OLEDB:New Database Password="";Jet OLEDB:Create System Database=False;Jet OLEDB:Encrypt Database=False;Jet OLEDB:Don't Copy Locale on Compact=False;Jet OLEDB:Compact Without Replica Repair=False;Jet OLEDB:SFP=False
|
|
|
|
|
I ran into a problem that I couldn't see how to solve in C#.
I have a method that takes a Control parameter. I wanted to further restrict it so that is only took Control objects that also implemented a particular interface. So I'd like to be able to declare something like:
public interface IMyNotificationsControl : Control
{
void MyNotification1();
void MyNotification2();
}
Of course, this fails at compilation, since Control is an object, not an interface.
If it were permitted, it would mean that an object of this type would support the entire Control interface (in fact derived from it) but also implement the additional methods.
That way, I could declare my function like this:
public void MyFunction(IMyNotificationsControl control)
{
...
}
Of course, you could derive a new object type from Control to accomplish this, but in my case, I need to take a wide variety of Control types (e.g. different classes derived from specific controls like TreeView etc.
I realise that multiple inheritance would solve this problem. But is there a better way to deal with it in C# other than casting and trusting the programmer?
Tom Clement
|
|
|
|
|
The only way I can see is:
public interface IMyNotificationsControl
{
...
}
public void MyFunction(IMyNotificationsControl control)
{
if( ! ( control is System.Windows.Forms.Control ) )
{
throw new Exception( "Must be of type System.Windows.Forms.Control" );
}
...
}
or
public void MyFunction(System.Windows.Forms.Control control)
{
if( ! ( control is IMyNotificationsControl ) )
{
throw new Exception( "Must implement IMyNotificationsControl" );
}
...
}
sorry, i dont think there is any other way.
|
|
|
|
|
Unless you create a new abstract class that derives from Control and implements the interface (stubbing out the interface methods as abstract methods), you can't automatically get what you want.
You say that you have to handle a wide variety of controls including TreeView, so you will be dealing with Controls that don't implement the interface, correct?
If this is the case, your method should do something like this:
public void MyFunction(Control control)
{
IMyNotificationsControl imnc = control as IMyNotificationsControl;
if (imnc != null)
{
}
}
I, for one, do not think the problem was that the band was down. I think that the problem may have been that there was a Stonehenge monument on the stage that was in danger of being crushed by a dwarf.
-David St. Hubbins
|
|
|
|
|
Thanks for the reply Kentamanos (and krisp). Both solutions are reasonable. I actually used krisps, since I had to fail if the control did not implement the interface. The discussion is really a theoretical one.
My point was that I couldn't just derive from Control and include the interface methods, since, for example, I'd need to handle controls derived from both (e.g.) ListView and TreeView. Since I can't insert my new control in the object hierarchy above these two controls, given the single inheritance nature of things in C# I couldn't see the kind of solution I'd really like, which is the ability to declare a Type consisting of a class plus an interface.
I guess there could be a restricted interface construct that would provide something more than what C# currently provides but that was less then full multiple interitance. This would be something like:
interface IMyInterface RestrictedTo <objecttype1>, <objecttype2>
Oh well...
Tom
|
|
|
|