Created
          March 15, 2019 18:22 
        
      - 
      
- 
        Save rdeioris/105a2c4cb426e87ce2c01eff8d7b2d0d to your computer and use it in GitHub Desktop. 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | using System; | |
| using System.Collections.Generic; | |
| using System.Linq; | |
| using System.Text; | |
| using System.Threading.Tasks; | |
| using System.Net; | |
| using System.Net.Sockets; | |
| namespace ReverseStringServer2 | |
| { | |
| class Program | |
| { | |
| static void Main(string[] args) | |
| { | |
| // address to bind | |
| IPEndPoint endPoint = new IPEndPoint(IPAddress.Any, 9999); | |
| // create the the socket | |
| Socket socket = new Socket(SocketType.Dgram, ProtocolType.Udp); | |
| // bind to endpoint | |
| socket.Bind(endPoint); | |
| while (true) | |
| { | |
| byte[] data = new byte[4096]; | |
| EndPoint sender = new IPEndPoint(0, 0); | |
| int rlen = socket.ReceiveFrom(data, ref sender); | |
| // transform to utf8 | |
| string message = Encoding.UTF8.GetString(data, 0, rlen); | |
| char[] chars = message.ToCharArray(); | |
| Array.Reverse(chars); | |
| byte[] response = Encoding.UTF8.GetBytes(chars); | |
| socket.SendTo(response, sender); | |
| } | |
| } | |
| static void Client() | |
| { | |
| IPEndPoint serverEndPoint = new IPEndPoint(IPAddress.Parse("192.168.22.223"), 9999); | |
| // create the the socket | |
| Socket socket = new Socket(SocketType.Dgram, ProtocolType.Udp); | |
| while (true) | |
| { | |
| string message = Console.ReadLine(); | |
| byte[] data = Encoding.UTF8.GetBytes(message); | |
| socket.SendTo(data, serverEndPoint); | |
| byte[] response = new byte[4096]; | |
| int rlen = socket.Receive(response); | |
| Console.WriteLine(Encoding.UTF8.GetString(response, 0, rlen)); | |
| } | |
| } | |
| } | |
| } | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment