Welcome to Dream.In.Code
Getting C# Help is Easy!

Join 132,679 C# Programmers for FREE! Get instant access to thousands of C# experts, tutorials, code snippets, and more! There are 1,187 people online right now. Registration is fast and FREE... Join Now!




changing code from console application to windows plllllz reply

 
Reply to this topicStart new topic

changing code from console application to windows plllllz reply

hiphop_13
post 25 Jul, 2007 - 12:35 AM
Post #1


New D.I.C Head

*
Joined: 4 Jun, 2007
Posts: 36



Thanked 1 times
My Contributions


hiii
well my purpose is to create a GUI server. I have the code for the console server application, i just want to change it to windows application.
( I want the server to read the text from a richtextbox text entered by user on button click and not from console as seen in example.)


Console server
==========

CODE
namespace ConsoleApplication5
{
    using System;
    using System.Net.Sockets;
    using System.IO;

    public class Echoserver
    {
        //entry point of main method....
        public static void Main()
        {
            //TcpListener is listening on the given port...{
            TcpListener tcpListener = new TcpListener(System.Net.IPAddress.Parse("127.0.0.1"), 1234);
            tcpListener.Start();
            Console.WriteLine("Server Started");
            //Accepts a new connection...
            Socket socketForClient = tcpListener.AcceptSocket();
            //StreamWriter and StreamReader Classes for reading and writing the data to and fro.
            //The server reads the meassage sent by the Client ,converts it to upper case and sends it back to the client.
            //Lastly close all the streams.
            try
            {
                if (socketForClient.Connected)
                {
                    while (true)
                    {
                        Console.WriteLine("Client connected");
                        NetworkStream networkStream = new NetworkStream(socketForClient);
                        StreamWriter streamWriter = new StreamWriter(networkStream);
                        StreamReader streamReader = new StreamReader(networkStream);
                        string line = streamReader.ReadLine();
                        Console.WriteLine("Read:" + line);
                        line =Console.ReadLine();//here i want to
//take text from user input through richtextbox on buton
//clik
                        streamWriter.WriteLine(line);
                        Console.WriteLine("Wrote:" + line);
                        streamWriter.Flush();
                    }
                }
                socketForClient.Close();
                Console.WriteLine("Exiting...");
            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
            }
        }
    }
}


thanku for help:)

This post has been edited by hiphop_13: 25 Jul, 2007 - 03:18 AM
User is offlineProfile CardPM

Go to the top of the page

Martyr2
post 26 Jul, 2007 - 11:48 AM
Post #2


Programming Theoretician

Group Icon
Joined: 18 Apr, 2007
Posts: 5,062



Thanked 175 times

Expert In: C/C++, Java, VB, VB.NET, C#, PHP, Web Development, HTML & CSS, Javascript

My Contributions


Hi,

Sometimes this type of thing can be a bit confusing, but the only real work you have to do is take the code from your console application, break it up into the proper event handlers and change where some of the output and input goto and come from respectively.

So start by creating a new windows application project. This will generate your GUI style application. Second you will need to take out most of the code statements from the main() method and decide where do you want to establish the listening. Do you want it to start listening when you start the application? If so, you could put this in the form load event. If you want to start listening when you press a button, put it in the onclick event of your button. Third you will have to move any reference to a client connection to higher scope so that you can hold a reference to it for reading and eventually writing. Fourth, you will take out those writing statements and put them into a button that when clicked will read from your richeditbox and write it to the proper client connection. Now writing to the proper client connection (if multiple people are connected to you) can be tricky, but if you moved the scope higher as I suggested, you could build in a list of connection objects and specify which connection much easier. Lastly, where you have any console statements you are going to be directing the output to a richeditbox or some other control.

Try breaking it up into your different handlers and then repost your code listing any errors you get, then we can advise you how to proceed and get the application working.
User is offlineProfile CardPM

Go to the top of the page

hiphop_13
post 30 Jul, 2007 - 02:24 AM
Post #3


New D.I.C Head

*
Joined: 4 Jun, 2007
Posts: 36



Thanked 1 times
My Contributions


Thank a lot for your help.
My problem was solved by following your steps + adding threads.
Thanks:)
User is offlineProfile CardPM

Go to the top of the page

RajputS
post 3 Sep, 2008 - 07:49 PM
Post #4


New D.I.C Head

*
Joined: 3 Sep, 2008
Posts: 1

Hi I am trying to do the similar thing, on button click i am connecting the server and i am writing down the information in richedittextbox instead of console. The problem is once i click the button the computer hangs, and i have to shut the application. The control doesnt go beyond tcpclient.AcceptTcpClient() function, Please help.
User is offlineProfile CardPM

Go to the top of the page

Fast ReplyReply to this topicStart new topic
Time is now: 11/23/08 06:37AM

Live C# Help!

C# Tutorials

Reference Sheets

C# Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month