new networkStream what arguments to use?

Asked

Viewed 116 times

0

is the following I have a server tcp and a client, the client has in a part of the code the following

        private void SendDesktop()
    {   
        BinaryFormatter bf = new BinaryFormatter();
        ns = new NetworkStream();
        ns = client.GetStream();
        bf.Serialize(ns, Desktop());
    }

in this part ns = new NetworkStream(); has an error that has no arguments I would like to put an argument but do not know which one. someone can tell me what I am supposed to put as argument?

If you need your own code here

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Drawing.Imaging;
using System.Drawing;
using System.Net;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;

namespace DesktopClient
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        TcpClient client = new TcpClient();
        NetworkStream ns = null;
        int port = 0;

        public Image  Desktop()
        {
            Rectangle bounds = new Rectangle();
            Bitmap Screenshot = null;
            Graphics graph = null;
            bounds = Screen.PrimaryScreen.Bounds;
            Screenshot = new Bitmap(bounds.Width, bounds.Height, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
            graph = Graphics.FromImage(Screenshot);
            graph.CopyFromScreen(bounds.X, bounds.Y, 0, 0, bounds.Size, CopyPixelOperation.SourceCopy);
            return Screenshot;               
        }

        private void SendDesktop()
        {   
            BinaryFormatter bf = new BinaryFormatter();
            ns = new NetworkStream();
            ns = client.GetStream();
            bf.Serialize(ns, Desktop());
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void ConnectBtb_Click(object sender, EventArgs e)
        {
            port = int.Parse(PortBox.Text);
            try
            {
                client.Connect(Ipbox.Text, port);
                label3.Text = "Connected";
            }
            catch (Exception)
            {
                label3.Text = "Faild To Connect";
            }
        }

        private void ShareBtn_Click(object sender, EventArgs e)
        {
            timer1.Start(); 
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            SendDesktop();
        }
    }
}
  • We do not know either, after all the code is yours, but whenever you have doubts about how to use a method, just consult the documentation: https://msdn.microsoft.com/en-us/library/system.net.sockets.networkstream(v=vs.110). aspx

  • Look at this question

1 answer

0


The builder Networkstream() requires a type parameter Socket, that is, it needs to know the corresponding socket to send and receive bytes. From what I’ve noticed, your application is kind of client you must create a socket to establish links between two computers:

string strEnderecoIP = "192.168.1.14";
                IPEndPoint pointCliente = new IPEndPoint(IPAddress.Parse(strEnderecoIP), 5486);
                Socket clienteSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.IP);

The variable strEnderecoIP saves the IP connection to the server followed by the port specified in endpoint. After that just get the byte array corresponding to the file:

clienteSocket.Connect(pointCliente);
clienteSocket.Send(byteArr, 0, byteArr.Length, 0);
                clienteSocket.Close();

Where byteArr is the byte matrix of the file to be sent.

Note: Remember to specify a reserved port by default to computer network services.

You can also find more information in the Microsoft documentation: https://msdn.microsoft.com/pt-br/library/system.net.sockets.socket(v=vs.110). aspx

  • How do I define byteArr?

  • I made this variable private byte[] byteArr; but the show said A referência de objecto não foi definida como uma instância de um objecto.If you could help me out here I’d appreciate it while I’m at it.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.