|
No, you'd need more than 1 TCP socket on port 1000 (you can use a socket across multiple threads, but you shouldn't Listen() on multiple threads with the same socket) which is clearly illegal.
However, you could handle the request on an other thread if you're not already doing that.
|
|
|
|
|
thanks.. but "handle the request on an other thread" how can i do that is ti like statring a new thread when any new connection is made to the server
|
|
|
|
|
There are several ways, starting a new thread wouldn't be the best of them (too much overhead), but you could use the threadpool
Or, you could let the current thread handle the request but Before that, give the socket to an other thread to listen for more connections.
As long as the listening for more connections doesn't happen on the same thread as the handling of the existing connections doesn't happen on the same thread.
I'd probably loop the listener on its own thread and add the handling to the threadpool queue.
|
|
|
|
|
Hey Max,
Harold is dead on - you cannot run multiple listeners you MUST have only one listener, however you can then run separate threads to actually convert the socket into a connection...
I eventually used: http://www.codeproject.com/KB/IP/Generic_TCP_IP_server.aspx?fid=431256&select=2489157&fr=26#xx0xx[^]
The code is easy to understand and very powerful - i've added encryption/authentication as i went through the code - took a couple of days and ive been trying to get a solid TCP stack working with async methods for a few weeks!
However i did eventually used the async method for the listening socket, everything else is similar to the code in the link
On the 'new connection' i pass the async socket result to a connection event in a separate thread - as the code above looks at - minimising the time taken to process new socket before your listener can go back and wait for the next connection.
HAving said all that i haven't load tested it just yet
Good luck, C
|
|
|
|
|
Every time accept() returns successfully you will have a new client. You should start a new thread as pass the client socket to that thread so it can process indepently of the main thread.
incidentally the only thing your main thread should do is open the socket, accept new connections, dispatch new threads, and look for shutdown.
Move your action to worker threads and you will see a notable improvement in perfomance. However be advised you will need to take care of critical sections, and thread synchronization.
|
|
|
|
|
|
The AcceptSocket method will return a TcpSocket object moved to a different port so you can begin the accept cycle again. No code has to be written to change the port. You can then queue the Socket objects and parse them in a separate thread. Or you can use Asynchronous sockets. In no case will you need more than two threads to handle the connections.
Read the documentation here, http://msdn.microsoft.com/en-us/library/system.net.sockets.tcplistener.acceptsocket.aspx[^] and to verify that the ports are indeed automatically assigned for you run the command line method netstat.
|
|
|
|
|
In C#, using WMI has been great for access SI data. For reasons still unbeknownst to me, I cannot get a ManagementBaseObject instance (lets's call it mob) to properly invoke any of these function using InvokeMethod.
For example:
mof = "Rename";
name = "NEWNAME";
password = "adminpw";
user = "administrator";
object[] args = new args[3];
args[0] = name;
args[1] = password;
args[1] = user;
ManagementBaseObject mob = new ManagementBaseObject();
long retval = (long) mob.InvokeMethod(mof, name, password, user);
if (retval == 0)
Console.Writeline("Reboot PC for RENAME to take effect");
else
Console.Writeline("RENAME error {0}");
Everytime I get Invalid Method Parameter(s) message.
If anyone knows how to succcessfully call Rename, JoinDomainOrWorkgroup, etc from C#, please share the technique.
Thanks
|
|
|
|
|
There is a sample for InvokeMethod() here[^] and also here.[^] Frankly, I don't know how you managed to get to Invalid Method Parameter(s) with your code.
In January you said "Money in April" -
That was two years ago!
B. Python
|
|
|
|
|
The examples on MSDN are for Win32_Process and they work when I test. For some reason, Win32_ComputerSystem is behaving differently. I think there must a different way of calling the Win32_ComputerSystem functions. Here is the exact code that yields this result every time!
Exception caught!!!
System.Management: Invalid method Parameter(s)
public bool RenameComputer(string name, string password, string user)
{
bool ok = true;
string mofName = "Rename";
try
{
ManagementClass processClass = new ManagementClass("Win32_ComputerSystem");
object[] methodArgs = { name, password, user};
for (int i = 0; i < methodArgs.Length; ++i)
Console.WriteLine("methodArgs[{0}] = {1}", i, methodArgs[i]);
object retval = processClass.InvokeMethod(mofName, methodArgs);
Console.WriteLine("InvokeMethod Status {0}, Process ID {1}",
retval, methodArgs[3]);
}
catch (Exception e)
{
Console.WriteLine("Exception caught!!!");
Console.WriteLine("{0}: {1}", e.Source, e.Message);
ok = false;
}
return (ok);
}
Has anyone been sucessful in calling Win32_ComputerSystem InvokeMethod() for Rename, or JoinDomainOrWorkgroup or UnjoinDomainOrWorkgroup. Help is greatly appreciated by me and anyone else dealing with this challenge.
|
|
|
|
|
Win32_ComputerSystem.Rename is an instance method so you first need to get the Win32_ComputerSystem instance to invoke it on:
ManagementClass computerSystemClass =
new ManagementClass("Win32_ComputerSystem");
object[] methodArgs = { name, password, user };
foreach (ManagementObject computerSystem in computerSystemClass.GetInstances())
{
object retval = computerSystem.InvokeMethod(mofName, methodArgs);
}
computerSystemClass.GetInstances() will return only one instance but you still need to use foreach. To avoid it, you could use the ManagementObject constructor that accepts the object path to get the instance:
ManagementObject computerSystem =
new ManagementObject("Win32_ComputerSystem.Name='SomeName'");
but you would have to get the computer name that you want to change first.
In January you said "Money in April" -
That was two years ago!
B. Python
|
|
|
|
|
|
Hi there!
I build C# application and then I add a setup project for the application within the solution. When I am installing the application, everything is going fine, I am starting the application, everything is going fine. When I want to insert some data in the database which is situated within the installation folder, I am getting this error:
a network-related or instance-specific error occured while establishing a connection to SQL server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL server is configured to allow remote connections. [provider: SQL Network Interfaces, error: 26 - Error Locating Server/Instance Specified
Can someone explain me why I am getting this?? I will really appreciate. Thanks in advance, Laziale
|
|
|
|
|
what is the type of database you have in your installation folder?
The error you are getting is because you are trying to connect to an SQL Server instance. In which you need to setup SQL server and have the database part of an instance in that.
Life goes very fast. Tomorrow, today is already yesterday.
|
|
|
|
|
the type of the database is Sql Server Express database. How I can solve that problem, can you give me some hint, I don't have any idea. Thanks in advance, Laziale
|
|
|
|
|
Im not hugely experienced with using it but as far as i known you will need to install SQL Server Express on the target machine, or on a server if its a network application. I think you can download the redistributable for SQL server express. you will then need to include that in your setup application. Then once that is set up you can create your database on it and use it as, i assume, you are using it for testing
Life goes very fast. Tomorrow, today is already yesterday.
|
|
|
|
|
Hi!
The plan is this database to be used at the client environment. It is a management system, so they will need to pull and insert data in their local database. That's why I have doubts how to build this setup, I have SQL Server installed on my machine, I am installing the application, trying to insert some data and the error is occuring. That's what I want to know, why that error is occurring, since I have installed SQL Server on my machine. Thanks for your help, Laziale
|
|
|
|
|
does you application work when you test it before adding the setup?
Can you also post some code in relation to the database connection
Life goes very fast. Tomorrow, today is already yesterday.
|
|
|
|
|
yes, when I am running and debugging within VS, it works fine, I am inserting, selecting, deleting data from database. When I am running like standalone application, it gives me the error what I said in the first post.
Here is some code where I am selecting some data:
SqlConnection conn = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=" + Environment.CurrentDirectory + @"\App_Data\CMS.mdf;Integrated Security=True;User Instance=True;");
conn.Open();
string selectFamilyID = "SELECT Family_Id, Family_Name FROM Families WHERE Family_HouseHold = '" + fullName + "';";
SqlCommand selectCommand = conn.CreateCommand();
selectCommand.CommandText = selectFamilyID;
SqlDataAdapter adapter = new SqlDataAdapter();
adapter.SelectCommand = selectCommand;
DataSet myDataSet = new DataSet();
string dataTableName = "Families";
adapter.Fill(myDataSet, dataTableName);
int familyID = 0;
DataTable table = myDataSet.Tables["Families"];
foreach (DataRow row in table.Rows)
{
familyID = Convert.ToInt32(row["Family_Id"]);
secondName = Convert.ToString(row["Family_Name"]);
}
|
|
|
|
|
hmmm. well i can only suggest that the application is not locating the file as expected.
You might want to try modifying the code so you get a OpenFileDialog prior to the connection. select you database location then use that information in the connection string. once you have done that test i VS, if that works then test with setup. If that also works then it must be something to do with the file path
Life goes very fast. Tomorrow, today is already yesterday.
|
|
|
|
|
The DB-filename isn't quoted. It might not find the file if there are spaces in the path to the database.
I are troll
|
|
|
|
|
how then is finding the file when I am running from visual studio?
|
|
|
|
|
I dunno, but by your reasoning a space can't be the problem
I are troll
|
|
|
|
|
But are you copying the database file to the installation directory. . .??
That means if you are installin it to some C:\Program Files\MyApplication\
then you need to copy the database file to that location. . .
According to me the main problem is in connection string . ..
What you can do is firstly you just give a message box showing the connection string by cn.ConnectionString.
So You can verify that the connection string is proper or not. . .
|
|
|
|
|
Greetings all!
My question:
From the main thread, how can I create two additional threads, each containing an object that will sit around and wait to be invoked? I want to do this because I don’t want the objects to be created and destroyed between invokes.
This is what I would like to do:
Have a main interface and two objects. The main form has 10 things it wants the objects to do, so upon starting it sends an item to each object via BeginInvoke (the objects are controls) to work and as soon as one of the objects is finished with an item, the main thread sends that object the next item to work, until all 10 are done.
Any help you can provide is much appreciated.
Thank you!
|
|
|
|