Advertisement
Guest User

Untitled

a guest
Nov 23rd, 2014
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.91 KB | None | 0 0
  1. using System;
  2. using System.Net;
  3. using System.Net.Sockets;
  4. using System.IO;
  5. using System.Collections.Generic;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9.  
  10. namespace serwer
  11. {
  12.     class Program
  13.     {
  14.         static void Main(string[] args)
  15.         {
  16.  
  17.             TcpListener serwer = new TcpListener(11000);
  18.             serwer.Start();
  19.             while (true)
  20.             {
  21.                 TcpClient klient = serwer.AcceptTcpClient();
  22.                 NetworkStream strumien = klient.GetStream();
  23.                 try
  24.                 {
  25.                     byte[] id = new byte[1];
  26.                     byte odczytane = (byte)strumien.Read(id, 0, 1);
  27.                     if (odczytane > 0)
  28.                     {
  29.                         Console.WriteLine("identyfikator:" + ((int)id[0]));
  30.                         string blok = "przykladowy blok danych"; //nie dluzsze niz 255 znakow
  31.                         byte[] dane = Encoding.ASCII.GetBytes(blok);
  32.                         byte[] rozmiar = new byte[1];
  33.                         rozmiar[0] = (byte)dane.Length;
  34.                         strumien.Write(rozmiar, 0, 1);
  35.                         strumien.Write(dane, 0, dane.Length);
  36.                         odczytane = (byte)strumien.Read(rozmiar, 0, 1); //czytanie przerwie sie w momencie zakonczenia polaczenia
  37.                         if (odczytane == 0) Console.WriteLine("Klient zakonczyl polaczenie"); // sprawdzic czy to wypisuje jak nie, to mozna wywalic
  38.                     }
  39.                     else
  40.                     {
  41.                         Console.WriteLine("klient zakonczyl polaczenie");
  42.                     }
  43.                 }
  44.                 catch (IOException)
  45.                 {
  46.                     Console.WriteLine("klient zakonczyl polaczenie");
  47.                 }
  48.                 strumien.Close();
  49.                 klient.Close();
  50.             }
  51.  
  52.  
  53.  
  54.         }
  55.     }
  56. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement