topbanner_forum
  *

avatar image

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

Login with username, password and session length
  • Thursday December 12, 2024, 9:50 pm
  • 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 3520 times)

kyrathaba

  • 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 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();
}
}
}
« Last Edit: September 21, 2018, 06:18 PM by kyrathaba »

KodeZwerg

  • 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,

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