|
Hi.
I am making just a simple socket program. Server program is always working. When I open client program and click the button, sending/receiving data normally. Everything's ok. But when I close client and opening again, and then clicking button, client program getting lock. Not getting error. When I debug, its waiting on "readline" line. What is the problem? Why its not working when I reopen client program?
Client
private void Form1_Load(object sender, EventArgs e)
{
try
{
tcp_client = new TcpClient("192.168.1.222", 4444);
}
catch
{
label2.Text = "not connected";
return;
}
ag_akimi = tcp_client.GetStream();
akim_okuyucu = new StreamReader(ag_akimi);
akim_yazici = new StreamWriter(ag_akimi);
}
private void button1_Click(object sender, EventArgs e)
{
try
{
akim_yazici.WriteLine("sample ");
akim_yazici.Flush();
textBox1.Text += akim_okuyucu.ReadLine();
}
catch
{
MessageBox.Show("error!");
}
}
private void form_kapatiliyor(object sender, FormClosingEventArgs e)
{
try
{
akim_okuyucu.Close();
akim_yazici.Close();
ag_akimi.Close();
istemcisoketi.Close();
}
catch
{
MessageBox.Show("Not properly closed");
}
}
}
***********************************************
server
public void Form1_Load(object sender, EventArgs e)
{
CheckForIllegalCrossThreadCalls = false;
thread_dinleyici = new Thread(new ThreadStart(dinle));
thread_dinleyici.Start();
}
public void dinle()
{
tcp_listener = new TcpListener(IPAddress.Any, 4444);
tcp_listener.Start();
istemcisoketi = tcp_listener.AcceptSocket();
ag_akimi = new NetworkStream(istemcisoketi);
if (istemcisoketi.Connected)
{
while (true)
{
akim_yazici = new StreamWriter(ag_akimi);
akim_okuyucu = new StreamReader(ag_akimi);
try
{
richTextBox1.Text += akim_okuyucu.ReadLine();
akim_yazici.WriteLine("message");
akim_yazici.Flush();
}
catch
{
label1.Text = "closing";
istemcisoketi.Close();
ag_akimi.Close();
akim_yazici.Close();
akim_okuyucu.Close();
istemcisoketi.Close();
return;
}
}
}
else
{
}
}
|
|
|
|
|
When you load your server form, it starts a thread which is listening for the Client socket.
When the client connects, all is fine.
When the client completes, you close the socket, and exit the "dinle" method via the catch block.
This terminates the thread, so there is nothing listening for the client any more. It then hangs, waiting for the server.
What you need to do is to restart a new thread when the old one closes. Make sure you have some way of terminating threads cleanly, or you will have problems shutting the server down.
All those who believe in psycho kinesis, raise my hand.
|
|
|
|
|
Thanks for your reply.
But how can I restart a new thread when the old one closes? If I didnt write "return" and close thread, I am getting another problems.
|
|
|
|
|
Probably the easier way to do it with your existing code is to move the listening outside the "dinle" method:
public void dinle()
{
while (!AbortAll)
{
DoThread();
}
}
private void DoThread()
{
tcp_listener = new TcpListener(IPAddress.Any, 4444);
tcp_listener.Start();
...
}
catch
{
...
return;
}
}
}
}
All those who believe in psycho kinesis, raise my hand.
|
|
|
|
|
What is "AbortAll"? Is this a variable or?
|
|
|
|
|
Remember I said you would need someway to stop them? It's just a variable - when you start killing things, you can stop the task with it, once you have closed the connection in your main routine. (I have only ever been a fan of "while(true)" loops in real-time software!)
All those who believe in psycho kinesis, raise my hand.
|
|
|
|
|
I am trying to read text from an external application using GetWindowText. It works just fine for all of the other languages in the software I am trying to read from. (German, English, French) However, it fails when it tries to read in Cyrillic (Russian), Arabic, Japanese. All it gets is a bunch of question marks. Here is the code that I am using.
[DllImport("User32.dll", CharSet = CharSet.Unicode)]
public static extern Int32 GetWindowText(int hWnd, StringBuilder s, int nMaxCount);
Win32.GetWindowText(hWnd, formDetails, 512);
When I display the text using a textbox is is merely a bunch of "?????? ????? ?? ???????".
I know it has nothing to do with the textboxes inability to display those languages because if I directly drop a string of Cyrillic into a variable in C# and display it in the box, it display just fine. What am I doing wrong?
-Elmernite
|
|
|
|
|
Message Closed
modified 23-Nov-14 7:12am.
|
|
|
|
|
I've read it, but that doesn't really help, unless I missed it somewhere.
See as a general rule, my program doesn't really display the other languages (what the article you post was mostly about), I just need to read in the languages of the other software so I know what language that software is currently in.
I'm guessing the GetWindowText or StringBuilder has problems reading in the aforementioned languages. That is what I need help with.
-Elmernite
|
|
|
|
|
I want to create a DataGridView with custom headings. Basically, I want to place a Panel with a number of controls in each heading cell. To do this I have created a new class which inherits DataColumn:
public class GenericDataColumn : DataColumn
{
private int columnIndex;
private string columnName;
private Panel panel;
private DataGridView table;
private Size initialPanelSize;
private Point[] initialControlLocations;
public GenericDataColumn(int columnIndex,
string columnName,
Panel panel,
DataGridView table)
{
this.columnIndex = columnIndex;
this.ColumnName = columnName;
this.table = table;
this.panel = panel;
this.initialPanelSize = panel.Size;
this.initialControlLocations = new Point[panel.Controls.Count];
for (int i = 0; i < panel.Controls.Count; i++)
{
initialControlLocations[i] = panel.Controls[i].Location;
}
}
}
Whenever the table is scrolled or resized or the column is resized I need to resize the Panel and possibly move around the controls inside the Panel. I wrote the following method to do that:
private void cutPanel(int cutFromLeft, int cutFromRight)
{
int newPanelWidth = initialPanelSize.Width;
if (0 < cutFromLeft)
{
newPanelWidth -= cutFromLeft;
}
if (0 < cutFromRight)
{
newPanelWidth -= cutFromRight;
}
panel.Width = newPanelWidth;
if (0 <= cutFromLeft)
{
for (int i = 0; i < panel.Controls.Count; i++)
{
panel.Controls[i].Location = new Point(
initialControlLocations[i].X - cutFromLeft,
initialControlLocations[i].Y);
}
}
}
In the Paint event I can easily determine what the new position and dimension of the Panel should be:
DataGridView.GetCellDisplayRectangle(columnIndex, -1, true);
However, this information alone is not enough to determine the cutFromLeft and cutFromRight parameters in the cutPanel method. What's the best way to determine those parameters? Am I forced to micro-manage all the "geography-related" events (DataGridView scroll, DataGridView resize, and column resize) or is there an easier way to do it?
|
|
|
|
|
Just a wild guess from my side:
How about setting AutoSize property of the Panel to true or to set its Dock property to fill.
50-50-90 rule: Anytime I have a 50-50 chance of getting something right, there's a 90% probability I'll get it wrong...!!
|
|
|
|
|
No, unfortunately that won't work. Whenever a "geographical" event occurs, we are responisble for moving and resizing the Panel according to the new header cell location and size. No auto-property is smart enough to do this.
|
|
|
|
|
I was hoping that Panel would be the Column's control and then this should work when you re-size the column. I guess that is not the case.
Now, IMHO, the only way is to track the resize and do the same for the Panel.
50-50-90 rule: Anytime I have a 50-50 chance of getting something right, there's a 90% probability I'll get it wrong...!!
|
|
|
|
|
Oh, I see, you're suggesting I should add the Panel to the GenericDataColumn instead of the DataGridView. I will try that and see what happens.
|
|
|
|
|
It doesn't seem to be possible to add Panels to a DataColumn. Therefore, I have to add it to the table instead and do Paint-event workaround.
|
|
|
|
|
That is what is coming to my mind at this point of time.
50-50-90 rule: Anytime I have a 50-50 chance of getting something right, there's a 90% probability I'll get it wrong...!!
|
|
|
|
|
If you create a custom control with an expandable property (i.e. it has a "+" in the properties window which expands to show you the individual subfields - similar to the Size property of most controls), how do you control the display order of these sub fields?
I have got the control working, with an expandable property containing minWidth, initialWidth and maxWidth, but in the designer they always display in aphabetical order: initialWidth, maxWidth, minWidth. I know it is possible (Size displays as Width first, then Height) - but google is being unhelpful as to how!
All those who believe in psycho kinesis, raise my hand.
|
|
|
|
|
Hello,
In your ExpandableObjectConverter you have to override the GetProperties method, like this:
public override PropertyDescriptorCollection GetProperties(ITypeDescriptorContext context, object value, Attribute[] attributes)
{
return TypeDescriptor.GetProperties(typeof(YourClass), attributes).Sort(new string[] { "FirstProperty", "SecondProperty", "..." });
}
Hope it helps!
All the best,
Martin
|
|
|
|
|
Martin# wrote: Hope it helps!
Oh yes! Perfect - thank you!
All those who believe in psycho kinesis, raise my hand.
|
|
|
|
|
Glad I could help!
All the best,
Martin
|
|
|
|
|
Hi,
I'm working on an application, in which, if the left mouse button is down, and user tries, I'm assigning a new delegate for Mouse wheel event, which does nothing. after completing the execution of new delegate the old delegate is triggered.
Can anyone explain this to me.
Thank you,
Pavan
|
|
|
|
|
Do you mean something like this?
MouseWheel += new MouseEventHandler(Form1_MouseWheel);
This just means, that you also want to be notified if this event occurs.
Your event handler was just added to a list of delegates from controls which also
want to be notified.
With += you just add and with -= you just remove your delegate from the delegate list.
Greetings
Covean
|
|
|
|
|
Your scroll handler could simply determine whether or not the left button is down and act accordingly.
|
|
|
|
|
Hi, I have XML in the following structure:
<Rooms>
<Room>
<RoomRQId>1</RoomRQId>
<Quantity>1</Quantity>
<NumAdults>3</NumAdults>
</Room>
<Room>
<RoomRQId>2</RoomRQId>
<Quantity>1</Quantity>
<NumAdults>2</NumAdults>
</Room>
</Rooms>
And each time, I get the values assigned as the first occurence of room. Running in debug mode, I can see the second node called within the For each loop, but the values are still assigned as the second occurence. Code below:
XmlNodeList nodeRooms = xmlDoc.SelectNodes("/test:HotelAvail/test:Rooms/test:Room", ns);
foreach (XmlNode objRNode in nodeRooms)
{
intRoomRQId = int.Parse(objRNode["RoomRQId"].InnerText);
strQuantity = objRNode["Quantity"].InnerText;
intNumAdults = int.Parse(objRNode["NumAdults"].InnerText);
}
So IntRoomRQID is set as 1 twice, when it should create a node of 1, and then a node of value 2.
|
|
|
|
|
The poblem is this bit of code:
jamesc69 wrote: foreach (XmlNode objRNode in nodeRooms)
{
intRoomRQId = int.Parse(objRNode["RoomRQId"].InnerText);
strQuantity = objRNode["Quantity"].InnerText;
intNumAdults = int.Parse(objRNode["NumAdults"].InnerText);
}
It will set the 3 variables (intRoomRQId , strQuantity intNumAdults ), move to the next node then, before you have done anything with them, re-set with the values from the next node. The code posted will always end up with the variables set to the value of the last node.
To fix this you either need to do whatever it is you need to do with the variables in the loop, or create an object to store the three variables, and maintain a List of these which can be iterated over later.
As a side note intRoomRQId , strQuantity and intNumAdults are poorly named, you shouldn't include the type in the variable identifier in C#, and fully named is better. This will improve the readability of your code. I suggest roomRequestId (or whatever RQ means), quantity and numberOfAdults
CCC solved so far: 2 (including a Hard One!)
37!?!! - Randall, Clerks
|
|
|
|
|