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.Text;
- using System.Threading;
- namespace Client
- {
- public class Client
- {
- private readonly Socket _sock;
- public void Run()
- {
- new Thread(() =>
- {
- while (true) ReceiveData();
- }) {IsBackground = true}.Start();
- while (true) SendData();
- }
- public Client(IPAddress address, int port)
- {
- Socket nouvelle_sock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
- _sock = nouvelle_sock;
- _sock.Connect(address,port);
- }
- public void ReceiveData()
- {
- byte[] bytes = new byte[1024];
- _sock.Receive(bytes);
- string str = "";
- int i = 0;
- while (bytes[i]!=0)
- {
- str += (char)(bytes[i]);
- i++;
- }
- Console.WriteLine("["+ DateTime.Today.ToString()+"] "+str);
- }
- public void SendData()
- {
- string message = Console.ReadLine();
- byte[] msg = Encoding.ASCII.GetBytes(message);
- _sock.Send(msg);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement