|
check your server call back function is correct or not..,
i think that is the problem, that's way you are able the receive the data for very first time of each new client.
Rajesh B --> A Poor Workman Blames His Tools <--
|
|
|
|
|
Whats hanging? The server or client application?
Are your listeners and tcp stuff in seperate threads or just running on the UI thread?
Are you using blocking or not blocking methods?
You really need to give us some code to look at here...
|
|
|
|
|
you should not execute blocking calls on the main thread; either use asynchronous methods, or better yet apply proper threading. There are some CP articles on threaded client-server systems.
|
|
|
|
|
Is the listener running in its own thread? It should be.
.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
|
|
|
|
|
This is the complete source code for my server side program... on clicking the button it calls func conn_man.. on clicking it this program hangs...
b4 clicking it.. the program is responding n i can move it resize it n stuff.. but after the button is clicked its not responding.. plz.. tell me how to get it working..
on receiving the connection from client it searches for a free port on the server it self.. when found it sends the client the free port to connect to[ function route does it]..
master_slave is another form which is called on accepting a connection from client
<pre>
namespace master
{
public partial class main_form : Form
{
public int port;
public main_form()
{
InitializeComponent();
activity_win.AppendText("Master On...");
}
public void conn_man()
{
try
{
//activity_win.AppendText("In conn_man...");
IPAddress ipAd = IPAddress.Parse("192.168.1.2");
// use local m/c IP address, and
// use the same in the client
/* Initializes the Listener */
TcpListener listener = new TcpListener(ipAd, 5000);
/* Start Listeneting at the specified port */
listener.Start();
activity_win.AppendText("\r\nThe server is running at port 5000...");
activity_win.AppendText("\r\nThe local End point is :" + listener.LocalEndpoint);
activity_win.AppendText("\r\nWaiting for a connection.....");
Socket s = listener.AcceptSocket();
activity_win.AppendText("\r\nConnection accepted from " + s.RemoteEndPoint);
byte[] b = new byte[100];
int k = s.Receive(b);
activity_win.AppendText("Recieved...");
activity_win.AppendText(ASCIIEncoding.ASCII.GetString(b));
if (route(5000) == 0)
{
activity_win.AppendText("No port free");
ASCIIEncoding asen = new ASCIIEncoding();
s.Send(asen.GetBytes("Busy"));
}
else
{
port = route(5000);
new master_slave(port);
ASCIIEncoding asen = new ASCIIEncoding();
s.Send(asen.GetBytes("Port " + port.ToString()));
}
/* clean up */
s.Close();
}
catch (Exception e)
{
activity_win.AppendText("Error..... " + e.StackTrace);
}
}
public int route(int port)
{
try
{
TcpClient scnr = new TcpClient("localhost", port);
return port;
}
catch (Exception e)
{
activity_win.AppendText(e.ToString());
port += 10;
route(port);
return 0;
}
}
private void Begin_Click(object sender, EventArgs e)
{
conn_man();
}
}
}
</pre>
|
|
|
|
|
The same code is working fine as a console app... i changed a lil bit in route func.. due to an logical problem... rest is same.. its working in console app... Y???
Wats the error or problematic statement in this code...
|
|
|
|
|
Why are you doing everything on the primary thread?
|
|
|
|
|
can u plz tell me... the number of threads n what shud it do?? for my program...
|
|
|
|
|
Dear All
ConfigurationManager.GetSection("Section1") only retrieve the default app.config's section information and it returns a object that can be casted. Now i have a specified configuration that used by my application. I have loaded the specified configuration file into the memory, please see the below code:
this is the specified config file:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<section name="Section1" type="CortexShell.DisplayModeSectionHandler,CortexShell" />
</configSections>
<Section1 DisplayMode="MonitorDisplay" Timeout="300" />
</configuration>
this is the c# code:
Configuration config = ConfigurationManager.OpenExeConfiguration(@"c:\AppState.config");
ExeConfigurationFileMap map = new ExeConfigurationFileMap();
map.ExeConfigFilename = path;
config = ConfigurationManager.OpenMappedExeConfiguration(map, ConfigurationUserLevel.None);
DisplayModeSection _displayMode = (DisplayModeSection)config.GetSection("Section1");
Now the config.GetSection("Section1") only returns the specified configuration.ConfigurationSection object, hence when i compile it, this is the exception: Cannot convert type 'System.Configuration.ConfigurationSection' to 'CortexShell.DisplayModeSection', my question is how can i convert System.Configuration.ConfigurationSection to CortexShell.DisplayModeSection (this is my customised config section in the specified config file)
Thanks very much
|
|
|
|
|
|
Smart Thread Pool[^] supports cancelation of queued items.
Harvey Saayman - South Africa
Software Developer
.Net, C#, SQL
you.suck = (you.Occupation == jobTitles.Programmer && you.Passion != Programming)
1000100 1101111 1100101 1110011 100000 1110100 1101000 1101001 1110011 100000 1101101 1100101 1100001 1101110 100000 1101001 1101101 100000 1100001 100000 1100111 1100101 1100101 1101011 111111
|
|
|
|
|
|
Glad to be of service
|
|
|
|
|
The standard ThreadPool holds threads for you to use; you never get full control over those threads, so there are a lot of managerial operations you can't perform on them, such as changing their priority, or aborting them. What you can do is implement a co-operational abort, say a while loop where the outside changes the expression or variable that is going to be tested.
public void myThreadAction(...) {
while(!threadStop) {
... go on
}
}
The net result most often is the "abort" is not instantaneous, the while loop may (and should) call some blocking method (some input/output operation, or a Thread.Sleep).
BTW: that is also the way you should do it on normal threads, a hard abort is to be avoided as one never knows in what state objects (such as locks, unmanaged resources) will be at the moment of the abort.
|
|
|
|
|
Luc Pattyn wrote: a hard abort is to be avoided as one never knows in what state objects (such as locks, unmanaged resources) will be at the moment of the abort.
Sorry any elaboration on this...? I can't see my calculation threads has any "State" to it... and my guess is for all practical reason I can throw an exception in the thread just to kill it. Am I missing something, Abort vs Interrupt?
dev
|
|
|
|
|
you'll have state whenever some I/O is included, or something gets locked (e.g. a shared results queue), etc. Here is an excellent article on threading: http://www.albahari.com/threading/[^]
My conclusion is: choose the co-operative way whenever you can.
|
|
|
|
|
Hello everyone,
I am designing a simple convertor, that will take an ammount and convert it to a different unit.
the uits that one can convert between are:
moles
millimoles
grams
milligrams
mirolitres
millilitres
The user enters an amout, selects its unit, and then selects a unit to convert to, and the new value is output onto the winform.
Now, should I write individual conversion functions, and maybe use a case to decide what unit we have, and then call the required converter.
Or, should I have an overloaded function and pass something in to let it decide how to convert?
Any other advice would be greatly appreciated.
Thank you
Steve
|
|
|
|
|
Hmmm...
You should probably have an enumeration that contains the supported units and have one method that takes the magnitude and two units.
But you could also provide separate methods and the above method could just be a switching station -- but with Dictionaries, not switch es.
private static Dictionary<FromUnit,Dictionary<ToUnit,delegate>> dic ... ;
dic [ Unit.Grams ] = ... ;
dic [ Unit.Grams ] [ MilliGrams ] = ConvertGramsToMilliGrams ;
...
return ( dic [ FromUnit ] [ ToUnit ] ( Magnitude ) ) ;
Actually, the populating of the Dictionary can be automated -- perhaps create an Attribute to decorate your methods:
[UnitConvertorAttribute ( From , To )]
public double ConvertGramsToMilliGrams ( double Magnitude , Unit From Unit To ) ...
|
|
|
|
|
stephen.darling wrote: Or, should I have an overloaded function and pass something
IMO, Function overload is fine.
Maintaining switch cases has its own overhead, especially for the developer who is going to maintain this code in the future.
The funniest thing about this particular signature is that by the time you realise it doesn't say anything it's too late to stop reading it.
My latest tip/trick
Visit the Hindi forum here.
|
|
|
|
|
Abhinav S wrote: IMO, Function overload is fine.
So what would I pass in to diffenciate the overloaded function?
For example, please consider the following punction
convert(float startValue, float outValue)
And I wanted to convert milliGrams to grams, how would this function know that?
would I need something like...
convert (float from, float to, UNIT_TYPE)
Where unit_type would be an enum, perhaps, to allow the correct units to be used. Or can you think of a better idea?
Thank you
Steve
|
|
|
|
|
Have you looked at applying the Strategy Pattern[^] or here[^].
[corrected links]
I know the language. I've read a book. - _Madmatt
modified on Sunday, October 10, 2010 5:20 PM
|
|
|
|
|
stephen.darling wrote: Now, should I write individual conversion functions, and maybe use a case to decide what unit we have, and then call the required converter.
The function is "always" the same; you take the base, and multiply it with a conversion-factor. The only thing you'd need to do is to match a factor to a unit, relative to it's base.
The only exception to this rule that I could find are temperatures
I are Troll
|
|
|
|
|
What about shoe sizes?
|
|
|
|
|
PIEBALDconsult wrote: What about shoe sizes?
Never considered those, but you're right
I are Troll
|
|
|
|
|
Is there a direct way to access a datatable in a collection directly by its name?
Currently, the only way I know how access a datatable is by using a foreach statement to find the table I wish to update/modify:
DataTableCollection tables = collection.Tables;<br />
<br />
foreach (DataTable tbl in tables)<br />
{<br />
string name = tbl.TableName;<br />
<br />
if (name == "name")<br />
{<br />
DataRow myNewRow;<br />
myNewRow = tbl.NewRow();<br />
<br />
code.....<br />
<br />
tbl.Rows.Add(myNewRow); <br />
<br />
}<br />
}
Thanks for any help
|
|
|
|