Early 20th Italy was monarchy, then how socialists and communists get legal position?
Italian Socialist Party was even largest party at 1919 general election.
I wonder why Italian government does not illegalized them at that time.
They did (try) - from time to time. But why would they?
One might be tempted to say that outlawing a party which holds about 30% of the electoral basin would have likely caused some major issues anyways... This argument loses much of its weight when you observe that the PSI and PCdI were actually outlawed without major inconveniences after a long and debilitating campaign of violence, political and economical pressure a few years into the establishment of Mussolini's Fascist Ministry.
Which brings us to the original question: why would they? Without invoking the fact that there are many monarchies which do not and have not - to the best of my knowledge - outlawed any socialist party as of recent (in so far as constitutional, "democratic" monarchies go, Italy was still an extremely flawed one at best, even after the introduction of universal male suffrage in 1919), there was no particular sentiment that "socialism", by and large, represented such a fundamental threat for Italy's society as to warrant a preemptive attempt at armed suppression.
In the aftermath of the unification Italy was a mostly agrarian country, the economy of which was caracterized by an (over)abundance of workforce and a tendency towards low salaries. If you examine the early documents of "political" labor unrest, you'll find such things as strikes for the introduction of a maximum of 16-18 hours workday. It was difficult - even for the staunchest conservatives and supporters of strict liberal (liberist) policies - not to admit that the conditions of industrial and agrarian workers were often miserable and, in fact, by themselves a major cause of social unrest. Intellectuals like Pasquale Villari, Francesco de Sanctis; politicians like Sidney Sonnino (himself a supporter of a semi-authoritarian interpretation of the Statute) denounced the conditions of the southern populace in the aftermath of the "Brigandage war" and the failure of the Italian post unitary governments to provide the necessary impulse for the improvement befitting a civilized and developing European nation (Villari, if I am not mistaken, had been an exile in London and usually cited - without a hint of sarcasm - the British poor houses as vastly superior environments in comparison to the lives of the southern agrarian and urban plebs).
Nor was the situation in the less developed north-eastern regions much better, if one of the main elements favoring the growth of Catholic social movements in the region was the campaign for the demolition of the large common houses - home of many impoverished agrarian families, and ridden with filth and disease - and their replacement with individual-family housing.
Where post-unitary labor structure transformations had been more intense, there social conflict often appeared to take a more "political" character - the Po Valley with its proletarized land-workers and their fledgling organizations; the industrial centers of Turin, Milan and Genoa - all saw the birth of the first semi-clandestine socialist, anarchist and republican associations, the inspirations of which varied, from Marx to Bakunin, from Preudhon to Mazzini, mostly coalescing around a form of "social-reformism" tinted with millenarian notes.
A thorough examination of the history of the Italian socialist movement and of the fortunes of the various currents of international socialism is probably beyond the scope of this answer. There was nonetheless a general perception - albeit far from universal - that socialism, in its "positive", uplifting, action among the masses represented a natural and almost necessary corollary of social and political development (there is an extensive amount of literature on the role of socialist organizations in shaping and originating new modes of relation between the individuals and their social and political environment - see for instance the works of M. Isnenghi); and that it were the "negative" destructive impulses which the State needed to keep firmly under control. After all, socialist parties existed in the UK, in Germany and France as well - with the SPD often cited as a, somewhat inaccurate, blueprint for the Italian socialist party, when it was established in 1892.
This doesn't mean that no-one regarded socialism as a more fundamental threat - ultimately irreconcilable with the social and political structures of liberal Italy, and openly or covertly at work to undermine the State's ethical, economical and social foundations. As prime minister Francesco Crispi - not far before leading the last frontal assault against the socialist movement and championing its de facto outlawing during the mid 1890s - declared, it was time for the loyal citizens of Italy to undertake the fight against a people who knew "no God, no King, no Nation". And cultural and societal norms were at play as well. When Giolitti - after the assassination of the somewhat authoritarian King Umberto I by anarchist Gaetano Bresci - promoted a sort of "institutionalization" of the socialist movement, with a series of half-hearted openings to its reformist wing for support to a liberal "democratic" government, the new King Vittorio Emanuele III eventually accepted to meet with a socialist delegation, as was customary for parliament forces (an office with the socialists had, always, declined before), including the man who had assisted his father's murdered in his trial. The so called - at the time at least - "democratic" King had a somewhat more modern understanding of his role than his father had had; the Queen mother though, allegedly berated her son for hours when she heard the news and according to persistent rumors never truly forgave him.
R. Vivarelli - who has studied in some detail the transition period between pre-war Italian society and the period of social unrest leading to the affirmation of fascism in the 1920s - lists a few noteworthy episodes. As the praxis had shifted - under Giolitti and furthermore under Nitti - towards a more "impartial" attitude of the local authorities and central government in matters of economical conflict between labor and owners, one land owner from the agrarian regions of Puglia complained to the prefect that had invited him to meet with a delegation of the workers, that he would accept that the law mandated for the prefect to deal with the peasant as his peers, but that asking him to sit at their table was an insult to his personal dignity and that he would not suffer that.
All these considerations notwithstanding, and aside for the structurally ambiguous relations between organized socialism and Italian State, up until the Bolshevik revolution and the perception of an imminent - and very real - threat of social upheaval, there wasn't really a strong support for outlawing the Socialist Party alltogether (and that includes the war period, when the Italian socialists maintained some form of "neutralist" position throughout). Rather you have a series of shades, from a sincere praise of the positive effects of socialism on the masses (education, organization, morals, etc.) to an almost explicit design of neutering and subverting the fundamentals of socialist proposals into an additional instrument of control of the masses. Socialists themselves operated within these two extremes - and within the wider framework of the structurally revolutionary historical process.
Even after the October Revolution these ambiguities continued to exist and operate - and played a significant role in the formation of the new Communist Party in 1921 - but it took years, and the substantial depletion and dissolution of the socialist organization and popular bases, before the Fascist Government choose to move forward with the explicit outlawing of the Socialist Party (as well as of any other party). An act that certainly made life harder for the few remaining active opponents of Fascism, but that - in so far as Italian politics went - served as the end, and not the beginning, of the Regime's anti-Bolshevik and anti-socialist campaign.
For general reference, in addition to those mentioned already:
Arfè, G. - Storia del socialismo italiano 1892-1926
Spriano, P. - Socialismo e classe operaia a Torino dal 1892 al 1913
Spriano, P. - Storia del Partito Comunista Italiano
Candeloro, G. - Il movimento cattolico in Italia
Candeloro, G. - Storia dell'Italia moderna
De Rosa, G. - Storia del movimento cattolico
AA.VV. - Storia della Emilia Romagna, vol. 3