Created
          March 25, 2019 12:40 
        
      - 
      
- 
        Save rdeioris/6364c7bcca73afb0c0b70c28a0dea537 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; | |
| using System.Net.Http; | |
| namespace StarWarsClient | |
| { | |
| class Program | |
| { | |
| static void Server(string[] args) | |
| { | |
| Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); | |
| socket.Blocking = false; | |
| IPEndPoint endPoint = new IPEndPoint(IPAddress.Any, 7777); | |
| socket.Bind(endPoint); | |
| socket.Listen(10); | |
| Dictionary<Socket, byte[]> dataToSend = new Dictionary<Socket, byte[]>(); | |
| List<Socket> knownSockets = new List<Socket>(); | |
| List<Socket> socketsWaitingForWrite = new List<Socket>(); | |
| // add myself | |
| knownSockets.Add(socket); | |
| while (true) | |
| { | |
| List<Socket> readSockets = new List<Socket>(knownSockets); | |
| Socket.Select(readSockets, null, null, -1); | |
| Console.WriteLine("{0} sockets ready for read", readSockets.Count); | |
| foreach (Socket readySocket in readSockets) | |
| { | |
| // myself ? | |
| if (readySocket == socket) | |
| { | |
| Socket newClient = socket.Accept(); | |
| newClient.Blocking = false; | |
| knownSockets.Add(newClient); | |
| Console.WriteLine("{0} sockets to monitor", knownSockets.Count); | |
| } | |
| // client socket ready, recv data | |
| else | |
| { | |
| byte[] data = new byte[4096]; | |
| int rlen = readySocket.Receive(data); | |
| if (rlen <= 0) | |
| { | |
| knownSockets.Remove(readySocket); | |
| } | |
| else | |
| { | |
| Console.WriteLine(Encoding.ASCII.GetString(data, 0, rlen)); | |
| // potentially blocked | |
| int wlen = readySocket.Send(data, 0, rlen, SocketFlags.None); | |
| if (wlen < rlen) | |
| { | |
| byte[] remainingData = new byte[rlen - wlen]; | |
| Buffer.BlockCopy(data, wlen, remainingData, 0, remainingData.Length); | |
| dataToSend[readySocket] = remainingData; | |
| // not enough bytes sent | |
| socketsWaitingForWrite.Add(readySocket); | |
| } | |
| } | |
| } | |
| } | |
| } | |
| } | |
| static void StarWars(string[] args) | |
| { | |
| Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); | |
| IPAddress[] addresses = Dns.GetHostAddresses("towel.blinkenlights.nl"); | |
| Console.WriteLine(addresses[0]); | |
| socket.Connect(addresses, 23); | |
| while (true) | |
| { | |
| byte[] data = new byte[4096]; | |
| int rlen = socket.Receive(data); | |
| Console.WriteLine(rlen); | |
| string message = Encoding.ASCII.GetString(data, 0, rlen); | |
| Console.WriteLine(message); | |
| } | |
| } | |
| static void Main(string[] args) | |
| { | |
| Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); | |
| IPEndPoint endPoint = new IPEndPoint(IPAddress.Any, 7777); | |
| socket.Bind(endPoint); | |
| socket.Listen(10); | |
| while (true) | |
| { | |
| Socket newClient = socket.Accept(); | |
| string request = ""; | |
| while (true) | |
| { | |
| byte[] data = new byte[4096]; | |
| int rlen = newClient.Receive(data); | |
| if (rlen <= 0) | |
| { | |
| break; | |
| } | |
| request += Encoding.ASCII.GetString(data, 0, rlen); | |
| if (request.Contains("\r\n\r\n")) | |
| { | |
| break; | |
| } | |
| } | |
| // if i am here the request is complete | |
| string response = "HTTP/1.0 200 OK\r\nContent-Type: text/html\r\nConnection: close\r\n\r\n<h1>AIV Cacca</h1><hr/><img src='http://fulmyne.it/Wood%20Carving/Immagini%20di%20servizio/paginiz.JPG' />"; | |
| newClient.Send(Encoding.ASCII.GetBytes(response)); | |
| newClient.Close(); | |
| } | |
| } | |
| static void HttpClient(string[] args) | |
| { | |
| Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); | |
| IPAddress[] addresses = Dns.GetHostAddresses("fulmyne.it"); | |
| Console.WriteLine(addresses[0]); | |
| socket.Connect(addresses, 80); | |
| string httpRequest = "GET /Wood%20Carving/Immagini%20di%20servizio/paginiz.JPG HTTP/1.0\r\nHost: fulmyne.it\r\n\r\n"; | |
| socket.Send(Encoding.ASCII.GetBytes(httpRequest)); | |
| while (true) | |
| { | |
| byte[] data = new byte[4096]; | |
| int rlen = socket.Receive(data); | |
| if (rlen <= 0) | |
| { | |
| break; | |
| } | |
| string message = Encoding.ASCII.GetString(data, 0, rlen); | |
| Console.WriteLine(message); | |
| } | |
| Console.ReadLine(); | |
| } | |
| static void HttpClient2(string[] args) | |
| { | |
| WebRequest request = WebRequest.Create("https://www.aiv01.it/wp-content/uploads/2018/11/AIVlogo-img1.png"); | |
| WebResponse response = request.GetResponse(); | |
| } | |
| } | |
| } | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment