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;
- using System.Threading;
- namespace Zad_4_5_Client_serwer_1_
- {
- class Program
- {
- private static Socket s;
- private static TcpListener tcpLsn;
- private static readonly string stopMsg = "quit";
- private static string stopSerMsg;
- private static void Connect()
- {
- try
- {
- s = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
- IPAddress hostIp = IPAddress.Parse("127.0.0.1");
- IPEndPoint EPhost = new IPEndPoint(hostIp, 2222);
- s.Connect(EPhost);
- Console.WriteLine("Połączono do serwera na porcie: " + 2222);
- }
- catch (Exception e)
- {
- Console.WriteLine(e.Message);
- return;
- }
- }
- private static void Send(string message)
- {
- try
- {
- Byte[] byteData = Encoding.ASCII.GetBytes(message.ToCharArray());
- Console.WriteLine("Wysyłano: " + message);
- s.Send(byteData, byteData.Length, 0);
- }
- catch (Exception e)
- {
- Console.WriteLine(e.Message);
- return;
- }
- }
- static void RunServer()
- {
- tcpLsn = new TcpListener(IPAddress.Parse("127.0.0.1"), 2000);
- tcpLsn.Start();
- Console.WriteLine("Serwer działa na porcie: " + 2000);
- }
- static void Receive()
- {
- while (true)
- {
- try
- {
- while (true)
- {
- Socket sckt = tcpLsn.AcceptSocket();
- Byte[] received = new Byte[100];
- int res = sckt.Receive(received, received.Length, 0);
- string tmp = null;
- tmp = System.Text.Encoding.ASCII.GetString(received);
- Console.WriteLine("Otrzymano: " + tmp);
- if (tmp.Length > 0)
- {
- Console.WriteLine(tmp);
- }
- stopSerMsg = tmp;
- if (string.Compare(tmp, "quit") == 0)
- {
- StopServer();
- }
- }
- }
- catch (Exception e)
- {
- Console.WriteLine(e.Message);
- return;
- }
- }
- }
- static void StopServer()
- {
- Console.WriteLine("Zatrzymano Serwer");
- tcpLsn.Stop();
- }
- private static void ServerTask()
- {
- RunServer();
- Receive();
- StopServer();
- }
- private static void ClientTask()
- {
- String input = null;
- do
- {
- Connect();
- Console.WriteLine("Podaj wiadomość: ");
- input = Console.ReadLine();
- Send(input);
- } while (IsQuit(input));
- Console.WriteLine(stopMsg);
- return;
- }
- private static Boolean IsQuit(string cmd)
- {
- return !stopMsg.Equals(cmd);
- }
- static void Main(string[] args)
- {
- Thread serwer = new Thread(ServerTask);
- Thread klient = new Thread(ClientTask);
- serwer.Start();
- klient.Start();
- if (string.Compare(stopSerMsg, "quit") == 0)
- {
- klient.Abort();
- }
- }
- }
- }
- drugi__
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Net.Sockets;
- using System.Net;
- using System.Threading;
- namespace Zad_4_5_Client_serwer_2_
- {
- class Program
- {
- private static Socket s;
- private static TcpListener tcpLsn;
- private static readonly string stopMsg = "quit";
- private static string stopSerMsg;
- private static void Connect()
- {
- try
- {
- s = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
- IPAddress hostIp = IPAddress.Parse("127.0.0.1");
- IPEndPoint EPhost = new IPEndPoint(hostIp, 2000);
- s.Connect(EPhost);
- Console.WriteLine("Połączono do serwera na porcie: " + 2000);
- }
- catch (Exception e)
- {
- Console.WriteLine(e.Message);
- return;
- }
- }
- private static void Send(string message)
- {
- try
- {
- Byte[] byteData = Encoding.ASCII.GetBytes(message.ToCharArray());
- Console.WriteLine("Wysyłano: " + message);
- s.Send(byteData, byteData.Length, 0);
- }
- catch (Exception e)
- {
- Console.WriteLine(e.Message);
- return;
- }
- }
- static void RunServer()
- {
- tcpLsn = new TcpListener(IPAddress.Parse("127.0.0.1"), 2222);
- tcpLsn.Start();
- Console.WriteLine("Serwer działa na porcie: " + 2222);
- }
- static void Receive()
- {
- while (true)
- {
- try
- {
- while (true)
- {
- Socket sckt = tcpLsn.AcceptSocket();
- Byte[] received = new Byte[100];
- int res = sckt.Receive(received, received.Length, 0);
- string tmp = null;
- tmp = System.Text.Encoding.ASCII.GetString(received);
- Console.WriteLine("Otrzymano: " + tmp);
- if (tmp.Length > 0)
- {
- Console.WriteLine(tmp);
- }
- stopSerMsg = tmp;
- if (string.Compare(tmp, "quit") == 0)
- {
- StopServer();
- }
- }
- }
- catch (Exception e)
- {
- Console.WriteLine(e.Message);
- return;
- }
- }
- }
- static void StopServer()
- {
- Console.WriteLine("Zatrzymano Serwer");
- tcpLsn.Stop();
- }
- private static void ServerTask()
- {
- RunServer();
- Receive();
- StopServer();
- }
- private static void ClientTask()
- {
- String input = null;
- do
- {
- Connect();
- Console.WriteLine("Podaj wiadomość: ");
- input = Console.ReadLine();
- Send(input);
- } while (IsQuit(input));
- Console.WriteLine(stopMsg);
- return;
- }
- private static Boolean IsQuit(string cmd)
- {
- return !stopMsg.Equals(cmd);
- }
- static void Main(string[] args)
- {
- Thread serwer = new Thread(ServerTask);
- Thread klient = new Thread(ClientTask);
- serwer.Start();
- klient.Start();
- if (string.Compare(stopSerMsg, "quit") == 0)
- {
- klient.Abort();
- }
- }
- }
- }
- Hello
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Net.Sockets;
- using System.Net;
- namespace Hello
- {
- class Program
- {
- private static TcpListener tcpLan;
- 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 wyslij(string wiadomosc)
- {
- Byte[] byteData = Encoding.ASCII.GetBytes(wiadomosc.ToCharArray());
- s.Send(byteData, byteData.Length, 0);
- }
- static void Main(string[] args)
- {
- polacz();
- Console.WriteLine("Wysyłam komunikat...");
- wyslij("ala ma kota");
- System.Threading.Thread.Sleep(3000);
- }
- }
- }
- __serwer hello
- 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
- {
- class Program
- {
- private static TcpListener tcpLsn;
- private static Socket s;
- static void serwuj()
- {
- tcpLsn = new TcpListener(IPAddress.Parse("127.0.0.1"), 2222);
- tcpLsn.Start();
- Socket sckt = tcpLsn.AcceptSocket();
- 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);
- }
- tcpLsn.Stop();
- }
- static void Main(string[] args)
- {
- Console.WriteLine("Startuję serwer...");
- serwuj();
- System.Threading.Thread.Sleep(3000);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement