Sockets

Hi there. Sorry for posting this in the wrong forum, I apologise but hope someone can guide me in the right direction.

I have googled for this endlessly however the examples are poor or they are ok but cannot seem to understand them or simply there is not enough in depth explaination.

I want to be able to create a simple application that sends/recieves data.

I will have a Server app which listens to a connection and acts to whatever it needs to do (read data from that conneciton and so on)

Q is, how do I create a socket listener which will keep running until the client says "close connection" or until the application is terminated?

I have made an example by following some websites on creating such an app however it will only work once - in other words, it does not continuouslykeep listening on that connection.

Surely there must be an EVENT that I can inherit or something which is dedicated to keep listening to a connection and recieve data?

BTW - this is using VS.NET 2003 with .NET framework 1.1

[1270 byte] By [ahmedilyas] at [2007-12-16]
# 1

I'm assuming that you're creating a Stream socket over TCP/IP. If that's the case you can do the following:


void Listen()
{
System.Net.Sockets.TcpListener listener = new TcpListener(6000);

while (true)
{
Socket client = listener.AcceptSocket();
Thread clientThread = new Thread(new ParameterizedThreadStart(ClientThread));
clientThread.Start(client);
}
}

void ClientThread(object param)
{
Socket clientSocket = (Socket)param;

byte[] buffer = new byte[1024];
while (clientSocket.Receive(buffer) > 0)
{
// Do stuff with client socket
}
}


The Listen method loops indefinitely listening for clients. When a client connects, it spawns a separate thread for the client and starts it. All the client communication happens in this thread.

You can modify this to allow only one client at a time.

HTH.

VijayeRaji at 2007-9-9 > top of Msdn Tech,Visual C#,Visual C# General...
# 2
Thanks :) I will try this, much appreciated
ahmedilyas at 2007-9-9 > top of Msdn Tech,Visual C#,Visual C# General...
# 3
The source code on this site has a full client/server implementation using sockets. The socket code is very small:
http://www.vgdotnet.com/articles/real_time_vis.shtml
FrankHileman at 2007-9-9 > top of Msdn Tech,Visual C#,Visual C# General...
# 4
Thanks for that I will check out the link
as for the user who posted the code - ParameterizedThreadStart does not exist in .NET Framework 1.1 (it's v2.0) - is there an alternative using .NET framework 1.1?

Will the client connection still be accepted if they use .NET 1.1 and the Server uses .NET 2.0 Framework?

ahmedilyas at 2007-9-9 > top of Msdn Tech,Visual C#,Visual C# General...
# 5
well i think i got it! it looks pretty good :-D need to test the client version on my smartphone now.

1 Q though... since the server only recieves, is there a way to make it also send data to an existing client connetion?

ahmedilyas at 2007-9-9 > top of Msdn Tech,Visual C#,Visual C# General...
# 6
The socket connection we opened is full-duplex, which means the server and the client can send or receive data thru the same socket (without having to wait for the other).

VijayeRaji at 2007-9-9 > top of Msdn Tech,Visual C#,Visual C# General...
# 7
Thanks :) I will give this a shot.
ahmedilyas at 2007-9-9 > top of Msdn Tech,Visual C#,Visual C# General...
# 8
unfortunatly the parameterized thread is in .NET 2.0 :( so i cannot send and recieve on the server... or can i?

I think I found a way of doing that in .NET 1.1

a Q though - I do not understand how to send data to the client using the new thread created that has been parameterized....

the client in my case is a mobile device (smartphone). It can send data no problem to the server but would also like the server to send data to the mobile device and to enable the mobile device to recieve that data (as well as currently sending data)

ahmedilyas at 2007-9-9 > top of Msdn Tech,Visual C#,Visual C# General...