new networkStream which arguments to use?

0

This is the following I have a server tcp and a client, the client has in one 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 have an argument but I do not know which one. Can someone tell me what I'm supposed to do as an argument?

If you need the code you have 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();
        }
    }
}
    
asked by anonymous 18.08.2016 / 17:33

1 answer

0
The NetworkStream () constructor requests a parameter of type Socket , that is, it needs to know the corresponding socket to send and receive bytes . So I've noticed, your application is of the client type you should create a socket to establish connections 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 strEnderecoIP variable saves the connection IP to the server followed by the port specified in the 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 array of the file to be sent.

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

You can also find more information in the Microsoft documentation: link

    
18.08.2016 / 20:45