in case of any errors ( like when the host don't accept your connection request) We need 3 specific event to use : (Error,ConnectEvent,DataArrival) To do that you must say to the system what event you want to control and handle. This is the part that you must control the event that happen to the Wnsock. MessageBox.Show("You are not connect to any host ") Ĭatch ( g) after we send the data we clear the textbox this will send the data that we wrote in SendText textboxĭataInput.Text = "\nClent(You -) : " SendText.Text Private void Send_Click(object sender, EventArgs e) with try - catch we take all the exception that maybe come and write it to the screenĬatch (.InvalidActiveXStateException g) if the server accept the connection the w1_connect event is raised (BUT client and host MUST use the same port ) in portText the user is write the host port in ipText the user is write the host ip W1.Close() // close the winsock in case it was open Private void Connect_Click(object sender,EventArgs e) " item from the menu bar and find "Microsoft Winsock Control 6.0", Check it and click ok.Īnd now the CODE!!! of the forms componentsīoolean isConnect= false // we use this Boolean to know if we are connected Remember that you need to add Winsock COM component control to your program, to do this right click on the toolbar that is left to the form ( where buttons ,labels. Now we have the form but it has no code inside, it's only the components. What i am going to show you is simply 2 applications connecting together, and sending/receiving text strings. Most common is for the servers to be able to receive more than one connections from different clients These is called multithreaded socket servers, but I am not going to show you how they are made in this tutorial, just to keep things simple. For example, when you connect to Google with your firefox ( or internet explorer ), your browser plays the role of the client that connects to the hosts that are running at Google. The server will be the program that opens the ports on the hosting machine and receive the connections while the client is called the program that connects to the remote host. To connect any 2 programs, you need at least one server and one client. You WILL NOT have to deal with that stuff using the Winsock control from C#(or any other language) Continue reading to see just how easy it is to effectively use Winsock.įor this tutorial you will only need a computer running Windows ,Microsoft Visual Studio 2005, and the will to learn! In order to do such complicated things a lot of in formation like headers, packet size ,hashes ,packet order and many more are required to create the packets. Packets are constructed by the softwares that are then being send through routers and others are being received by your Operating System and analyzed by the application that send it. When using the internet ( like from a web browser ) a lot of things happen behind the scenes. The Winsock we are going to use is an COM (References) that we can add in our C# program so we can use it's features. The chat program will be just a server and a client, that you can connect from the internet ( or LAN ) and simply exchange text messages. In this article we will see how to create a simple chat program. This is the beginning of everything in the computer of today's !!! The net is the most power tools of the programmers and you must learning to use it. We use Winsock to establish connection between client and server.
0 Comments
Leave a Reply. |
Details
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |