protected void connect() { IPEndPoint ip = new IPEndPoint(IPAddress.Parse("128.143.69.241"), 9050); Socket server = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); try { server.Connect(ip); } catch (SocketException e) { Console.WriteLine(e.StackTrace); Console.WriteLine("Unable to connect to server."); return; } server.Send(Encoding.ASCII.GetBytes("GetLogCount\r\n")); byte[] data = new byte[1024]; int receivedDataLength = server.Receive(data); string stringData = Encoding.ASCII.GetString(data, 0, receivedDataLength); Console.WriteLine("DATA FROM SERVER: " + stringData); //server.Close(); }
public void clearScore(int level) { if (level > 5 || level < 0) throw new ArgumentOutOfRangeException("level must be between 0 and 5"); IPEndPoint ip = new IPEndPoint(IPAddress.Parse("128.143.69.241"), 9050); Socket server = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); try { Console.WriteLine("Trying connection to server."); server.Connect(ip); } catch (SocketException e) { Console.WriteLine(e.StackTrace); Console.WriteLine("Unable to connect to server."); return; } double rando = new System.Random().NextDouble(); string command = "HIGHSCORE-CLEAR-" + level + "\r\n"; Console.WriteLine("Sending command \"" + command + "\"."); server.Send(Encoding.ASCII.GetBytes(command)); byte[] data = new byte[1024]; Console.WriteLine("Sent command \"" + command + "\"."); int receivedDataLength = server.Receive(data); Console.WriteLine("Received command \"" + command + "\"."); string stringData = Encoding.ASCII.GetString(data, 0, receivedDataLength); Console.WriteLine("DATA FROM SERVER: " + stringData); server.Close(); }
public void getScores(int level, out int numberOfEntries, out String[] names, out int[] scores, out long[] datesInMillis) { if (level > 5 || level < 0) throw new ArgumentOutOfRangeException("level must be between 0 and 5"); IPEndPoint ip = new IPEndPoint(IPAddress.Parse("128.143.69.241"), 9050); Socket server = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); try { Console.WriteLine("Trying connection to server."); server.Connect(ip); } catch (SocketException e) { Console.WriteLine(e.StackTrace); Console.WriteLine("Unable to connect to server."); numberOfEntries = 0; names = null; scores = null; datesInMillis = null; return; } double rando = new System.Random().NextDouble(); string command = "HIGHSCORE-GET-" + level + "\r\n"; Console.WriteLine("Sending command \"" + command + "\"."); server.Send(Encoding.ASCII.GetBytes(command)); byte[] data = new byte[1024]; Console.WriteLine("Sent command \"" + command + "\"."); int receivedDataLength = server.Receive(data); Console.WriteLine("Received command \"" + command + "\"."); string stringData = Encoding.ASCII.GetString(data, 0, receivedDataLength); Console.WriteLine("DATA FROM SERVER: " + stringData); //process returned data String[] splits = stringData.Split('|'); numberOfEntries = 0; foreach (String s in splits) { if (!(s == null)) if (!s.Equals("") && !s.Equals("\n")) { numberOfEntries++; } } String[] entries = new String[numberOfEntries]; int index = 0; foreach (String s in splits) { if (!(s == null)) if (!s.Equals("") && !s.Equals("\n")) { entries[index] = s; index++; //Console.WriteLine("<<" + s + ">>"); } } names = new String[numberOfEntries]; scores = new int[numberOfEntries]; datesInMillis = new long[numberOfEntries]; //Console.WriteLine("entries = " + numberOfEntries); for (int i = 0; i < numberOfEntries; i++) { //Console.WriteLine(entries[i]); String[] split = entries[i].Split('-'); names[i] = split[0]; scores[i] = Convert.ToInt32(split[1]); datesInMillis[i] = Convert.ToInt64(split[2]); } server.Close(); }