IIRLaborRemoting

A VIK Wikiből

Ez az oldal a korábbi SCH wikiről lett áthozva.

Ha úgy érzed, hogy bármilyen formázási vagy tartalmi probléma van vele, akkor, kérlek, javíts rajta egy rövid szerkesztéssel!

Ha nem tudod, hogyan indulj el, olvasd el a migrálási útmutatót.


<noautolink>

Labvezér: Rajacsics Tamás


A laboron megírt forráskódok

RemotingCommon / ICalc.cs

using System;
using System.Collections.Generic;
using System.Text;

namespace RemotingCommon2
{
	 public interface ICalc
	 {
		  int Add(int a, int b);
	 }
}


RemotingServer / Calc.cs

using System;
using System.Collections.Generic;
using System.Text;
using RemotingCommon2;

namespace RemotingServer
{
	 class Calc : MarshalByRefObject,ICalc
	 {
		  public int Add(int a, int b)
		  {
				return a + b;
		  }

		  public override object InitializeLifetimeService()
		  {
				// sosem szabad törölni, mert nincs életcikluskezelője
				return null;
		  }

	 }
}


RemotingServer / TextServer.cs

using System;
using System.Collections.Generic;
using System.Text;
using System.Net.Sockets;
using System.Net;

namespace RemotingServer
{
	 class TextServer : IDisposable
	 {
		  UdpClient connection;

		  public TextServer()
		  {
				connection = new UdpClient(9001);
		  }

		  public void OnTimer()
		  {
				if (connection.Available == 0)
					 return;
				IPEndPoint from = new IPEndPoint(IPAddress.Any, 0);
				byte[] packet = connection.Receive(ref from);
				// ha nem adom  meg, hogy hova menjen, akkor oda küldené, ahova korábban
				// a connectben megadtam neki
				packet[2] = (byte)(packet[0] + packet[1]);
				connection.Send(packet, packet.Length, from);
		  }

		  public void Dispose()
		  {
				connection.Close();
		  }
	 }
}


RemotingServer / Program.cs

using System;
using System.Collections.Generic;
using System.Windows.Forms;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;


namespace RemotingServer
{
	 static class Program
	 {
		  /// <summary>
		  /// The main entry point for the application.
		  /// </summary>
		  [STAThread]
		  static void Main()
		  {
				Application.EnableVisualStyles();
				Application.SetCompatibleTextRenderingDefault(false);

				Calc calc = new Calc();
				RemotingServices.Marshal(calc, "Tibi");
				ChannelServices.RegisterChannel(new TcpChannel(9000), false);



				Application.Run(new Form1());
		  }
	 }
}


RemotingServer / Form1.cs

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace RemotingServer
{
	 public partial class Form1 : Form
	 {
		  TextServer ts = new TextServer();
		  public Form1()
		  {
				InitializeComponent();
		  }

		  private void timer_Tick(object sender, EventArgs e)
		  {
				ts.OnTimer();
		  }
	 }
}


RemotingClient / TSClient.cs

using System;
using System.Collections.Generic;
using System.Text;
using RemotingCommon2;
using System.Runtime.Remoting.Proxies;
using System.Windows.Forms;
using System.Runtime.Remoting.Messaging;
using System.Net.Sockets;
using System.Net;

namespace RemotingClient
{
	 class TSClient : RealProxy
	 {
		  public TSClient() : base (typeof(ICalc))
		  {

		  }

		  public ICalc GetRemoteObject()
		  {
				return (ICalc) GetTransparentProxy();
		  }

		  public override IMessage Invoke(IMessage msg)
		  {
				MethodCall call = new MethodCall(msg);
				UdpClient client = new UdpClient(Dns.GetHostName(), 9001);
				client.Send(new byte[] { (byte)(int)call.InArgs[0], (byte)(int)call.InArgs[1], 0 }, 3);
				IPEndPoint ep = new IPEndPoint(IPAddress.Any, 0);
				byte[] ret = client.Receive(ref ep);

				MessageBox.Show(ret[2].ToString());
				return new ReturnMessage((int)ret[2], null, 0, null, call);
		  }


	 }
}


RemotingClient / Form1.cs

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using RemotingCommon2;

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

		  private void addbutton_Click(object sender, EventArgs e)
		  {
				ICalc obj = (ICalc) Activator.GetObject(typeof(ICalc), "tcp://localhost:9000/Tibi");
				textBox3.Text = obj.Add(int.Parse(txtA.Text), int.Parse(txtB.Text)).ToString();
		  }

		  private void button1_Click(object sender, EventArgs e)
		  {
				TSClient c = new TSClient();
				c.GetRemoteObject().Add(int.Parse(txtA.Text), int.Parse(txtB.Text)).ToString();
		  }
	 }
}


-- palacsint - 2007.10.23.