/// <summary> /// Run a very simple server. /// </summary> private void RunServer() { try { Log.I("SimpleHttpServer", "Creating server socket"); var serverSocket = new ServerSocket(PORT); var requestCount = 0; try { while (!stop) { Log.I("SimpleHttpServer", "Waiting for connection"); var socket = serverSocket.Accept(); var input = new BufferedReader(new InputStreamReader(socket.GetInputStream())); var output = new BufferedWriter(new OutputStreamWriter(socket.GetOutputStream())); string line; Log.I("SimpleHttpServer", "Reading request"); while ((line = input.ReadLine()) != null) { Log.I("SimpleHttpServer", "Received: " + line); if (line.Length == 0) break; } Log.I("SimpleHttpServer", "Sending response"); output.Write("HTTP/1.1 200 OK\r\n"); output.Write("\r\n"); output.Write(string.Format("Hello world {0}\r\n", requestCount)); output.Flush(); socket.Close(); requestCount++; } } finally { serverSocket.Close(); } } catch (Exception ex) { Log.E("SimpleHttpServer", "Connection error", ex); } }
public void OnConnectionInfoAvailable(WifiP2pInfo info) { if (_progressDialog != null && _progressDialog.IsShowing) _progressDialog.Dismiss(); _info = info; View.Visibility = ViewStates.Visible; // The owner IP is now known. var view = _contentView.FindViewById<TextView>(Resource.Id.group_owner); view.Text = Resources.GetString(Resource.String.group_owner_text) + ((info.IsGroupOwner) ? Resources.GetString(Resource.String.yes) : Resources.GetString(Resource.String.no)); // InetAddress from WifiP2pInfo struct. view = _contentView.FindViewById<TextView>(Resource.Id.device_info); view.Text = "Group Owner IP - " + _info.GroupOwnerAddress.HostAddress; // After the group negotiation, we assign the group owner as the file // server. The file server is single threaded, single connection server // socket. if (_info.GroupFormed && _info.IsGroupOwner) { Task.Factory.StartNew(() => { try { var serverSocket = new ServerSocket(8988); Log.Debug(WiFiDirectActivity.Tag, "Server: Socket opened"); var client = serverSocket.Accept(); Log.Debug(WiFiDirectActivity.Tag, "Server: connection done"); var f = new File(Environment.ExternalStorageDirectory + "/" + Activity.PackageName + "/wifip2pshared-" + DateTime.Now.Ticks + ".jpg"); var dirs = new File(f.Parent); if (!dirs.Exists()) dirs.Mkdirs(); f.CreateNewFile(); Log.Debug(WiFiDirectActivity.Tag, "Server: copying files " + f); var inputStream = client.InputStream; CopyFile(inputStream, new FileStream(f.ToString(), FileMode.OpenOrCreate)); serverSocket.Close(); return f.AbsolutePath; } catch (IOException e) { Log.Error(WiFiDirectActivity.Tag, e.Message); return null; } }) .ContinueWith(result => { if (result != null) { _contentView.FindViewById<TextView>(Resource.Id.status_text).Text = "File copied - " + result.Result; var intent = new Intent(); intent.SetAction(Intent.ActionView); intent.SetDataAndType(Android.Net.Uri.Parse("file://" + result.Result), "image/*"); Activity.StartActivity(intent); } }); } else if (_info.GroupFormed) { _contentView.FindViewById<Button>(Resource.Id.btn_start_client).Visibility = ViewStates.Visible; _contentView.FindViewById<TextView>(Resource.Id.status_text).Text = Resources.GetString(Resource.String.client_text); } _contentView.FindViewById<Button>(Resource.Id.btn_connect).Visibility = ViewStates.Gone; }