Dear board, I'm (trying) to make lil socket app. Its a server app thats supposed to echo back the text I type to it. Problem is, when I telnet to it (i want telnet to be my client) it only accepts one character and spits back only one character.
Any advice?
Oh right, heres the code:
using System;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Text;
namespace servertest2
{
class MyTcpListener
{
public static void Main()
{
TcpListener server = null;
try
{
Int32 port = 654;
string hostnamestring = Dns.GetHostName();
IPAddress hostip = (Dns.Resolve(hostnamestring)).AddressList[0];
server = new TcpListener(hostip, port);
server.Start();
Byte[] bytes = new Byte[256];
String data = null;
while(true)
{
Console.Write("Waiting for a connection...\n ");
TcpClient client = server.AcceptTcpClient();
Console.WriteLine("Connected!");
NetworkStream stream = client.GetStream();
int i;
while((i = stream.Read(bytes, 0, bytes.Length))!=0)
{
data = System.Text.Encoding.ASCII.GetString(bytes, 0, i);
Console.WriteLine("Received: {0}", data);
byte[] msg = System.Text.Encoding.ASCII.GetBytes(data);
stream.Write(msg, 0, msg.Length);
}
client.Close();
}
}
catch(SocketException e)
{
Console.WriteLine("SocketException: {0}", e);
}
finally
{
server.Stop();
}
Console.WriteLine("\n Hit enter to continue...");
Console.Read();
}
}
}