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;
- using System.Net.Sockets;
- using System.Threading;
- namespace Client
- {
- class Program
- {
- public class AsyncClient
- {
- private Socket Client;
- private byte[] buffer;
- public AsyncClient()
- {
- Setup();
- Console.ReadKey();
- }
- private void Setup()
- {
- try
- {
- Client = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
- Client.BeginConnect(new IPEndPoint(IPAddress.Loopback, 1111), new AsyncCallback(ConnectCallBack), null);
- Send();
- }
- catch(Exception ex)
- {
- Console.WriteLine(ex.ToString());
- }
- }
- private void ConnectCallBack(IAsyncResult AR)
- {
- try
- {
- Client.EndAccept(AR);
- Console.WriteLine("Connected.");
- }
- catch (Exception ex)
- {
- Console.WriteLine(ex.ToString());
- }
- }
- private void Send()
- {
- while (true)
- {
- string text = "";
- text = Console.ReadLine();
- buffer = new byte[text.Length];
- for (int i = 0; i < text.Length; i++)
- {
- buffer[i] = (byte)text[i];
- }
- Client.BeginSend(buffer, 0, buffer.Length, SocketFlags.None, new AsyncCallback(SendCallBack), null);
- }
- }
- private void SendCallBack(IAsyncResult AR)
- {
- try
- {
- Client.EndSend(AR);
- }
- catch (Exception ex)
- {
- Console.WriteLine(ex.ToString());
- }
- }
- }
- static void Main(string[] args)
- {
- AsyncClient AC = new AsyncClient();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement