-3

I am new in C#. I can't figure what's my wrong with my code below. When I try to compile.I get the message:does not contain a static 'main' method suitable for an entry point i am searching over the internet but can't find the solution.

This is my code:

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

namespace ClientSocket2
{
    class Program
    {
        static void Connect (String server, String message)
        {
            try
            {
                // Create a TcpClient.
                // Note, for this client to work you need to have a TcpServer 
                // connected to the same address as specified by the server, port
                // combination.
                Int32 port = 13000;
                TcpClient client = new TcpClient(server, port);

                // Translate the passed message into ASCII and store it as a

                Byte array.
                Byte[] data = System.Text.Encoding.ASCII.GetBytes(message);

                // Get a client stream for reading and writing.
                // Stream stream     = client.GetStream();

                NetworkStream stream = client.GetStream();

                // Send the message to the connected TcpServer. 
                stream.Write(data, 0, data.Length);

                Console.WriteLine("Sent: {0}", message);

                // Receive the TcpServer.response.

                // Buffer to store the response bytes.
                data = new Byte [256];

                // String to store the response ASCII representation.
                String responseData = String.Empty;

                // Read the first batch of the TcpServer response bytes.
                Int32 bytes  = stream.Read(data, 0, data.Length);
                responseData = System.Text.Encoding.ASCII.GetString(data, 0, bytes);
                Console.WriteLine("Received: {0}", responseData);

                // Close everything.
                stream.Close();
                client.Close();
            }
            catch (ArgumentNullException e)
            {
                Console.WriteLine("ArgumentNullException: {0}", e);
            }
            catch (SocketException e)
            {
                Console.WriteLine("SocketException: {0}", e);
            }
            Console.WriteLine("\n Press Enter to continue...");
            Console.Read();
         }
     }
 }

Can someone please tell me where I've gone wrong. Thanks in advance.

AustinWBryan
  • 3,249
  • 3
  • 24
  • 42
RITESH KUMAR
  • 1,070
  • 10
  • 10
  • 5
    Error message isn't self-explanatory? – Soner Gönül Jan 22 '16 at 07:19
  • 1
    What are you expecting to happen when your program starts? What do you expect it to run? I think it would be a good idea to read a C# tutorial at this point, which is likely to tell you the expectations for a `Main` method and explain things in more detail. As an aside, it's very non-idiomatic to have all those spaces between dots and identifiers, array creation expressions etc. – Jon Skeet Jan 22 '16 at 07:20
  • You have searched? Add a "static void Main(){}" for example, o search again: http://stackoverflow.com/questions/9607702/does-not-contain-a-static-main-method-suitable-for-an-entry-point ... – Morcilla de Arroz Jan 22 '16 at 07:23
  • the line from your errror message "does not contain a static 'main' method suitable for an entry point" actualy means that you need to ADD a static method called main. wich is the entry point. – Thorarins Jan 22 '16 at 07:23

1 Answers1

3

You need a Main method:

namespace ClientSocket2
{
    class Program
    {
      static void Main(string[] args)
      {
        // I assume you want to read server and message from args here?
        Connect("someServer", "someMessage");
      }

      static void Connect ( String server , String message )
      {
         // ... do your stuff

I've assumed from the number of Console. statements that this is a console app. For a win forms app it is slightly different.

Community
  • 1
  • 1
NikolaiDante
  • 18,469
  • 14
  • 77
  • 117