|
rareseu wrote: i've checked the web and msdn but most exmpales are either too convoluted or use different mechanics than mine
Maybe your mechanism isn't right then? .
1. The simplest way is creating a thread that will call accept in a loop. For each accepted thread you create new client thread, pass new socket there and voila. Drawback is that you will use 1 socket per connection, so managing a lot of sockets could be expensive. (btw, for each client socket, you can use synchronous operations now).
2. A bit more complicated, but more robust solution is to use asynchrounous communication. Then, foreach connected client, you create him a read buffer, write buffer, supply callbacks methods and just invoke BeginRead/Write. Your callbacks methods will be ran automatically when some data will arive (read) or was sent (write). In callback methods, what you should do is call EndRead/Write to get data, do some quick processing (fe. copy received data to some external buffer) and invoke Begin methods once again, and so on, and so on .
|
|
|
|
|
Thanks for the suggestions Ravadre I've got a question about the 2nd method and in fat this is the main thing i don't understand about working with sockets : how can i tell who i'm talking to ? (client wise) I mean if i accept a connection how do i know who's trying to connect to me ?
|
|
|
|
|
I'm not sure if I understand your question correctly. If you meant that you don't know who has just connected to your server - well, you don't , You can get his IP, or authentificate him using some sort of your own protocol. But if you would have meant that, then this is general issue, not only related to 2nd method, so maybe you've meant, how to know which client has sent you some data, which invoked your callback method, well, there are 2 ways to keep this information; when you Invoke BeginRead() you can pass your own parameter, you can pass some sort of structure that will tell you what you need to know, fe.:
<br />
tcpClient.GetStream().BeginRead(buffer, 0, bufferSize, callback, this)<br />
then, in callback:
<br />
TcpClient myClient = (TcpClient)result.AsyncState;<br />
The second approach (which I use) is to create some sort of wrapper class that handles everything, a little example (a bit simplified):
public class Program
{
class NetClient
{
const int bufferSize = 4096;
TcpClient client;
byte[] writeBuffer;
byte[] readBuffer;
public NetClient(TcpClient client)
{
this.client = client;
writeBuffer = new byte[bufferSize];
readBuffer = new byte[bufferSize];
client.GetStream().BeginRead(readBuffer, 0, bufferSize, OnDataRead, null);
}
void OnDataRead(IAsyncResult result)
{
int dataRead = client.GetStream().EndRead(result);
client.GetStream().BeginRead(readBuffer, 0, bufferSize, OnDataRead, client);
}
}
public static void Main()
{
List<NetClient> clients = new List<NetClient>();
TcpListener server = new TcpListener(11111);
server.Start();
while (true)
{
TcpClient client = server.AcceptTcpClient();
Console.WriteLine((client.Client.RemoteEndPoint as IPEndPoint).Address);
clients.Add(new NetClient(client));
}
}
}
Alternatively, you could make OnDataRead static, and pass NetClient as a parameter, just like in 1st approach.
|
|
|
|
|
Thanks for taking the time to explain Ravadre , i really appreciate it, i'm gonna need some research time to understand the classes and methods you're using. I guess the reason i'm being a little vague with my questions is because i'm not quite sure what i need to make this work, this is the first time i'm working with sockets under c# ( done it once before in c under linux )
|
|
|
|
|
Hi Peoples,
i need to check that in a folder a file type of xml ,
that is *.xml
for a single file we use to check like
normally we do like,
folderpath
System.IO.File.Exists(D:\CodeProject\CodeProject.xml)
this will work, but i need to check all the xml file in that folder,
how to do that, can any one help me?
|
|
|
|
|
|
thanks mr jimmanuvel,
actually i need to delete all the xml file in a directory if exists..
ur code will give me the xml directoris in that directory..
so, i have to loop through or is there anyoption for deleting all the xml files in a directory at one stretch??
|
|
|
|
|
just loop through the files. .Net doesn't have a built in function for everything
|
|
|
|
|
|
|
Dear All,
I developed an application and I would like to install it on one computer in a network and want other users in the network to run this application without need to install this app. in all machines.
and for your info:
1. My application requires other application called (Exceed) need to be installed in all machines in order to run my application. (this is out of scope. this application must be installed in all machines).
2. this (Exceed) application stores its path location in a user environment variable in a user machine.
3. my application need to read this Env. variable to locate the (Exceed) application in user machines.
and my goal for that is to make it easy if I would like to release a new version of my application so all what I need to do is to re-install this version in that computer without need to re-install it in all machines in the network.
so is there a way to do that?
I hope this explaination is clear ... and I apprieciate your helps
thanks,
|
|
|
|
|
First up, your post is as clear as mud. But luckily I had one over the eight for lunch so understand you.
I'd suggest a slightly different approach. Unless you actually NEED to run your app over the network then DON'T.
Write a very simple bootstrap. All it does is start up and check the installed version against the server, if they're different then it installs the new version. This gets around your problem of needing to propagate new versions manually.
Panic, Chaos, Destruction.
My work here is done.
|
|
|
|
|
It's possible to do, but not very nice. Managed code running from a non-local (i.e.: Network) source is not trusted by default. You'll have to go to each machine (or do through policy if your on Active Directory) and configure the .NET Framework to trust the source location of the code, or trust any code that is signed with a certain certificate that you provide. Read up on Code Access Security for all the details (and there is a lot of it!).
|
|
|
|
|
Hi,
am creating appconfig file dynamically to my application folder.
it is working fine but while am uninstalling my application, everything unstalled but that appconfig not uninstalled why this is happening?
how can i uninstall appconfig also?
Thanks InAdvance
|
|
|
|
|
The logic seems to be clear, uninstall removes all it knows about, if you create the config file manually how is the uninstaller gong to know about it? Try including an empty config file in the project and edit it, I'm betting the uninstall will find it then.
Never underestimate the power of human stupidity
RAH
|
|
|
|
|
yes, you are correct.
cant we delete that app.config file during uninstallation?
|
|
|
|
|
HOW CAN WORK WITH MULTI PROCESS?
|
|
|
|
|
what is your problem exactly?
|
|
|
|
|
By switching between the processes. Why are you shouting, and what are you trying to achieve?
I are troll
|
|
|
|
|
Hy guys ! i've been trying to write up classes for a server and a client using sockets, i'm having a bit of a problem with the server : whenever i try to read incoming messages or send messages i get "Object reference not set to an instance of an object" error, this is because my streamWriter/Reader is null. My problem is that i can't figure out why they are null!? when i instance them there are no problems, but when i invoke their mothods they're null , does anybody have any ideeas ?
some code snippets might help to clear this up :
the declarations :
class Server
{
private TcpListener listner;
private Socket socketForClients;
private System.IO.StreamReader messageReader;
private System.IO.StreamWriter messageWriter;
the instantianion :
public void startSever()
{
listner.Start();
socketForClients = listner.AcceptSocket();
if (socketPentruClienti.Connected == true)
{
try
{
NetworkStream networkStream = new NetworkStream(socketForClients);
System.IO.StreamWriter messageWriter = new System.IO.StreamWriter(networkStream);
System.IO.StreamReader messageReader = new System.IO.StreamReader(networkStream);
}
catch (Exception eNetworkStream)
{
System.Windows.Forms.MessageBox.Show("EROARE : eroare la network stream\nEXCEPTIE : "+eNetworkStream.Message);
}
}
}
the invoking :
public void write(string message)
{
try
{
messageWriter.WriteLine(message);
messageWriter.Flush();
}
catch (Exception eScriere)
{
System.Windows.Forms.MessageBox.Show("EROARE : serverul nu a putut trimite mesaj\nEXCEPTIE : " + eScriere.Message);
return;
}
}
PS : some of the text is in romanian but it's nothing important
|
|
|
|
|
rareseu wrote: try
{
NetworkStream networkStream = new NetworkStream(socketForClients);
System.IO.StreamWriter messageWriter = new System.IO.StreamWriter(networkStream);
System.IO.StreamReader messageReader = new System.IO.StreamReader(networkStream);
}
catch (Exception eNetworkStream)
{
System.Windows.Forms.MessageBox.Show("EROARE : eroare la network stream\nEXCEPTIE : "+eNetworkStream.Message);
}// try/catch
This is where your problem is likely to be.
By declaring and instantiating the objects inside a code block (the try/catch block in this case) they are not visible to code outside of the block.
You have already declared the streams outside the block, so you are declaring new ones inside the block.
Just remove the bits, from inside the try block, struck out below and all should be well:
System.IO.StreamWriter messageWriter =
Henry Minute
Do not read medical books! You could die of a misprint. - Mark Twain
Girl: (staring) "Why do you need an icy cucumber?"
“I want to report a fraud. The government is lying to us all.”
|
|
|
|
|
Ah can't believe i missed that , thanks Henry ! i've got new problem now but i'l start a new thread if i can't figure it out
|
|
|
|
|
I want to add some Label and Button controls on a winForm with the code(not by dragging the label and button from toolbox) in the design environment(because there are many labels and buttons need to be created ),I created a component which has a right-click menu under the design environment,when selecting one item of the menu it shows a window, and there is one button in the shown window,Now I want to add the label and button into winform that the component is located after clicling this button.
Could you give some codes to domenstrate to create one button on the form by this way?
Thanks in advance!
|
|
|
|
|
Have a look in your form's designer.cs file and see how the IDE does it and replicate that in your code.
Something like:
Button button = new Button();
button.Text = "&New Button";
button.Location = new Point(12, 12);
this.Controls.Add(button);
DaveBTW, in software, hope and pray is not a viable strategy. (Luc Pattyn) Visual Basic is not used by normal people so we're not covering it here. (Uncyclopedia) Why are you using VB6? Do you hate yourself? (Christian Graus)
|
|
|
|
|
Dave,thank for your help.
I know if I write the code as you suggested and euecute it under the running time, It can create one button, but in the design time, If I execute the same code by running the button in the popup winform, it can not create button in the design winform. Could you give some advice? Thanks!
|
|
|
|
|