Click here to Skip to main content
15,886,199 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
how to convert this console app to form app in C#. the problem is the output bytes contain characters(somthing like fff30fff). its working fine on console applicaton. But when i tried to convert it to windows form application and show the bytes out in textbox it shows error. i googled several hours. but icant find solution.


C#
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading;
using System.Net;
using System.Net.Sockets;
using System.IO;

//client

namespace ConsoleApplication9
{
    class Program
    {
        static Socket sck;
        static void Main(string[] args)
        {
            sck = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            IPEndPoint localEndPoint = new IPEndPoint(IPAddress.Parse("192.168.0.177"), 80);

            try
            {
                sck.Connect(localEndPoint);
            }
            catch 
            {
                Console.Write("Unable to Connect \r\n");
                Main(args);
            }
            Console.Write("Enter Text: ");
            string text=Console.ReadLine();
            byte[] data = Encoding.ASCII.GetBytes(text);

            sck.Send(data);
            Console.Write("Data Sent!\r\n");
            Console.Write("Press any key to Continue...");
           string  l;
           
            Console.Read();
            
            byte[] bytes = new byte[15];
           
            sck.Receive(bytes);
            Console.WriteLine(Encoding.UTF8.GetString(bytes));
           
           sck.Close();
           Console.Read();
          

       
            
           
        }
        
    }
}
Posted
Comments
Sergey Alexandrovich Kryukov 24-Nov-15 3:26am    
It does not really matter if this is a console application or not, bytes and characters are the same.
In what variable/object you have these characters? In what line? What do you want to get from this data? What do you want to achieve?
—SA

1 solution

A console app and a WinForms app are not the same, and the way they work is very, very different: In a console app you can code "do this, then this, then that" (as you have done) but in a WinForms app you have to work within the structure of a Windows application. Which means you don't just say "do this, then that", you react to events and do something then.

We can't tell exactly what you have done wrong when you converted it, but I'm guessing that you just lifted that code, dropped it in and tried to replace Console.Read and Console.Write with textboxes. That doesn't work: you need to redesign the whole app to work with Sockets using the event they provide.

This may help: https://msdn.microsoft.com/en-us/library/538f44zk(v=vs.80).aspx[^] - it's pretty old, but it's a starting point.
 
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