question

difficulty-7628 avatar image
0 Votes"
difficulty-7628 asked MikoKage answered

How can i implement a Remoting Service that sends pictures to a client

I managed to get the server running but i have no idea how to set up the client and which visual studio forms i have too use
Server:

using System;
using System.Drawing;
using System.IO;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Http;

namespace Server
{
class Server
{
static void Main(string[] args)
{
HttpChannel channel = new HttpChannel(8090);
ChannelServices.RegisterChannel(channel, false);
RemotingConfiguration.RegisterWellKnownServiceType(typeof(Huans), "Object", WellKnownObjectMode.Singleton);
Console.WriteLine("Server is running at:8090");
Console.ReadLine();

     }
 }

 [Serializable]
 public class MyImage
 {
     public string imagePath, name;
     public MyImage(string imagePath, string name)
     {
         this.imagePath = imagePath;
         this.name = name;
     }
 }
 public class Huans : MarshalByRefObject, IPictureServer
 {
     public byte[] getImage(string name)
     {
         string path = AppDomain.CurrentDomain.BaseDirectory;
         Bitmap image1 = (Bitmap)Image.FromFile(path + @"\imgs\" + name, true);
         MemoryStream ms = new MemoryStream();
         image1.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
         byte[] imgData = ms.ToArray();
         return imgData;

     }

     public MyImage [] getImageList()
     {
         MyImage[] images =
         {
             new MyImage (@"\imgs", "Download(11).jpg"),
               new MyImage (@"\imgs", "Download(12).jpg"),
                 new MyImage (@"\imgs", "Download(13).jpg"),
                   new MyImage (@"\imgs", "Download(14).jpg"),
                     new MyImage (@"\imgs", "Download(15).jpg"),
                       new MyImage (@"\imgs", "Download(16).jpg"),
                 new MyImage (@"\imgs", "Download(17).jpg"),
                   new MyImage (@"\imgs", "Download(18).jpg"),
         };
         return images;
     }
 }

public interface IPictureServer
{
MyImage[] getImageList();
byte[] getImage(string name);
}

}

dotnet-csharp
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

MikoKage avatar image
0 Votes"
MikoKage answered MikoKage published

Try this out for your client, and adjust it as you need it:
using Server;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Http;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

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

     private void Form1_Load_1(object sender, EventArgs e)
     {
         HttpChannel channel = new HttpChannel();
         ChannelServices.RegisterChannel(channel, false);
         Huans huans = (Huans)Activator.GetObject(typeof(Huans), "http://localhost:8090/Object");
         List<string> range = new List<string>();
         MyImage[] imageList = huans.getImageList();
         foreach (MyImage huan in imageList)
         {
             Console.WriteLine("pfad" + huan.name);
             range.Add(huan.name);
         }
         comboBox1.DataSource = range;
         Console.WriteLine(range.Count);
     }

     private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
     {
         Huans huans = (Huans)Activator.GetObject(typeof(Huans), "http://localhost:8090/Object");
         byte[] imgBytes = huans.getImage(comboBox1.SelectedItem.ToString());
         Console.WriteLine("Pfad" + imgBytes);
         System.Drawing.ImageConverter converter = new System.Drawing.ImageConverter();
         System.Drawing.Image img =(System.Drawing.Image)converter.
         ConvertFrom(imgBytes);
         pictureBox2.Image = img;
     }

     private void pictureBox2_Click(object sender, EventArgs e)
     {

     }

        
 }

}

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

TimonYang-MSFT avatar image
1 Vote"
TimonYang-MSFT answered

Do we have to use this method? Is it possible to use TCP to transfer files?

Server:

         static void Main(string[] args)
         {
             ReceiveTCP(8088);
         }
         private const int BufferSize = 1024;
         public static void ReceiveTCP(int port)
         {
             TcpListener Listener = null;
             try
             {
                 Listener = new TcpListener(IPAddress.Any, port);
                 Listener.Start();
             }
             catch (Exception ex)
             {
                 Console.WriteLine(ex.Message);
             }
             byte[] RecData = new byte[BufferSize];
             int RecBytes;
             for (; ; )
             {
                 TcpClient client = null;
                 NetworkStream netstream = null;
                 try
                 {
                     client = Listener.AcceptTcpClient();
                     netstream = client.GetStream();
                     Console.WriteLine("Connected to a client...");
    
                     string SaveFileName = @"C:\xxx\2.docx";
    
                     int totalrecbytes = 0;
                     using (FileStream Fs = new FileStream(SaveFileName, FileMode.OpenOrCreate, FileAccess.Write))
                     {
                         while ((RecBytes = netstream.Read(RecData, 0, RecData.Length)) > 0)
                         {
                             Fs.Write(RecData, 0, RecBytes);
                             totalrecbytes += RecBytes;
                         }
                     }  
                 }
                 catch (Exception ex)
                 {
                     Console.WriteLine(ex.Message);
                 }
                 finally 
                 {
                     netstream.Close();
                     client.Close();
                 }
             }
         }

Client:

        private const int BufferSize = 1024;
         static void Main(string[] args)
         {
             SendTCP(@"d:\xxx\test.docx", "localhost", 8088);
         }
         public static void SendTCP(string fileName, string ip, Int32 port)
         {
             byte[] SendingBuffer = null;
             TcpClient client = null;
             NetworkStream netstream = null;
             try
             {
                 client = new TcpClient(ip, port);
                 Console.WriteLine("Connected to the Server...");
                 netstream = client.GetStream();
                 FileStream Fs = new FileStream(fileName, FileMode.Open, FileAccess.Read);
                 int NoOfPackets = Convert.ToInt32
                (Math.Ceiling(Convert.ToDouble(Fs.Length) / Convert.ToDouble(BufferSize)));
                 int TotalLength = (int)Fs.Length, CurrentPacketLength;
                 for (int i = 0; i < NoOfPackets; i++)
                 {
                     if (TotalLength > BufferSize)
                     {
                         CurrentPacketLength = BufferSize;
                         TotalLength = TotalLength - CurrentPacketLength;
                     }
                     else
                         CurrentPacketLength = TotalLength;
                     SendingBuffer = new byte[CurrentPacketLength];
                     Fs.Read(SendingBuffer, 0, CurrentPacketLength);
                     netstream.Write(SendingBuffer, 0, (int)SendingBuffer.Length);
                 }
                 Console.WriteLine("Sent " + Fs.Length.ToString() + "bytes to the server");
                 Fs.Close();
             }
             catch (Exception ex)
             {
                 Console.WriteLine(ex.Message);
             }
             finally
             {
                 netstream.Close();
                 client.Close();
             }
         }

If the response is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

MikoKage avatar image
0 Votes"
MikoKage answered

I got something:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Mediaplayer
{
public partial class Form1 : Form
{
String path = null;
Timer t;
public Form1()
{
InitializeComponent();
axWindowsMediaPlayer1.uiMode = "none";
}

     private void Form1_Load(object sender, EventArgs e)
     {
         t = new Timer();
         t.Interval = 1000;
         t.Tick += new EventHandler(t_Tick);
     }

     void t_Tick(object sender, EventArgs e)
     {
         trackBar1.Value = (int) axWindowsMediaPlayer1.Ctlcontrols.currentPosition;
            
     }

     private void openButton_Click(object sender, EventArgs e)
     {
         OpenFileDialog dlg = new OpenFileDialog();
         dlg.ShowDialog();
         if(dlg.FileName != null)
         {
             path = dlg.FileName;
             axWindowsMediaPlayer1.URL = path;
             nameLabel.Text = axWindowsMediaPlayer1.currentMedia.name;
             trackBar1.Maximum++;
             t.Start();
         }
            
     }

     private void playButton_Click(object sender, EventArgs e)
     {
         axWindowsMediaPlayer1.Ctlcontrols.play();
         t.Start();
     }

     private void stopButton_Click(object sender, EventArgs e)
     {
         axWindowsMediaPlayer1.Ctlcontrols.pause();
         t.Stop();
     }
 }

Screenshot:
131566-eede.png



eede.png (14.5 KiB)
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

MikoKage avatar image
1 Vote"
MikoKage answered

Ok I got the newest solution:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace MediaPlayer
{
public partial class Form1 : Form
{
Timer t;
public Form1()
{
InitializeComponent();
axWindowsMediaPlayer1.uiMode = "none";
t = new Timer();
t.Interval = 1000;
t.Tick += new EventHandler (t_Tick);
}

     void t_Tick(object sender, EventArgs e)
     {
         trackBar1.Value = (int)axWindowsMediaPlayer1.Ctlcontrols.currentPosition;
         label1.Text = axWindowsMediaPlayer1.Ctlcontrols.currentPosition.ToString();
     }

     private void button1_Click(object sender, EventArgs e)
     {
         OpenFileDialog dlg = new OpenFileDialog();
         dlg.ShowDialog();

         if(dlg.FileName != null)
         {
             axWindowsMediaPlayer1.URL = dlg.FileName;
         }
     }

     private void button2_Click(object sender, EventArgs e)
     {
         axWindowsMediaPlayer1.Ctlcontrols.play();
         t.Start();

     }

     private void button3_Click(object sender, EventArgs e)
     {
         axWindowsMediaPlayer1.Ctlcontrols.pause();
         t.Stop();
     }

     private void axWindowsMediaPlayer1_PlayStateChange(object sender, AxWMPLib._WMPOCXEvents_PlayStateChangeEvent e)
     {
         if(axWindowsMediaPlayer1.currentMedia != null)
         {
             trackBar1.Maximum = (int)Math.Round(axWindowsMediaPlayer1.currentMedia.duration);
             t.Start();
         }
            
     }

     private void trackBar1_Scroll(object sender, EventArgs e)
     {
         axWindowsMediaPlayer1.Ctlcontrols.currentPosition = trackBar1.Value;
     }
 }

}
131528-eede.png



eede.png (21.6 KiB)
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.