//@dany modifiche private void ReceiveMessages() { // Receive the response from the server srReceiver = new StreamReader(tcpServer.GetStream()); // If the first character of the response is 1, connection was successful string ConResponse = srReceiver.ReadLine(); // If the first character is a 1, connection was successful if (ConResponse[0] == '1') { // Update the form to tell it we are now connected this.Invoke(new UpdateLogCallback(this.UpdateLog), new object[] { "Connected Successfully!" }); //if there is data in the clipboard, enable the button to send it IDataObject d = Clipboard.GetDataObject(); if (d!=null && (d.GetDataPresent(DataFormats.Text) || d.GetDataPresent(DataFormats.Text))) bntClipboard.Enabled = true; // read the port sent by the server for screen sharing port_scr=int.Parse(srReceiver.ReadLine()); //MessageBox.Show("la porta è" + port_scr); //start socket for screen sharing s = new SocketforClient(ipAddr.ToString(), port_scr); //for receiving clipboard by other users IPEndPoint ipEndPoint = new IPEndPoint(ipAddr, 48231); tcpClip = new TcpClient(); tcpClip.Connect(ipEndPoint); str = new StreamReader(tcpClip.GetStream()); Thread clipThread = new Thread(ReceiveClip); clipThread.SetApartmentState(ApartmentState.STA); clipThread.Start(); //alby workerObject = new Worker(this, pictureBox1, s); workerThread = new Thread(workerObject.DoWork); workerThread.Start(); while (!workerThread.IsAlive); //alby end } else // If the first character is not a 1 (probably a 0), the connection was unsuccessful { string Reason = "Non Connesso: "; // Extract the reason out of the response message. The reason starts at the 3rd character Reason += ConResponse.Substring(2, ConResponse.Length - 2); try { // Update the form with the reason why we couldn't connect this.Invoke(new CloseConnectionCallback(this.CloseConnection), new object[] { Reason }); } catch { return; } // Exit the method return; } // While we are successfully connected, read incoming lines from the server while (Connected) { // Show the messages in the log TextBox try { this.Invoke(new UpdateLogCallback(this.UpdateLog), new object[] { srReceiver.ReadLine() }); } catch { string Reason = "Disconnesso dal server!"; //MessageBox.Show("Disconnessione!"); //CloseConnection("Disconnected..."); //btnConnect.Text = "Connect"; if (Connected) { this.Invoke(new CloseConnectionCallback(this.CloseConnection), new object[] { Reason }); this.Invoke(new DisableClipboardCallback(this.DisableClipboard), new object[] { Reason}); str.Close(); //bntClipboard.Enabled = false; } // Close the objects //Connected = false; } } }
public Worker(Form1 f, PictureBox p1, SocketforClient s) { this.f = f; this.s = s; this.p = p1; }