Click here to Skip to main content
15,886,919 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Hello my friends
I have a console code and I want to convert it to windows forms application in order to build a project for me
Please help me
This is the code:

C#
/*   Server Program    */
                 
using System;
using System.Text;
using System.Net;
using System.Net.Sockets;

public class serv {
    public static void Main() {
    try {
        IPAddress ipAd = IPAddress.Parse("172.21.5.99");
         // use local m/c IP address, and 
         // use the same in the client

/* Initializes the Listener */
        TcpListener myList=new TcpListener(ipAd,8001);

/* Start Listeneting at the specified port */        
        myList.Start();
        
        Console.WriteLine("The server is running at port 8001...");    
        Console.WriteLine("The local End point is  :" + 
                          myList.LocalEndpoint );
        Console.WriteLine("Waiting for a connection.....");
        
        Socket s=myList.AcceptSocket();
        Console.WriteLine("Connection accepted from " + s.RemoteEndPoint);
        
        byte[] b=new byte[100];
        int k=s.Receive(b);
        Console.WriteLine("Recieved...");
        for (int i=0;i<k;i++)
            Console.Write(Convert.ToChar(b[i]));

        ASCIIEncoding asen=new ASCIIEncoding();
        s.Send(asen.GetBytes("The string was recieved by the server."));
        Console.WriteLine("\nSent Acknowledgement");
/* clean up */            
        s.Close();
        myList.Stop();
            
    }
    catch (Exception e) {
        Console.WriteLine("Error..... " + e.StackTrace);
    }    
    }
    
}


What I have tried:

I tried searching Google for a solution but to no avail
Posted
Updated 29-Mar-18 4:21am
v2
Comments
F-ES Sitecore 29-Mar-18 8:30am    
You can't convert from console to windows forms, you're going to have to create a winforms project and copy the relavnt bits of code into the relevent events. For outputting you'll probably need to add something to the form that you can write to. Now that you've gone from single-threaded console to multi-threaded forms you'll probably need to implement some multi-threading also.

In your case you need to consider a worker thread, I would recommend for simplicity BackgroundWorker-Klasse .

The point is, your GUI will freeze by such blocking action like you do:

CSS
/* Initializes the Listener */
        TcpListener myList=new TcpListener(ipAd,8001);

/* Start Listeneting at the specified port */        
        myList.Start();
 
Share this answer
 
Basically, don't.
Console applications are intended to run in the older, traditional method and are called "procedural applications": the code tells the user what to do and is in control at all times.
Windows applications don't: they respond to events (of which there are a huge number) and do things as a result of that - the user is (at least nominally) in charge and can do anything he wants in whatever sequence suits him.

So where a Console app will go:
"Enter name:" 
"Enter address:"
"Insert to database? (Y/N): "
A windows application would have two textboxes and an "Insert" button - and the user can fill in what he wants and press the button when he is ready.

So converting a Console app to Windows isn't simple, and doesn't end up with a "good" windows app - and in your case what it ends up with is a very frustrating experience for the user as he can't tell if the app had crashed or is working because most of what the code is doing is executing what are called "blocking calls"; ones which do not return until the operation is fully complete, which can take between seconds and forever!

To use that code in a Windows app that won't get uninstalled with extreme prejudice is extremely complicated: you will need to learn about threading, Invoking, and about how to get and present information to the user.

Basically, if you want a Windows chat app to play with, go find a Windows chat app - there are plenty of them - you do not want to start from a console version!
 
Share this answer
 

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