|
-
Nov 10th, 2002, 07:43 PM
#1
Thread Starter
Addicted Member
another sockets question
This is an example that I found.
PHP Code:
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
-
Nov 10th, 2002, 10:57 PM
#2
Frenzied Member
I'm not too keen on network programming, but wont it listen for all connections to that port?
Dont gain the world and lose your soul
-
Nov 12th, 2002, 01:02 AM
#3
Thread Starter
Addicted Member
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
-
Nov 19th, 2002, 04:01 PM
#4
Lively Member
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/de...ientsocket.asp
and that's where I got started with my program, hope that helps out.
Jacob438
-
Nov 20th, 2002, 10:37 PM
#5
Frenzied Member
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
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|