ATTENTION: You are viewing a page formatted for mobile devices; to view the full web page, click HERE.

Other Software > Developer's Corner

Need help tweaking C# server code


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...");

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,



[0] Message Index

Go to full version