I want to make a simple TCP server in C# so I want to define some variables but I can't access the variables which I declared. Is it possible to bypass this?
Current code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Net;
using System.Net.Sockets;
namespace TCP_Server_gui
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
string serverIp = "127.0.0.1";
int serverPort = 4200;
IPAddress localAdd = IPAddress.Parse(serverIp);
TcpListener listener = new TcpListener(localAdd, serverPort);
}
}
I get those errors:
Severity Code Description
Error CS0236 A field initializer cannot reference the non-static field, method, or property 'Form1.serverIp'
Error CS0236 A field initializer cannot reference the non-static field, method, or property 'Form1.localAdd'
Error CS0236 A field initializer cannot reference the non-static field, method, or property 'Form1.serverPort'