Click here to Skip to main content
15,896,269 members
Home / Discussions / C#
   

C#

 
AnswerRe: nVidia PhysX & C# ? Pin
Nagy Vilmos1-May-09 4:00
professionalNagy Vilmos1-May-09 4:00 
GeneralRe: nVidia PhysX & C# ? Pin
Mohammad Dayyan1-May-09 4:07
Mohammad Dayyan1-May-09 4:07 
AnswerRe: nVidia PhysX & C# ? Pin
Jimmanuel1-May-09 4:38
Jimmanuel1-May-09 4:38 
GeneralRe: nVidia PhysX & C# ? Pin
Mohammad Dayyan1-May-09 4:51
Mohammad Dayyan1-May-09 4:51 
Questionmanaging multiple socket connections !? Pin
rareseu1-May-09 3:14
rareseu1-May-09 3:14 
AnswerRe: managing multiple socket connections !? Pin
Ravadre1-May-09 3:33
Ravadre1-May-09 3:33 
GeneralRe: managing multiple socket connections !? Pin
rareseu1-May-09 4:23
rareseu1-May-09 4:23 
GeneralRe: managing multiple socket connections !? Pin
Ravadre1-May-09 4:54
Ravadre1-May-09 4:54 
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 Smile | :) , 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.
GeneralRe: managing multiple socket connections !? Pin
rareseu1-May-09 5:19
rareseu1-May-09 5:19 
QuestionHow to check files exists *.xml Pin
Hema Bairavan1-May-09 2:53
Hema Bairavan1-May-09 2:53 
AnswerRe: How to check files exists *.xml Pin
Jimmanuel1-May-09 3:13
Jimmanuel1-May-09 3:13 
GeneralRe: How to check files exists *.xml Pin
Hema Bairavan1-May-09 3:22
Hema Bairavan1-May-09 3:22 
GeneralRe: How to check files exists *.xml Pin
Jimmanuel1-May-09 3:29
Jimmanuel1-May-09 3:29 
GeneralRe: How to check files exists *.xml Pin
Hema Bairavan1-May-09 3:31
Hema Bairavan1-May-09 3:31 
GeneralRe: How to check files exists *.xml Pin
Jimmanuel1-May-09 3:35
Jimmanuel1-May-09 3:35 
Questionrunning an application through network Pin
Genius.Boy1-May-09 2:31
Genius.Boy1-May-09 2:31 
AnswerRe: running an application through network Pin
Nagy Vilmos1-May-09 3:53
professionalNagy Vilmos1-May-09 3:53 
AnswerRe: running an application through network Pin
Dave Kreskowiak1-May-09 4:02
mveDave Kreskowiak1-May-09 4:02 
QuestionProblem with appconfig while uninstalling project Pin
Narendra Reddy Vajrala1-May-09 2:09
Narendra Reddy Vajrala1-May-09 2:09 
AnswerRe: Problem with appconfig while uninstalling project Pin
Mycroft Holmes1-May-09 2:24
professionalMycroft Holmes1-May-09 2:24 
GeneralRe: Problem with appconfig while uninstalling project Pin
Narendra Reddy Vajrala1-May-09 2:40
Narendra Reddy Vajrala1-May-09 2:40 
QuestionMULTI PROCESS Pin
KALAMN1-May-09 2:06
KALAMN1-May-09 2:06 
AnswerRe: MULTI PROCESS Pin
Genius.Boy1-May-09 2:08
Genius.Boy1-May-09 2:08 
AnswerRe: MULTI PROCESS Pin
Eddy Vluggen1-May-09 2:44
professionalEddy Vluggen1-May-09 2:44 
QuestionSocket comunication problem Pin
rareseu1-May-09 0:29
rareseu1-May-09 0:29 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.