Quantcast
Channel: Azure Virtual Machines forum
Viewing all articles
Browse latest Browse all 12545

How to communicate with a simple c# console app with TCP on Microsoft Azure virtual machine?

$
0
0

Hello, i have just started with Microsoft Azure free edition and playing around with a virtual machine using Windows Server 2012 OS I managed to set up MySQL and FileZilla FTP server and connect ot them.

But at the beginning, i got two problems with setting up MySQL and FileZilla.

First, I didn't know i had to set EndPoints/Portfoward with port 3306 on MySQL and port 21 on FTP in Microsoft Azure. 

After i did that, then it still didn't work, but then i figured out that i had to set the Windows Firewall Inbound Rules. 

Then i could connect to it and it works!!

But now, im trying to create my own c# console application that i can communiate with by using TCP. Below, is the two programs i have made. One called Client, the other called Server. I have made them as simple as simple it can get.

Client program

static void Main(string[] args)
{
    string msg = Console.ReadLine();
    byte[] buffer = Encoding.ASCII.GetBytes(msg);

    //The xxx... is the Public Virtual IP Address on Azure
    using (TcpClient client = new TcpClient("xxx.xx.xx.xx", 10000))
    {
        using (Stream stream = client.GetStream())
        {
            stream.Write(buffer, 0, buffer.Length);
        }
    }

    Console.ReadLine();
}


Server Program

static void Main(string[] args)
{
    TcpListener listener = new TcpListener(IPAddress.Any, 10000);
    listener.Start();
    using (TcpClient client = listener.AcceptTcpClient())
    {
        using (Stream stream = client.GetStream())
        {
            byte[] requestBuffer = new byte[100];
            int size = stream.Read(requestBuffer, 0, requestBuffer.Length);
            string msg = Encoding.ASCII.GetString(requestBuffer, 0, size);
            Console.WriteLine(msg);
        }
    }

    Console.ReadLine();
}

I have set up the EndPoints in Microsoft Azure and set up the WindowsFirewall Inbound things for the c# program like i did with MySQL and FileZilla. But it doesn't connect/work.

I have tried to make this work with my computer connecting to my friends computer by setting portforward on his router. And it works. So im sure i have made most of the things right, but now im totally stuck and have no idea why it doesn't work on this Azure Virtual machine. Do i need to do some more special confgurations with Windows Server 2012 OS?

Im a beginner with server os'es, so i have no idea.

Hope someone knows can save my life. But thank you very much for reading this far!


Viewing all articles
Browse latest Browse all 12545

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>