DonationCoder.com forum
 
Read-only archive. This is a static copy of the DonationCoder.com forum. Reading and searching work as before; logging in, posting and private messages do not.
Archive of 2026-10-21 · About this archive · Main site

Author Topic: Need help tweaking C# server code  (Read 5,172 times)

kyrathaba

  • N.A.N.Y. Organizer
  • Honorary Member
  • Joined in 2006
  • **
  • Posts: 3,200
    • Participated in NANY Challenge Eight Times
    • Programming School - Deluxe Bonus Medal
    • Programming School - Stage 1 Medal
    • Coding Snacks
    • Military Service Badge - US Army
    • Writer
Need help tweaking C# server code
« on: September 21, 2018, 10:56 PM »
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.

Code
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();
		}
	}
}
Win 10 Pro running on a 3.2 GHz CPU and 12 Gb RAM
__________________________________________

Proofreading Service: http://williammillerservices.com/proofreading

« Last Edit: September 21, 2018, 11:18 PM by kyrathaba »

KodeZwerg

  • Honorary Member
  • Joined in 2018
  • **
  • Posts: 718
    • Participated in NANY Challenge Four Times
    • Coding Snacks
Re: Need help tweaking C# server code
« Reply #1 on: September 22, 2018, 02:49 AM »
Hello kyrathaba,

i am sorry that i can not help direct, since wrong programming language, but by reading your source i see that you are operating with single chars.
Please take a look, watch closely the "Write" and "Reader" function.
I hope this will show you how to deal with strings instead of single chars.



Best regards,

KodeZwerg
« Last Edit: September 22, 2018, 02:55 AM by KodeZwerg »

Original address: https://www.donationcoder.com/forum/index.php?topic=46429.0