ZengCode.Com (The Thai Php Framework)  


Home   Download   Manual   About us    

Facebook   


MAIN MENU
News
Php Tips
Spring+Strut+Hibernate
Android Programming
Design Pattern By PHP
Ubuntu
Linux Quick Tips
C# Design Pattern
C# using Linq น่าใช้จริงๆ
C# Tips & Technique
Java & JavaScript Tips
Database & SQL
ZengCode Framework Guide
Zeng Code Code
Programming
IPhone (Tips and Trick)

Download เอกสารที่น่าสนใจ

     TCP IP Simple  (2009-09-11)

class Server
{
 private TcpListener tcpListener;
 private Thread listenThread;
 public Server()
 {
  this.tcpListener = new TcpListener(IPAddress.Any, 9001);
  this.listenThread = new Thread(new ThreadStart(ListenForClients));
  this.listenThread.Start();
 }
 
 private void ListenForClients()
 {
  this.tcpListener.Start();
  while (true)
  {
   //blocks until a client has connected to the server
   TcpClient client = this.tcpListener.AcceptTcpClient();
   //create a thread to handle communication
   //with connected client
   Thread clientThread = new Thread(new ParameterizedThreadStart(HandleClientComm));
   clientThread.Start(client);
  }
  
 }
 
 private void HandleClientComm(object client)
 {
  //int receivedDataLength = ns.Read(data, 0, data.Length);
   TcpClient tcpClient = (TcpClient)client;
   NetworkStream clientStream = tcpClient.GetStream();
   ASCIIEncoding encoder = new ASCIIEncoding();
   byte[] message = new byte[4096];
   int bytesRead;
   
   while (true)
   {
    bytesRead = 0;
    try
    {
     //blocks until a client sends a message
     bytesRead = clientStream.Read(message, 0, message.Length);
    }
    catch
    { //a socket error has occured
     break;
    }
    
    if (bytesRead == 0)
    {
     //the client has disconnected from the server
     break;
    } //message has successfully been received

   string stringData = Encoding.ASCII.GetString(message, 0, bytesRead);
  }

  clientStream.Close();
  tcpClient.Close();
  }
 }


Comment
Name
Comment
Security CodeCAPTCHA Image

web hit counter

This page took 0.049159 seconds to load.