help with TCP server in c#

  • From: Mohammed Adeel <licenced2chill@xxxxxxxxxxx>
  • To: programmingblind@xxxxxxxxxxxxx
  • Date: Thu, 3 Jan 2008 16:26:08 +0000 (GMT)

  Hi,
  I can't get the attached tcp server to work the way I want it too. It picks 
up the first text I send it but after that it won't.
  Thanks for any help,
  Adeel
   
   
  namespace sampleTcpUdpServer2
  {
  using Accessibility;
  using System;
  using System.Net;
  using System.Net.Sockets;
  using System.Threading;
  public class SampleTcpUdpServer2
  {
  private const int sampleTcpPort = 4567;
  public Thread sampleTcpThread;
  public SampleTcpUdpServer2()
  {
  try
  {
  //Starting the TCP Listener thread.
  sampleTcpThread = new Thread(new ThreadStart(StartListen2));
  sampleTcpThread.Start();
  Console.WriteLine("Started Tcp Server's TCP Listener Thread!\n");
  }
  catch (Exception e)
  {
  Console.WriteLine("An TCP Exception has occurred!" + e.ToString());
  sampleTcpThread.Abort();
  }
  }
  public static void Main(String[] argv)
  {
  TTS.Initialize();
  SampleTcpUdpServer2 sts = new SampleTcpUdpServer2();
  }
  public void StartListen2()
  {
  //Create an instance of TcpListener to listen for TCP connection.
  TcpListener tcpListener = new TcpListener(sampleTcpPort);
  try
  {
  while (true)
  {
  tcpListener.Start();
  //Program blocks on Accept() until a client connects.
  Socket soTcp = tcpListener.AcceptSocket();
  Console.WriteLine("SampleClient is connected through TCP.");
  Byte[] received = new Byte[512];
  int bytesReceived = soTcp.Receive(received, received.Length, 0);
  String dataReceived = System.Text.Encoding.ASCII.GetString(received);
  Console.WriteLine(dataReceived);
  //TTS.StopSpeech();
  TTS.Speak( " " + dataReceived, true);
  String returningString = "The Server got your me-ssage through TCP: " +
  dataReceived;
  Byte[] returningByte = 
System.Text.Encoding.ASCII.GetBytes(returningString.ToCharArray());
  //Returning a confirmation string back to the client. 
  soTcp.Send(returningByte, returningByte.Length, 0);
  tcpListener.Stop();
  }
  }
  catch (SocketException se)
  {
  Console.WriteLine("A Socket Exception has occurred!" + se.ToString());
  }
  }
  }
  }

       
---------------------------------
 Sent from Yahoo! &#45; a smarter inbox.

Other related posts:

  • » help with TCP server in c#