Notice
Recent Posts
Recent Comments
관리 메뉴

안까먹을라고 쓰는 블로그

[C#] IPC(Inter Process Communication) - 프로세스간 통신 본문

Language/C#

[C#] IPC(Inter Process Communication) - 프로세스간 통신

YawnsDuzin 2012. 10. 29. 13:31

 

반응형

프로젝트 네임

- IPC Client : IPC_Client

- IPC Server : IPC_Server

- IPC Object : IPC_RemoteObject



IPC_Example.zip



IPC_Server





 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
 
using System.Runtime.Remoting.Channels.Ipc;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting;
using IPC_RemoteObject;
 
namespace IPC_Server
{
    public partial class frmIPC_Server : Form
    {
        IpcServerChannel ServerChannel;
        RemoteObject ro;
 
        public frmIPC_Server()
        {
            InitializeComponent();
        }
 
        private void frmIPC_Server_Load(object sender, EventArgs e)
        {
            ServerChannel = new IpcServerChannel("remote");
            ChannelServices.RegisterChannel(ServerChannel, false);
            RemotingConfiguration.RegisterWellKnownServiceType(typeof(RemoteObject), "Cnt", WellKnownObjectMode.Singleton);
 
            this.textBox1.AppendText("Listening on " + ServerChannel.GetChannelUri() + Environment.NewLine);
            ro = new RemoteObject();
            ro.SetCount(1);
        }
 
        private void btnDown_Click(object sender, EventArgs e)
        {
            ro.SetCount(ro.GetCount() - 1);
            this.textBox1.AppendText("Cnt Get : " + ro.GetCount() + Environment.NewLine);
        }
        
        private void btnUp_Click(object sender, EventArgs e)
        {
            ro.SetCount(ro.GetCount() + 1);
            this.textBox1.AppendText("Cnt Get : " + ro.GetCount() + Environment.NewLine);
        }
        
        private void btnGet_Click(object sender, EventArgs e)
        {
            this.textBox1.AppendText("Cnt Get : " + ro.GetCount() + Environment.NewLine);
        }
    }
}
 
 
cs



IPC_Client





 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
 
using System.Runtime.Remoting.Channels.Ipc;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting;
using IPC_RemoteObject;
 
namespace IPC_Client
{
    [SecurityPermission(SecurityAction.Demand)]
 
    public partial class frmIPC_Client : Form
    {
        IpcClientChannel ClientChannel;
        RemoteObject ro;
 
        Timer Tmr = new Timer();
        int CheckCnt = 0;
        int TempCnt = 0;
 
        public frmIPC_Client()
        {
            InitializeComponent();
        }
 
        private void frmIPC_Client_Load(object sender, EventArgs e)
        {
            try
            {
                ClientChannel = new IpcClientChannel();
                ChannelServices.RegisterChannel(ClientChannel, false);
                RemotingConfiguration.RegisterWellKnownClientType(typeof(RemoteObject), "ipc://remote/Cnt");
                ro = new RemoteObject();
 
                Tmr.Interval = 1000;
                Tmr.Enabled = true;
 
                Tmr.Tick += new EventHandler(Tmr_Tick);
                CheckCnt = ro.GetCount();
            }
            catch
            {
            }            
        }
        
        void Tmr_Tick(object sender, EventArgs e)
        {
            try
            {
                TempCnt = CheckCnt;
 
                if (CheckCnt != ro.GetCount())
                {
                    CheckCnt = ro.GetCount();
                    this.textBox1.AppendText("Data Change - Before : " + TempCnt + ", Now : " + ro.GetCount() + Environment.NewLine);
                }
            }
            catch
            {
            }
        }
        
        private void btnDown_Click(object sender, EventArgs e)
        {
            ro.SetCount(ro.GetCount() - 1);
            this.textBox1.AppendText("Cnt Get : " + ro.GetCount() + Environment.NewLine);
        }
        
        private void btnUp_Click(object sender, EventArgs e)
        {
            ro.SetCount(ro.GetCount() + 1);
            this.textBox1.AppendText("Cnt Get : " + ro.GetCount() + Environment.NewLine);
        }
 
        private void btnGet_Click(object sender, EventArgs e)
        {
            this.textBox1.AppendText("Cnt Get : " + ro.GetCount() + Environment.NewLine);
        }
    }
}
cs




IPC_RemoteObject

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
 
namespace IPC_RemoteObject
{
    public class RemoteObject : MarshalByRefObject
    {
        private static int Count = 0;
 
        public int GetCount()
        {
            return (Count);
        }
 
        public void SetCount(int cnt)
        {
            Count = cnt;
        }
    }
}
cs

 


반응형

'Language > C#' 카테고리의 다른 글

[C#] 부모폼과 자식폼의 참조  (0) 2012.10.30
[C#] IPC / RPC 통신  (0) 2012.10.29
[C#] Timer 배열로 사용하기  (0) 2012.10.05
[C#] Network Ping Test  (0) 2012.09.11
[C#] 시스템 종료 / 재시작  (0) 2012.09.11
Comments