PDA

Click to See Complete Forum and Search --> : another sockets question


Jeremy Martin
Nov 10th, 2002, 06:43 PM
This is an example that I found.


public static void Main()
{

try
{
// Set the TcpListener on port 23.
Int32 port = 23;
IPAddress localAddr = IPAddress.Parse("127.0.0.1");

// TcpListener server = new TcpListener(port);
TcpListener server = new TcpListener(localAddr, port);

// Start listening for client requests.
server.Start();

// Buffer for reading data
Byte[] bytes = new Byte[256];
String data = null;

// Enter the listening loop.
while(true)
{
Console.Write("Waiting for a connection... ");

// Perform a blocking call to accept requests.
// You could also user server.AcceptSocket() here.
TcpClient client = server.AcceptTcpClient();
Console.WriteLine("Connected!");

data = null;

// Get a stream object for reading and writing
NetworkStream stream = client.GetStream();

Int32 i;

// Loop to receive all the data sent by the client.
while((i = stream.Read(bytes, 0, bytes.Length))!=0)
{


My question is does anyone know how to modify it so that I can listen on a port for more than one connection.

Thanks.
Jeremy

DevGrp
Nov 10th, 2002, 09:57 PM
I'm not too keen on network programming, but wont it listen for all connections to that port?

Jeremy Martin
Nov 12th, 2002, 12:02 AM
I would think so but if you telnet in to this example more than once, it will only return data to the first connection.

Jeremy

Jacob438
Nov 19th, 2002, 03:01 PM
I'm new to network programming, but I'm working on an app of my own. I am using asynchronous client/server socket connections and once a new connection is created, I spin off a new class to handle the client and then go back to listen on the port for a new connection.

Here's an example from MSDN on asynchronous sockets (nonblocking):
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguide/html/cpconusingnon-blockingclientsocket.asp
and that's where I got started with my program, hope that helps out.

Jacob438

MrNorth
Nov 20th, 2002, 09:37 PM
I have spent the past week trying to find out a way to implement an easy way to send data using ftp in my applications, and so far without luck. I did it quite easily in Visual Basic, but then I used Winsock controls that had events for everything... Just code and it worked...

With C# it is much harder I think. But when I read the MSDN article on asynchronous sockets, I realized that I could use the delegetes pretty much the same way as vb uses events for it's controls.

When using ftp, I need 2 sockets, one that listens to a port and one that sends data to port 21. I also need them to operate in two threads due to their asynchronous nature... and that's precicely what async. sockets is all about!!!!!

Now comes the real question... how do I really start? In the MSDN article, the methods where nice and all.. but I wish there where some kind of full example... not just the methods.... :(


best regards
Henrik