Ejemplo n.º 1
0
 private void DoReceive(IAsyncResult asyncResult)
 {
     try {
         NetworkStream netStream = AcceptedClient.GetStream();
         int           noBytesRecv;
         lock (netStream) {
             noBytesRecv = AcceptedClient.GetStream().EndRead(asyncResult);
         }
         if (noBytesRecv < 1)
         {
             UpdateTcpStatus("Disconnected from Server....", false);
         }
         else
         {
             string message = Encoding.Default.GetString(ClientMsgArray, 0, noBytesRecv);
             UpdateTcpMessage(message);
             NetworkStream netStream2 = AcceptedClient.GetStream();
             lock (netStream2) {
                 AcceptedClient.GetStream().BeginRead(ClientMsgArray, 0, 1024, new AsyncCallback(DoReceive), null);
             }
         }
     } catch (Exception ex) {
         Console.WriteLine(ex.Message);
     }
 }
Ejemplo n.º 2
0
        public void Send(string msgData)
        {
            if (AcceptedClient == null)
            {
                byte[] array = Encoding.ASCII.GetBytes(msgData);//new byte[data.Length];
                ClientSocket.BeginSend(array, 0, array.Length, SocketFlags.None, null, null);
                return;
            }
            NetworkStream netStram = AcceptedClient.GetStream();

            lock (netStram) {
                StreamWriter streamWriter = new StreamWriter(AcceptedClient.GetStream(), Encoding.GetEncoding("ISO-8859-1"));
                streamWriter.Write(msgData);
                streamWriter.Flush();
            }
        }
Ejemplo n.º 3
0
 private void Listen()
 {
     try {
         IPHostEntry iPHost = Dns.GetHostEntry(IpAddress);
         TcpListner = new TcpListener(iPHost.AddressList[0], Port);
         TcpListner.Start();
         UpdateTcpStatus("listener_start", true);
         for (; ;)
         {
             if (AcceptedClient == null)
             {
                 UpdateTcpStatus("Waiting for incoming Connection......", false);
             }
             AcceptedClient = TcpListner.AcceptTcpClient();
             RemoteIp       = AcceptedClient.Client.RemoteEndPoint.ToString();
             UpdateTcpStatus("connected", true);
             ClientMsgArray = new byte[1024];
             AcceptedClient.GetStream().BeginRead(ClientMsgArray, 0, 1024, new AsyncCallback(DoReceive), null);
         }
     } catch (Exception ex) {
         Console.WriteLine(ex.Message);
         return;
     }
 }