Re: InterNetwork communication

Tech-Archive recommends: Fix windows errors by optimizing your registry



Benny wrote:
I am rather new to network programming and need a kickstart. I am looking for the same idea as the dos prompt or unix, but remote. I want a client app to be able to send a command to the server and the server respond with an action. I realize i need to use sockets and TcpListner, but my main question: how do i keep the connection alive and listening for any commands until the remote user disconnects? The code i have seen so far just connects sends then disconnects without staying alive. Thanks in advance!

The simple answer is not close the socket at either the client or server until you need to. Your server probably waits for connection like this:


TcpListener listener = new TcpListener(IPAddress.Any, port);

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

while(!Stopping)
{
	TcpClient client = listener.AcceptTcpClient();
	...
	...
}

Once you have the TcpClient object from the listener start a separate thread that will process it. In that thread just keep going round the loop of reading the data from the stream and writing the response. Only close the TcpClient object when the listener receives a command like "quit".

The loop might be something like this:

stream = client.GetStream();

Int32 i;

sb = new StringBuilder();
while (!sb.toString().equals("quit")) {
// Loop to receive all the data sent by the client.
while((i = stream.Read(bytes, 0, bytes.Length))!=0)
{
// Translate data bytes to a ASCII string.
data = System.Text.Encoding.ASCII.GetString(bytes, 0, i); sb.Append(data);
}
stream.Write("recieved");
stream.Flush();
}


Your client app will the work on a similar principal of reading and writing to the socket until it sends the the "quit" command, at which point it can close the socket.

Hope this helps.
Jimbo
.



Relevant Pages

  • Re: GROUP BY and Aggregating Data
    ... > What command or query will take the data above and return the following? ... I suggest doing this in the client app, not in the database. ...
    (microsoft.public.sqlserver.programming)
  • Re: SSH question
    ... > client application, reattaching to that session in a new client app ... You probably want the "screen" command. ...
    (Fedora)
  • Re: Is my VB6 app form open?
    ... Public Function IsFormLoadedAs Boolean ... Dim i As Long ... |> On a remote pc that is... ... |> determine if the main form is open when a client app is opened. ...
    (microsoft.public.vb.general.discussion)
  • Re: Is .Net remoting possible from VB6 apps
    ... it is possible to call a .Net remoting server from VB6 ... > VB6 client App and the .Net Remoting Client App. ... >> Remote component ...
    (microsoft.public.dotnet.framework.remoting)
  • Re: Async Socket IO Question
    ... I think you're mostly bumping into Socket Timeout issues. ... If you kill the process of your client app, the TCP session isn't cleanly ... When your server sends to the client app, that send happens just fine (the ...
    (microsoft.public.dotnet.framework)