|
hi, I'm using the below code for my client/server application.The question is, it creates a new connection each time it triggered by the timer and the number of sockets in the arraylist(alSockets) increases. What is the ideal way?
if you don't mind, I have another question;
if a client is sending two requests, server has to reply those two requests. If server replies only one instead of two, an exception is thrown. Sometimes I may need to send three requests, how can I prepare the server so that no exception is thrown
public void listenerThread()
{
IPHost = Dns.GetHostEntry(Dns.GetHostName());
TcpListener tcpListener = new TcpListener(IPHost.AddressList[0], 8080);
tcpListener.Start();
while (true)
{
Socket listenerSocket = tcpListener.AcceptSocket();
if (listenerSocket.Connected)
{
lock (this)
{
alSockets.Add(listenerSocket);
}
Thread thdHandler = new Thread(new ThreadStart(handlerThread));
thdHandler.Start();
}
}
}
public void handlerThread()
{
Socket handlerSocket = (Socket)alSockets[alSockets.Count - 1];
NetworkStream networkStream = new NetworkStream(handlerSocket);
ntrStream = new NetworkStream(handlerSocket);
strRead = new StreamReader(ntrStream);
strWrite = new StreamWriter(ntrStream);
string textFromClient = strRead.ReadLine();
if (handlerSocket.Connected)
{
}
}
private void timer1_Tick(object sender, EventArgs e)
{
connectToServer();
}
private void connectToServer()
{
IPHostEntry ipHost = Dns.GetHostEntry(server);
try
{
client = new TcpClient(ipHost.AddressList[0].ToString(), 8080);
ntrStream = client.GetStream();
streamWrite = new StreamWriter(ntrStream);
streamYaz.WriteLine("textToServer");
streamYaz.Flush();
ntrStream.Close();
streamWrite.Close();
}
catch (Exception)
{
MessageBox.Show("No Connection");
}
}
|
|
|
|
|
If you're just using alSockets to get a reference to the latest collection, then you can eliminate that entirely.
Give handlerThread an object parameter, and call it as a ParameterizedThreadStart (Check the overloads for the Thread constructor). That way, you can pass the socket itself as a parameter, and avoid the potential race condition.
teknolog123 wrote: if you don't mind, I have another question;
if a client is sending two requests, server has to reply those two requests. If server replies only one instead of two, an exception is thrown. Sometimes I may need to send three requests, how can I prepare the server so that no exception is thrown
Usually you create some sort of communication protocol that both the server and client adhere to. Just make sure the server replies to each message in turn... Not really sure what the problem is here.
|
|
|
|
|
thanks for your reply,
Ian Shlasko wrote: Not really sure what the problem is here.
to be more specific, Client is sending three messages. Server is prepared for replying those three messages.If sometimes client sends four messages, how can I prepare my server to accept that fourth message without exception error?
I'm sorry if I'm not clear enough
|
|
|
|
|
Well, you're going to have to be clearer... Why does it matter if the client sends three messages, four, or four hundred? The server should receive the messages, respond to it, and wait for the next one.
|
|
|
|
|
Hi Guys. How can I check if a file is being used by another process?
I have tried the following but it doesn't seem to work though.
FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.ReadWrite, FileShare.None)
If I open the .txt file in NotePad and then run the app it opens the file. What I need to do is for my app to throw an exception that tells me that the file being used by another process , or something like that.
Anybody have any idea?
Thanks
Excellence is doing ordinary things extraordinarily well.
|
|
|
|
|
I'm not sure you can. Notepad Opens the file, reads the content, and closes it again. It may never touch the file again, so how is the OS to know the file is "in use"?
If you think about it, most well behaved applications do much the same - file handles are a scarce resource, so you don't hang onto them unless you really must.
Real men don't use instructions. They are only the manufacturers opinion on how to put the thing together.
|
|
|
|
|
There's no way to tell. Notepad doesn't hold the file open while you are viewing it. It's opened, read, and closed before you even see it on screen. Notepad also doesn't block other applications from reading the file at the same time it is, so this code will fail to detect Notepad using the file in all cases.
|
|
|
|
|
Try opening this file in MS Word. It locks the file when you open it.
Then when you open file, test your application.
Don't forget to rate answer, that helped you. It will allow other people find their answers faster.
|
|
|
|
|
Dear friends
I have small clarification
I have c++ Dll which contains a class like these
public class Vector3d {
public:
float x, y, z;
Vector3d()
{
}
}
when i try to access the class by adding reference in C# project I could only create object for Vector3d class
i am unable to access variables (x,y,z)
my Vector3d Metadata looks like these
[NativeCppClass]
[CLSCompliant(false)]
public struct Vector3d
{
}
i could not find the variables in metadata also... what is the mistake i have done???
And what i have to do get the values to access?
Thanks
Joe.I
|
|
|
|
|
That's because you didn't make them public . Unless otherwise specified, all methods and data members are priviate .
.45 ACP - because shooting twice is just silly ----- "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997 ----- "The staggering layers of obscenity in your statement make it a work of art on so many levels." - J. Jystad, 2001
|
|
|
|
|
Dear john,
thanks for reply
Please check the second line in C++ class
|
|
|
|
|
C++ is different than C#. In C++, you only need to specify "public:" once above the members you want to be public, whereas in C# you need to specify "public" in front of each member you want to be public.
|
|
|
|
|
No, they're internal .
/ravi
|
|
|
|
|
I believe you will have to provide set_ and get_ methods in your C++ code to access the data as these are fields.
|
|
|
|
|
Dear peter
Thanks for the reply,
i have not only public variables and also public methods. i could not access anything.
i do not know where is my mistake. is there anything i need to do to access the public method of c++ class?
i just added the dll in my reference, then i specified in the Using namespace.
Thanks
|
|
|
|
|
Is this a managed C++ class? You can't add an unmanaged DLL to .NET like this - you need to either wrap it in a Managed C++ implementation, or you need to p/invoke into it.
|
|
|
|
|
I'm developing a client/server system which uses native sockets for communication between the two.
One irritation I'm having is that once a connection has been established, if one of the processes (client or server) dies or is killed, a SocketException is thrown in the other. Arguably this is correct but I'd much rather have an event fired or a callback or something if the connection breaks - I like to debug things with first chance exceptions turned on (I do know I can filter out SocketExceptions) and it gets in the way.
Does anyone know a way of avoiding these exceptions?
Regards,
Rob Philpott.
|
|
|
|
|
Try/Catch
I know the language. I've read a book. - _Madmatt
|
|
|
|
|
Yeah, sure I can use exception handling. But what I really want is to handle disconnections without it.
Regards,
Rob Philpott.
|
|
|
|
|
Exception handling is the best way to approach it. If you want to, you can post an event in the exception block.
.45 ACP - because shooting twice is just silly ----- "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997 ----- "The staggering layers of obscenity in your statement make it a work of art on so many levels." - J. Jystad, 2001
|
|
|
|
|
Good question. Not that I know of. The only way I've managed in the past is to send a custom SocketClosing message between them to inform the other. This only works if you are in control of both Server and Client code of course and will not handle 'improper' closure of either.
DaveIf this helped, please vote & accept answer!
Binging is like googling, it just feels dirtier.
Please take your VB.NET out of our nice case sensitive forum.(Pete O'Hanlon)
BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
|
|
|
|
|
I have just struck this. Using VS2008
private void button2_Click(object sender, EventArgs e)
{
bool cond1 = this.confirmDeletesToolStripMenuItem.Visible;
bool cond2 = !this.confirmDeletesToolStripMenuItem.Visible;
MessageBox.Show(string.Format("Cond1 = {0} Cond2 = {1}",cond1,cond2));
this.confirmDeletesToolStripMenuItem.Visible = !this.confirmDeletesToolStripMenuItem.Visible;
cond1 = this.confirmDeletesToolStripMenuItem.Visible;
cond2 = !this.confirmDeletesToolStripMenuItem.Visible;
MessageBox.Show(string.Format("Cond1 = {0} Cond2 = {1}", cond1, cond2));
}
The confirmDeletesToolStripMenuItem is not the topmost menuitem.
If I get the value of any top level menu item it returns the correct value i.e True if set to Visible = True;
Whereas the lower menu levels return False even if set to True.
I would have thought that the Visible property is a reflection of whether it should be displayed rather than whether it can be seen?
I can set the Visible property directly eg this.confirmDeletesToolStripMenuItem.Visible = True; but still returns False.
Thanks for any help....
|
|
|
|
|
So, if the item is visible and you then do 'item.Visible = false;' does that make it disappear? And how about after 'Visible' is set to 'false'? If you then call 'item.Visible = true;' does the item actually show up again?
Just to make sure I'm clear, I don't mean "does that return true", I mean does it physically disappear/appear as it should?
That is unlike anything I've ever seen. I have a project that I'm working on in which I toggle the 'Visible' property for several menu and toolstrip items. Each of them works perfectly normal.
|
|
|
|
|
Matt U. wrote: So, if the item is visible and you then do 'item.Visible = false;' does that make it disappear? And how about after 'Visible' is set to 'false'? If you then call 'item.Visible = true;' does the item actually show up again?
Yes when setting the Visible property in this way the item disappears and appears as you would expect.
It just always returns false. Even something like....
item.Visible = true;
bool isVisible = item.Visible;
returns false, despite actually being Visible. Hence the inabilty to Toggle based on it current state.
What I have found is that the top level menu items behave as expected, whereas the lower level, or at least mine on the 2nd and 3rd levels don't seem to behave?
modified on Friday, September 3, 2010 12:59 AM
|
|
|
|
|
This is the correct behaviour. As the parent menu item isn't visible, it is impossible for the child to be visible therefore setting the Visible property has no effect.
You should either:
1. Set the visibility of the higher items first.
2. Use the Enabled property instead. You could iterate over all the items and set Visible based on Enabled .
private void SetMenuItemsVisiblity(MenuStrip menuStrip)
{
foreach (ToolStripMenuItem item in menuStrip.Items)
{
item.Visible = item.Enabled;
if (item.Visible && item.DropDownItems.Count > 0)
SetToolStripMenuItemVisiblity(item);
}
}
private void SetToolStripMenuItemVisiblity(ToolStripMenuItem item)
{
foreach (ToolStripMenuItem subItem in item.DropDownItems)
{
subItem.Visible = subItem.Enabled;
if (subItem.Visible && item.DropDownItems.Count > 0)
SetToolStripMenuItemVisiblity(subItem);
}
}
DaveIf this helped, please vote & accept answer!
Binging is like googling, it just feels dirtier.
Please take your VB.NET out of our nice case sensitive forum.(Pete O'Hanlon)
BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
|
|
|
|