Doge
Doge

Reputation: 1

c# Can't seem to get tcp client to work properly

I used the mdsn guide as an example to creating a tcp client (https://msdn.microsoft.com/en-us/library/system.net.sockets.tcpclient(v=vs.110).aspx?cs-save-lang=1&cs-lang=csharp#code-snippet-2) But i don't seem to be able to find a way to get it work either its that i can't start it (The code can't be in main or it won't recognize main as main) And I can't seem to figure any other way to running it (I can't call it in code) Heres what i wrote (The comments are in finnish so please don't mind them)

static void Connect(String server, String message) 
    {
        try
        {
        //  Luodaan Tcpclient.
        Int32 port = 1978;
        TcpClient client = new TcpClient(server, port);

            //  Käännä viesti ascii ja sitte tallenna bytenä.
            Byte[] data = System.Text.Encoding.ASCII.GetBytes(message);
            //  Tee client stream kirjottamista varten etc
            NetworkStream stream = client.GetStream();

            //  Lähetä viesti servulle
            stream.Write(data, 0, data.Length);

            Console.WriteLine("Sent: {0}", message);
            //  Saa se Tcpserver.response
            //  Bufferoi se byteiks
            data = new byte[256];

            //  Store vastaus stringinä
            String responseData = String.Empty;
            // lue eka osa siitä vastauksesta
            Int32 bytes = stream.Read(data, 0, data.Length);
            responseData = System.Text.Encoding.ASCII.GetString(data, 0, bytes);
            Console.WriteLine("Received: {0}", responseData);
            //  sulje kaikki
            stream.Close();
            client.Close();

        }
        catch (ArgumentException e)
        {
            Console.WriteLine("ArgumentNullException: {0}", e);
        }
        catch (SocketException e)
        {
            Console.WriteLine("SocketExeption: {0}", e);
        }
        Console.WriteLine(server);
        Console.WriteLine("\nPress enter to continue...");
        Console.Read();
    }

The error i'm getting is:"Program does not contain a static 'Main' method suitable for an entry point"

Please help me i'm getting really desperate

Upvotes: 0

Views: 225

Answers (2)

Doge
Doge

Reputation: 1

I figured it out, Instead of using what the guide told me i just used main and added 2 strings instead of having them in the top thingy(Don't know the real name)

Upvotes: 0

Sigge
Sigge

Reputation: 2172

Assuming you're programming a Console Application, ensure your project has the following set in Properties -> Application:

enter image description here

Then, assuming you have a Program class, ensure you have a valid static Main() method present. From this method you can call the rest.

Example:

class Program
{
    static void Main(string[] args)
    {
        Connect("my.server.test", "Hellow TCP World!");
    }

    static void Connect(String server, String message)
    {
        //ommited for brevity
    }
}

Upvotes: 1

Related Questions