Click here to Skip to main content
11,632,778 members (86,539 online)
Rate this: bad
Please Sign up or sign in to vote.
how to create a program using c# windows form that takes input from one computer and separately ouputs on another computer..
i have 4 computers namely.. com1, com2, com3, com4..
com1 is the server and the others the client..
when com2 inputs, the input is then shown on com1 separately say textbox1.
when com3 inputs, the input is then shown on com1 separately say textbox2
when com4 inputs, the input is then shown on com1 separately say textbox3.

thank you for your kind reply..
can email me at [DELETED]
hoping for a quick response.. Smile | :)

[edit]Never post your email address in any forum, unless you really like spam! If anyone replies to you, you will receive an email to let you know - OriginalGriff[/edit]
Posted 12-Mar-12 18:25pm
Edited 12-Mar-12 20:57pm
SAKryukov at 13-Mar-12 0:29am
What have you done so far? What's the problem?
SAKryukov at 13-Mar-12 0:36am
Aha, and when computer #5 is connected (or another client on any of com2... com4 is connected), what happens? server crashes? :-)
Very unrealistic scenario. First, for a server, it's absolutely does not matter a client on which computer has connected, could be 2 or 3 on one computer, 1 on another -- anything. Second, number of connected clients can be different. How do you want to identify those client computers? Usually this is not needed. I'm not asking about technique, what do you want to require? What happens if a client disconnects? Everything should be defined.
kebeen at 15-Mar-12 3:28am
of course, i would like my program to handle as many client.
and if a client disconnects, then it is simply disconnected..
SAKryukov at 15-Mar-12 21:49pm
You need two threads on server side, one waiting for new connection, another one for reading/writing from/to network stream. Shorter: see my past answer I referenced below.
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

Try this: Multithreaded Chat Server[^]

It probably isn't exactly what you want, but it covers most of the basic needs and functions.
Rate this: bad
Please Sign up or sign in to vote.

Solution 4

[Answering a follow-up question in the comments to Solution 3]

The question was about simulation of an event like a click event.

Technically, a event can be invoked only in a class where the event is declared. It is not possible to do anywhere else, not even it the derived class. This is a one of the limitations of event instances, in contrast to "regular" delegate instances, an important fool-proof feature of events.

Please see my recent answer on this topic:
Since we have multicast delegates, why do we need events?[^].

Take for example the class System.Windows.Forms.Control. It has the method InvokeOnClick which actually invokes the Click event, please see:[^].

This way, one can directly invoke the event in the base class, but only because of this method which was exposed to the derived classes (and only for them, through the protected access modifier) to provide some back door to simulation of the click. To use it, one should create a derived class from any of the control classes.

Conceptually, this is possible but redundant. In real development, you don't need an event itself, not an actual click. You really need to call some method normally called when an event is invoked. So, this is exactly what you could do (instead of awkward sub-classing and using System.Windows.Forms.Control.InvokeOnClick:
void ProcessMyButtonClick() { /* you don't need any parameters ... */ }

myButton.Click += (sender, eventArgs) => {
   // note that you don't need event handler parameters;
   // in other cases, you need only one of them or both...

// and you can have the exact same effect if you call it elsewhere:
ProcessMyButtonClick(); //that's all; this is a regular method, call it a will!

The code shown above also shows the benefits of anonymous event handler, in the lambda form, which also allowed for type inference (note that the types of parameters are not specified, but compiler takes them from the delegate type of the event instance Click, this is the delegate System.EventHandler<System.EventArgs>).

For C# v.2, when neither lambda nor type inference were introduced, the syntax of adding an event handler would be a bit longer, but still using anonymous event handler:

myButton.Click += (object sender, System.EventArgs eventArgs) {
   // note that you don't need event handler parameters;
   // in other cases, you need only one of them or both...

I'm sure that .NET prior v.2.0 and C# prior to v.2 should not be ever considered.

Now, the original question was about invocation of event between different host linked via network. After the above explanations, is should be clear that effectively the problem is reduced to calling the same method like ProcessMyButtonClick in response to some command sent via network.

I would note, I don't see a whole lot of sense in it. The network communication should be implemented in a separate communication layer and isolated from the UI. It could be semantic or very abstract, not exchanging some logical data and commands. You need loose coupling, as opposed to strong coupling; please see:[^].

I suggest you learn and analyze applicability of the following architectural patterns ([^]):

MVVM — Model View View Model,[^],

MVC — Model-View-Controller,[^]),

MVA — Model-View-Adapter,–view–adapter[^],

MVP — Model-View-Presenter,[^].

Pay attention for the motivation of those architectures. If you understand it, you would be able to create better design ideas.

Good luck,
Rate this: bad
Please Sign up or sign in to vote.

Solution 3

Please see my past answer for some important ideas:
Multple clients from same port Number[^].


Please see my reply in response to the follow-up discussion:

Solution 4

kebeen at 16-Mar-12 22:17pm
im really really new to c# programming..

i would like to know how the forms are connected(over the LAN) in the network and how to pass values from one form(client) to the other form(server).. can you give examples??
SAKryukov at 16-Mar-12 22:28pm
Forms are not connected, normally. Applications are connected. You should not communicate between forms, you need to have a separate communication layers, and it needs at least two additional threads in server side and at least one additional thread in client side. If you are not comfortable with C# in general and threads as well, perhaps you are not quite ready to attack socket-level networking. Anyway, it's pretty hard to explain in one quick answer. It would take at least one article of decent size. There are many code samples around though. Perhaps a code sample in "Multithreaded Chat Server" (see the answer by OriginalGriff) is good enough.

You also can use networking on other levels, such as classical remoting or WCF (self-hosted). Perhaps it could be easier in basic cases. The approach is different, it mimics using of an instance of some class, only remote one. I would recommend to learn socket programming anyway.

kebeen at 16-Mar-12 23:07pm
thanks for the quick reply..
anyway.. can you recommend downloadable e-books for socket programming?
SAKryukov at 17-Mar-12 0:03am
Oh... sorry, no. I learned is so long time ago and later just used my knowledge on different platforms only using help pages (like MSDN) just for reference, as the ideas are pretty much the same. MSDN is good enough so I would simply use MDSN; it usually provides pretty clear introductory material, code samples, the navigation is not bad, too...
kebeen at 18-Mar-12 2:05am
i already connected the forms!!! wuhoo!! thanks for your replies..
just one more thing i dont know.. i want my client_form to execute an event(e.g. click a button) from my server form.. how do i do that??
SAKryukov at 18-Mar-12 14:11pm
There is no way to invoke any event anywhere expect the class where an event instance is declared (not even in a derived class, forget about remote object). This is an important fool-proof feature. Also, it is never needed. Create a separate method, call it in different places: one in the event handler, others elsewhere. In particular, call it in in response to some command sent through network channel.
kebeen at 21-Mar-12 21:51pm
i dont get it.. can you give more examples perhaps??
SAKryukov at 18-Mar-12 14:14pm
I tell you, do not connect the forms. Introduce some layers in your applications, make communication logical, not UI-wise. Use loose coupling... I'm afraid, this is a long talking on architecture and design...
kebeen at 18-Mar-12 1:01am
also, how can i resize my textbox when resizing my form?

the form is the only one that is resized, but the other controls in the form stay as it is.
SAKryukov at 18-Mar-12 14:13pm
It has the property Width and Height, use them. However, in correctly designed form layout, it is not needed. You should use hierarchy of panels, docking and padding (properties Dock and Padding). This way, all controls will resize/move according to the form size automatically and without flicker.
SAKryukov at 18-Mar-12 14:15pm
Will you consider to accept this answer formally after all (green button) -- thanks.
kebeen at 21-Mar-12 21:45pm
yeah.. thanks for the replies.. your help is really very helpful..
kebeen at 21-Mar-12 21:49pm
so there is no way in executing an event(e.g. clicking a button)in the other forms in the network??
kebeen at 21-Mar-12 22:09pm
its because i want to control the clients form to be enabled,i.e. since it is a quiz bee, the forms of the client should be simultaneously enabled that is controlled from the server
SAKryukov at 21-Mar-12 22:30pm
(Sigh...) Well, this is a separate question, a pretty big one.
Please see my new answer on this page.

See the update above, after [EDIT]

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 8,618
1 Sergey Alexandrovich Kryukov 8,189
2 Mika Wendelius 6,415
3 F-ES Sitecore 2,321
4 Suvendu Shekhar Giri 2,075

Advertise | Privacy | Mobile
Web01 | 2.8.150728.1 | Last Updated 21 Mar 2012
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100