Click here to Skip to main content
15,434,147 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
how to create a program using c# windows form that takes input from one computer and separately ouputs on another computer..
example.
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]@yahoo.com
hoping for a quick response.. :)

[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
Updated 12-Mar-12 20:57pm
v2
Comments
Sergey Alexandrovich Kryukov 13-Mar-12 0:29am     CRLF
What have you done so far? What's the problem? --SA
Sergey Alexandrovich Kryukov 13-Mar-12 0:36am     CRLF
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. --SA
kebeen 15-Mar-12 3:28am     CRLF
of course, i would like my program to handle as many client. and if a client disconnects, then it is simply disconnected..
Sergey Alexandrovich Kryukov 15-Mar-12 21:49pm     CRLF
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. --SA

Try this: Multithreaded Chat Server[^]

It probably isn't exactly what you want, but it covers most of the basic needs and functions.
 
Share this answer
 
[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:
http://msdn.microsoft.com/en-us/library/system.windows.forms.control.invokeonclick.aspx[^].

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:
C#
void ProcessMyButtonClick() { /* you don't need any parameters ... */ }
//...

myButton.Click += (sender, eventArgs) => {
   ProcessMyButtonClick();
   // 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:

C#
myButton.Click += (object sender, System.EventArgs eventArgs) {
   ProcessMyButtonClick();
   // 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:
http://en.wikipedia.org/wiki/Loose_coupling[^].

I suggest you learn and analyze applicability of the following architectural patterns (http://en.wikipedia.org/wiki/Architectural_pattern_(computer_science)[^]):

MVVM — Model View View Model,
http://en.wikipedia.org/wiki/Model_View_ViewModel[^],

MVC — Model-View-Controller,
http://en.wikipedia.org/wiki/Model-view-controller[^]),

MVA — Model-View-Adapter,
http://en.wikipedia.org/wiki/Model–view–adapter[^],

MVP — Model-View-Presenter,
http://en.wikipedia.org/wiki/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,
—SA
 
Share this answer
 
v2
Please see my past answer for some important ideas:
Multple clients from same port Number[^].

[EDIT]

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

Solution 4

—SA
 
Share this answer
 
v3
Comments
kebeen 16-Mar-12 22:17pm     CRLF
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??
Sergey Alexandrovich Kryukov 16-Mar-12 22:28pm     CRLF
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. --SA
kebeen 16-Mar-12 23:07pm     CRLF
thanks for the quick reply.. anyway.. can you recommend downloadable e-books for socket programming?
Sergey Alexandrovich Kryukov 17-Mar-12 0:03am     CRLF
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... --SA
kebeen 18-Mar-12 2:05am     CRLF
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??

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900