Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Net.Sockets;
- using System.Net;
- namespace Serwer
- { /////////////////////// K L I E N T ///////////////////////
- class Program
- {
- private static TcpListener tcpLsn;
- private static Socket s;
- private static void polacz()
- {
- s = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
- IPAddress hostadd = IPAddress.Parse("127.0.0.1");
- int port = 2222;
- IPEndPoint EPhost = new IPEndPoint(hostadd, port);
- s.Connect(EPhost);
- }
- private static void odbierz()
- {
- tcpLsn = new TcpListener(IPAddress.Parse("127.0.0.1"), 2222);
- tcpLsn.Start();
- Socket s = tcpLsn.AcceptSocket();
- Byte[] odebraneBajty = new Byte[100];
- int red = s.Receive(odebraneBajty, odebraneBajty.Length, 0);
- string eko= null;
- eko = System.Text.Encoding.ASCII.GetString(odebraneBajty);
- Console.WriteLine(eko);
- }
- private static void odbierzrr()
- {
- Byte[] odebraneBajty = new Byte[100];
- int ret = sckt.Receive(odebraneBajty, odebraneBajty.Length, 0);
- string tmp = null;
- tmp = System.Text.Encoding.ASCII.GetString(odebraneBajty);
- if (tmp.Length > 0)
- Console.WriteLine("Odebrałem komunikat:");
- Console.WriteLine(tmp);
- }
- private static void wyslij(string wiadomosc)
- {
- Byte[] byteData = Encoding.ASCII.GetBytes(wiadomosc.ToCharArray());
- s.Send(byteData, byteData.Length, 0);
- }
- static void Main(string[] args)
- {
- polacz();
- string Line;
- for(;;)
- try
- {
- Console.WriteLine("Podaj komunikat klient 1 ");
- Line = Console.ReadLine();
- if (Line != "quit" )
- {
- Console.WriteLine("Wysyłam komunikat na serwer");
- wyslij(Line);
- Console.WriteLine("Oczekuje na serwer");
- Console.ReadKey();
- tcpLsn = new TcpListener(IPAddress.Parse("127.0.0.1"), 2222);
- tcpLsn.Start();
- Socket s = tcpLsn.AcceptSocket();
- Byte[] odebraneBajty = new Byte[100];
- int red = s.Receive(odebraneBajty, odebraneBajty.Length, 0);
- string eko = null;
- eko = System.Text.Encoding.ASCII.GetString(odebraneBajty);
- Console.WriteLine(eko);
- Console.WriteLine("odebrano komunikat");
- Console.ReadKey();
- }
- }
- catch
- {
- Environment.Exit(0);
- }
- tcpLsn.Stop();
- }
- // Console.WriteLine("Wysyłam komunikat...");
- // wyslij("ala ma kota");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement