Exemplo n.º 1
0
 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();
 }
Exemplo n.º 2
0
		void Connect (HttpWebRequest request)
		{
			lock (socketLock) {
				if (socket != null && socket.Connected && status == WebExceptionStatus.Success) {
					// Take the chunked stream to the expected state (State.None)
					if (CanReuse () && CompleteChunkedRead ()) {
						reused = true;
						return;
					}
				}

				reused = false;
				if (socket != null) {
					socket.Close();
					socket = null;
				}

				chunkStream = null;
				IPHostEntry hostEntry = sPoint.HostEntry;

				if (hostEntry == null) {
#if MONOTOUCH
					xamarin_start_wwan (sPoint.Address.ToString ());
					hostEntry = sPoint.HostEntry;
					if (hostEntry == null) {
#endif
						status = sPoint.UsesProxy ? WebExceptionStatus.ProxyNameResolutionFailure :
									    WebExceptionStatus.NameResolutionFailure;
						return;
#if MONOTOUCH
					}
#endif
				}

				//WebConnectionData data = Data;
				foreach (IPAddress address in hostEntry.AddressList) {
					try {
						socket = new Socket (address.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
					} catch (Exception se) {
						// The Socket ctor can throw if we run out of FD's
						if (!request.Aborted)
								status = WebExceptionStatus.ConnectFailure;
						connect_exception = se;
						return;
					}
					IPEndPoint remote = new IPEndPoint (address, sPoint.Address.Port);
					socket.NoDelay = !sPoint.UseNagleAlgorithm;
					try {
						sPoint.KeepAliveSetup (socket);
					} catch {
						// Ignore. Not supported in all platforms.
					}

					if (!sPoint.CallEndPointDelegate (socket, remote)) {
						socket.Close ();
						socket = null;
						status = WebExceptionStatus.ConnectFailure;
					} else {
						try {
							if (request.Aborted)
								return;
							socket.Connect (remote);
							status = WebExceptionStatus.Success;
							break;
						} catch (ThreadAbortException) {
							// program exiting...
							Socket s = socket;
							socket = null;
							if (s != null)
								s.Close ();
							return;
						} catch (ObjectDisposedException) {
							// socket closed from another thread
							return;
						} catch (Exception exc) {
							Socket s = socket;
							socket = null;
							if (s != null)
								s.Close ();
							if (!request.Aborted)
								status = WebExceptionStatus.ConnectFailure;
							connect_exception = exc;
						}
					}
				}
			}
		}
Exemplo n.º 3
0
 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();
 }