Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Net;
- using System.Net.Sockets;
- using System.IO;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace serwer
- {
- class Program
- {
- static void Main(string[] args)
- {
- TcpListener serwer = new TcpListener(11000);
- serwer.Start();
- while (true)
- {
- TcpClient klient = serwer.AcceptTcpClient();
- NetworkStream strumien = klient.GetStream();
- try
- {
- byte[] id = new byte[1];
- byte odczytane = (byte)strumien.Read(id, 0, 1);
- if (odczytane > 0)
- {
- Console.WriteLine("identyfikator:" + ((int)id[0]));
- string blok = "przykladowy blok danych"; //nie dluzsze niz 255 znakow
- byte[] dane = Encoding.ASCII.GetBytes(blok);
- byte[] rozmiar = new byte[1];
- rozmiar[0] = (byte)dane.Length;
- strumien.Write(rozmiar, 0, 1);
- strumien.Write(dane, 0, dane.Length);
- odczytane = (byte)strumien.Read(rozmiar, 0, 1); //czytanie przerwie sie w momencie zakonczenia polaczenia
- if (odczytane == 0) Console.WriteLine("Klient zakonczyl polaczenie"); // sprawdzic czy to wypisuje jak nie, to mozna wywalic
- }
- else
- {
- Console.WriteLine("klient zakonczyl polaczenie");
- }
- }
- catch (IOException)
- {
- Console.WriteLine("klient zakonczyl polaczenie");
- }
- strumien.Close();
- klient.Close();
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement