|
shyagam wrote: Sorry the code isn't tabified.
This editor destroys it.
Use <pre> tags.
shyagam wrote: Michel Prévost wrote:
One problem I can see is that you don't close the Accept'ing socket, and you lose any reference to it (unless there is a hidden Socket variable also holding a reference to this socket).
I didn't quite get that... Could you please explain it more detailed please?
How and where should I close it?
When done with a Socket, you must close it:
aSocket.Shutdown(SocketShutdown.Both);
aSocket.Close();
If you don't do that, the socket still holds the resources. Also, I noticed that you have an empty catch , this is very bad coding practice. You probably have an exception that would certainly help you debug your problem.
As of why it works if both apps are local and doesn't when on different machines, that is probably because of the different way windows handles TCP/IP communications when both apps are on the same host.
--------
"I say no to drugs, but they don't listen."
- Marilyn Manson
-- modified at 9:02 Friday 17th February, 2006
|
|
|
|
|
Well... Let me show you another piece of my code...
private void performDisconnect(bool sendDisconnectionPack)
{
if (sck != null)
{
if (sendDisconnectionPack)
{
sendPack(CMD_DISCONNECT);
disconnectWait = new EventWaitHandle(false, EventResetMode.ManualReset);
disconnectWait.WaitOne(5000, true);
disconnectWait.Close();
disconnectWait = null;
}
if (sck.Connected) sck.Shutdown(SocketShutdown.Both);
sck.Close();
sck = null;
GC.Collect();
}
}
Hope it helps...
Hmm... BTW... What do you suggest to do with this:
sck = sck.EndAccept(result);
-- modified at 14:16 Friday 17th February, 2006
|
|
|
|
|
shyagam wrote: Hmm... BTW... What do you suggest to do with this:
sck = sck.EndAccept(result);
if you want to end accepting connections (maybe to resume later):
Socket tempSck = sck.EndAccept(result);
sck.Shutdown(SocketShutdown.Both);
sck.Close();
sck = tempSck;
Do you have the code for sendPack()?
--------
"I say no to drugs, but they don't listen."
- Marilyn Manson
|
|
|
|
|
Well... It's a function I wrote...
Its purpose is creating a pack according to 2 parameters, and asynchronously send it.
I've taken out almost everything
It's a bit long, and you don't have to see the entire function to get the idea...
private void sendPack(byte command, byte param)
{
byte[] data, pack;
data = makePack(command, param);
sck.BeginSend(pack, 0, pack.Length, SocketFlags.None, new AsyncCallback(sendCallBack), null);
}
Here is the CallBack:
private void sendCallBack(IAsyncResult result)
{
try
{ sck.EndSend(result); }
catch(Exception ex)
{ this.Invoke(new stringDelegate(setStatus), ex.Message); }
}
lolz...
Looks like I WILL end up posting all the code :P
|
|
|
|
|
I forgot you wanted to see the code where my BeginAccept is...
Well... Here it is:
private void cmdListen_Click(object sender, EventArgs e)
{
if (cmdListen.Text == "Listen")
{
try
{
IPEndPoint ipEP = new IPEndPoint
(IPAddress.Any, int.Parse(txtPort.Text));
if (sck == null)
sck = new Socket(ipEP.Address.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
sck.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
sck.Bind(ipEP);
sck.Listen(100);
sck.BeginAccept(new AsyncCallback(this.acceptCallBack), null);
listening();
}
catch (Exception ex)
{
setStatus(ex.Message);
this.Invoke(new booleanDelegate(performDisconnect), false);
}
}
else performDisconnect(false);
}
I can't remember though, And why I set the Listen() method's backlog parameter to 100, and why I wrote this line:
sck.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
Guess I'll end up posting the entire code here.
-- modified at 14:36 Friday 17th February, 2006
|
|
|
|
|
I've added the ability to access a word document using COM. However when I go to run this on another machine I get an error saying that my object is not set to an instace of an object. How come this would work fine on one machine (my development machine) and not on the other (testing)....here is my code
<br />
if (File.Exists("c:\\PO.dot"))<br />
{<br />
File.Copy("c:\\PO.dot", "c:\\PO1666.doc");<br />
oWord.Visible = true;<br />
object oFile = "c:\\PO1666.doc";<br />
oDocs = oWord.Documents;<br />
oDoc = (Word.Document) oDocs[1];<br />
oDoc = (Word.Document) oDocs.Open(ref oFile, ref oMissing,<br />
ref oMissing, ref oMissing, ref oMissing, ref oMissing,<br />
ref oMissing, ref oMissing, ref oMissing, ref oMissing,<br />
ref oMissing, ref oMissing, ref oMissing, ref oMissing,<br />
ref oMissing, ref oMissing);<br />
<br />
The error comes up when I open the document.
Thanks
Tom Wright
tawright915@yahoo.com
|
|
|
|
|
never mind. Didn't see the openold method.
Tom Wright
tawright915@yahoo.com
|
|
|
|
|
|
I have a network application that is reading an incomming message. The message should be UTF-8 formatted and could be transfered with garbage. I am currently using the StreamReader and BinReader to read from the stream, but I am unsure if I am using these objects properly.
When I use the StreamReader to ReadToEnd() will it stop when it has reached an incomplete character?
Will it leave the stream position after these incomplete characters, or will it go past them and just not include them in the results?
Assert(this);
|
|
|
|
|
This is from MSDN:
ReadToEnd works best when you need to read all the input from the current position to the end of the stream. If more control is needed over how many characters are read from the stream, use the Read(Char[],Int32,Int32) method overload, which generally results in better performance.
ReadToEnd assumes that the stream knows when it has reached an end. For interactive protocols, in which the server sends data only when you ask for it and does not close the connection, ReadToEnd might block indefinitely and should be avoided.
Note than when using the Read method, it is more efficient to use a buffer that is the same size as the internal buffer of the stream. If the size of the buffer was unspecified when the stream was constructed, its default size is 4 kilobytes (4096 bytes).
modified 12-Sep-18 21:01pm.
|
|
|
|
|
Hmm, I'll try changing it to Read. I'm guessing that an incomplete character could cause a block, I guess I'll just have to waste the memory in triple allocation (stream, array, string). It's kind of hard to specify a buffer that is the same size as the stream though since with multibyte I won't know the number of chars that are actually in the stream until it's decoded.
Assert(this);
|
|
|
|
|
Is there anyway of specifying the order that properties will appear in a property grid? I know how to create property categories but i wish to order the properties in the category.
Cheers
Mark.
|
|
|
|
|
I think that there is no way to define order of properties in the property control. It sorts properties alphabetically in each category.
Look at MSDN Property Control
|
|
|
|
|
Hello,
I would like to convert my windows forms application from C# to C++. Can this be done without rewriting the whole application? Oh, and I would not like to use MFC just plain API if possible. Any tools avaiable?
The reasons are:
C# is too slow.
C# wastes too much memory.
Before somebody complains: This can't be true! The application runs on an embedded device with a Micky Mouse processor and increadable 32 Megabyte of avaiable RAM. C# is just not good enough for this kind of maschines
The garbage collector sucks and does not rock. But I need all avaiable
Bytes for data. Every possible byte.
Operating system is windows ce (PocketPC)
Chris
P.S.: It was not my idea to use .net in the first place. I planned to use C++ with API. But the project manager insisted on .net Maybe he red too much codeproject
-- modified at 11:26 Thursday 16th February, 2006
|
|
|
|
|
There are converters that do a C# to C++/CLI conversion for you. But to convert a C# app to a Win32 API app, it's not syntax conversion that's needed - you are essentially writing a totally different app targeting a totally different environment. The only converter available would be a human coder.
Regards,
Nish
|
|
|
|
|
Like Nish said, you'd be better off rewriting the app.
Jeremy Falcon
|
|
|
|
|
dl4gbe wrote: But the project manager insisted on .net Maybe he red too much codeproject
Tsk. Tsk. Managers choosing technologies
--------
"I say no to drugs, but they don't listen."
- Marilyn Manson
|
|
|
|
|
Michael said; "Tsk. Tsk. Managers choosing technologies"
They have a nasty habit of doing that.
I'm on-line therefore I am.
JimmyRopes
|
|
|
|
|
|
|
At my last company after writing over 100,000 lines of test and prototype code in .NET, I recommended we switch back to C++/MFC. C# was nice, I loved working in it, but for what we were doing, I felt it just isn't ready for "thick" client apps.
You will have to rewrite, and would want to anyway in order to optimize the app for Win32 CE, but it's not difficult.
Anyone who thinks he has a better idea of what's good for people than people do is a swine.
- P.J. O'Rourke
|
|
|
|
|
Joe Woodbury wrote: I loved working in it, but for what we were doing, I felt it just isn't ready for "thick" client apps.
Can you elaborate. I'm always interested to know where others have hit problems with writing thick clients with C#, so that I can avoid the same pitfalls.
thanks,
Michael
CP Blog [^] Development Blog [^]
|
|
|
|
|
How can i tell if a remote pc is connected to the network using c#.
-- modified at 10:23 Thursday 16th February, 2006
|
|
|
|
|
you could ping it via c# and see if you get a reply. You could enumerate the network and get a list of all the machines names.
Tom Wright
tawright915@yahoo.com
|
|
|
|
|
Hi,
thanks for the response. i have tried to ping using c# but this takes too long. what i am trying to do is get all of the machines in the active directory on my network and then check to see if each one is connected. i then load a listview item into a listview with the relevant image. this is for a network admin tool.
Thanks again for any help on this.
|
|
|
|