avatar image

Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length
  • Wednesday February 21, 2024, 4:59 am
  • Proudly celebrating 15+ years online.
  • Donate now to become a lifetime supporting member of the site and get a non-expiring license key for all of our programs.
  • donate

Author Topic: Need help tweaking C# server code  (Read 2972 times)


  • N.A.N.Y. Organizer
  • Honorary Member
  • Joined in 2006
  • **
  • Posts: 3,200
    • View Profile
    • Donate to Member
Need help tweaking C# server code
« on: September 21, 2018, 05: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.

using System;
using System.Reflection;
using System.IO;
using com.wms.strings;
using System.Text;
using System.Net;
using System.Net.Sockets;


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", "", "ls44.exe");
Console.WriteLine(" Trying to initialize server...");

IPAddress ipAdress = IPAddress.Parse("");
TcpListener myList = new TcpListener(ipAdress,8000);

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;

byte[] b = new byte[100];
int k = s.Receive(b);
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!");


}catch(Exception e){
MyConsole.WriteLine("Exception: " + e.Message );

MyConsole.Write("To exit ls44.exe, press any key...");
« Last Edit: September 21, 2018, 06:18 PM by kyrathaba »


  • Honorary Member
  • Joined in 2018
  • **
  • Posts: 718
    • View Profile
    • Donate to Member
Re: Need help tweaking C# server code
« Reply #1 on: September 21, 2018, 09:49 PM »
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,

« Last Edit: September 21, 2018, 09:55 PM by KodeZwerg »