|
|
@@ -1,41 +1,44 @@
|
|
|
-using System.Text;
|
|
|
+using System;
|
|
|
+using System.Text;
|
|
|
using System.Net.Sockets;
|
|
|
using System.Threading;
|
|
|
+using System.Net;
|
|
|
+using System.IO;
|
|
|
|
|
|
namespace MusicPlayer
|
|
|
{
|
|
|
class NetworkHandler
|
|
|
{
|
|
|
private int port = 8585;
|
|
|
- private Socket s;
|
|
|
+ private string ip;
|
|
|
private APIHandler api;
|
|
|
|
|
|
public NetworkHandler(string ip, APIHandler apihandler)
|
|
|
{
|
|
|
- s = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
|
|
|
- s.Connect(ip, port);
|
|
|
- ThreadStart thread = new ThreadStart(ReceiveData);
|
|
|
- Thread childThread = new Thread(thread);
|
|
|
- childThread.Start();
|
|
|
- api = apihandler;
|
|
|
+ this.ip = ip;
|
|
|
+ this.api = apihandler;
|
|
|
}
|
|
|
|
|
|
public void SendString(string m)
|
|
|
{
|
|
|
- byte[] req_as_bytes = Encoding.UTF8.GetBytes(m);
|
|
|
- s.Send(req_as_bytes);
|
|
|
- }
|
|
|
-
|
|
|
- public void ReceiveData()
|
|
|
- {
|
|
|
- while (s.Connected)
|
|
|
+ HttpWebRequest server = (HttpWebRequest)WebRequest.Create(ip+":"+port+"/"+m);
|
|
|
+ server.KeepAlive = false;
|
|
|
+ HttpWebResponse respond = (HttpWebResponse)server.GetResponse();
|
|
|
+ Stream streamResponse = respond.GetResponseStream();
|
|
|
+ StreamReader streamRead = new StreamReader(streamResponse);
|
|
|
+ Char[] readBuff = new Char[256];
|
|
|
+ int count = streamRead.Read(readBuff, 0, 256);
|
|
|
+ string data = "";
|
|
|
+ while (count > 0)
|
|
|
{
|
|
|
- byte[] data = new byte[1024 * 200];
|
|
|
- s.Receive(data);
|
|
|
- string message = Encoding.ASCII.GetString(data);
|
|
|
- System.Console.WriteLine(message);
|
|
|
- //Iets doen met api calls
|
|
|
+ String outputData = new String(readBuff, 0, count);
|
|
|
+ data +=outputData;
|
|
|
+ count = streamRead.Read(readBuff, 0, 256);
|
|
|
}
|
|
|
+ System.Console.WriteLine(data);
|
|
|
+ respond.Close();
|
|
|
+ streamResponse.Close();
|
|
|
+ streamRead.Close();
|
|
|
}
|
|
|
}
|
|
|
}
|