I have quoted a C# source code file that compiles fine. It runs a server on my local machine and listens for a connection. As the screenshot shows, I can connect to it successfully via telnet. However, the server is responding to each individual character I type in telnet, rather than waiting until I type a word or sentence and press Enter while in telnet. I know this has to be an easy fix, but I'm foggy right now.
The behaviors I'm looking for is for the server to receive multi-word phrases and echo them to the console. See source code and also the screenshot. I want to be able to type "quit" from telnet and have the server respond to that word, rather than each character.
using System;
using System.Reflection;
using System.IO;
using com.wms.strings;
using com.wms.io;
using System.Text;
using System.Net;
using System.Net.Sockets;
//https://codeabout.wordpress.com/2011/03/06/building-a-simple-server-client-application-using-c/
namespace MyProgramNamespace{
class Program{
static void Main(){
//insert code here...
string sAppPath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().GetName().CodeBase);
string sExeName = Path.GetFileName(Assembly.GetExecutingAssembly().GetName().CodeBase);
sAppPath = sAppPath.Substring(6, sAppPath.Length - 6);
string sFullPathToExe = sAppPath + "\\" + sExeName;
MyConsole.Entitle("Bryan Miller", "kyrathasoft@gmail.com", "ls44.exe");
Console.WriteLine(" Trying to initialize server...");
try{
IPAddress ipAdress = IPAddress.Parse("127.0.0.1");
TcpListener myList = new TcpListener(ipAdress,8000);
myList.Start();
Console.WriteLine("Server running - Port: 8000");
Console.WriteLine("Local end point:" + myList.LocalEndpoint );
Console.WriteLine("Waiting for connections...");
Socket s = myList.AcceptSocket();
// When accepted
Console.WriteLine("Connection accepted from " + s.RemoteEndPoint);
bool condition = true;
while(condition){
byte[] b = new byte[100];
int k = s.Receive(b);
Console.WriteLine("Received...");
string sMsgFromClient = string.Empty;
for (int i=0;i<k;i++)
{
sMsgFromClient += Convert.ToChar(b[i]).ToString();
}
Console.WriteLine(" Client sent: {0}", sMsgFromClient);
if(sMsgFromClient.Trim().ToUpper() == "QUIT"){ condition = false; }
ASCIIEncoding asen = new ASCIIEncoding();
s.Send(asen.GetBytes("Automatic message: " + "String received by server!"));
Console.WriteLine("\n Automatic message sent!");
}
s.Close();
myList.Stop();
}catch(Exception e){
MyConsole.WriteLine("Exception: " + e.Message );
}
MyConsole.Write("To exit ls44.exe, press any key...");
Console.ReadLine();
Console.WriteLine();
}
}
}