koray bey
koray bey
CC#
Created by koray bey on 5/7/2024 in #help
Socket Console App
Client
using System.Diagnostics;
using System.Net;
using System.Net.Sockets;

string url = "tcp://6.tcp.eu.ngrok.io:";
Uri serverUri = new Uri(url);
string hostname = serverUri.Host;
IPAddress serverIP = Dns.GetHostEntry(hostname).AddressList[0];
ushort port;
do
{
Console.Write("Enter port: ");
if (ushort.TryParse(Console.ReadLine(), out port))
{
Debug.WriteLine("Successfully parsed");
break;
}
else
{
Debug.WriteLine("Couldn't parse");
}

} while (true);

Socket clientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

clientSocket.Connect(new IPEndPoint(serverIP, port));
Console.WriteLine("Connected!");
using System.Diagnostics;
using System.Net;
using System.Net.Sockets;

string url = "tcp://6.tcp.eu.ngrok.io:";
Uri serverUri = new Uri(url);
string hostname = serverUri.Host;
IPAddress serverIP = Dns.GetHostEntry(hostname).AddressList[0];
ushort port;
do
{
Console.Write("Enter port: ");
if (ushort.TryParse(Console.ReadLine(), out port))
{
Debug.WriteLine("Successfully parsed");
break;
}
else
{
Debug.WriteLine("Couldn't parse");
}

} while (true);

Socket clientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

clientSocket.Connect(new IPEndPoint(serverIP, port));
Console.WriteLine("Connected!");
Server
using System.Diagnostics;
using System.Text;
using System.Net.Sockets;
using System.Net;


ushort port;

do
{
Console.Write("Enter a port to listen: ");
if (ushort.TryParse(Console.ReadLine(), out port))
{
Debug.WriteLine("Successfully parsed.");
break;
}
else
{
Debug.WriteLine("Couldn't parse");
}
} while (true);



Socket listener = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

listener.Bind(new IPEndPoint(IPAddress.Any, port));

listener.Listen(10);

Console.WriteLine("Server started on port: " + port);

Socket clientSocket = listener.Accept();

Console.WriteLine("Client connected!");
using System.Diagnostics;
using System.Text;
using System.Net.Sockets;
using System.Net;


ushort port;

do
{
Console.Write("Enter a port to listen: ");
if (ushort.TryParse(Console.ReadLine(), out port))
{
Debug.WriteLine("Successfully parsed.");
break;
}
else
{
Debug.WriteLine("Couldn't parse");
}
} while (true);



Socket listener = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

listener.Bind(new IPEndPoint(IPAddress.Any, port));

listener.Listen(10);

Console.WriteLine("Server started on port: " + port);

Socket clientSocket = listener.Accept();

Console.WriteLine("Client connected!");
How do I handle the possible exceptions and how do I communicate between.
32 replies
CC#
Created by koray bey on 5/6/2024 in #help
LINQ Help
bool IsSmooth(string input)
{
var inputWords = input.Split();

bool allCharsMatch = true;
for (int i = 1; i < inputWords.Length; i++)
{
if (inputWords[i][0] != inputWords[i - 1].Last())
{
allCharsMatch = false;
break;
}
}
return allCharsMatch;
}
bool IsSmooth(string input)
{
var inputWords = input.Split();

bool allCharsMatch = true;
for (int i = 1; i < inputWords.Length; i++)
{
if (inputWords[i][0] != inputWords[i - 1].Last())
{
allCharsMatch = false;
break;
}
}
return allCharsMatch;
}
How can I get the same functionality with LINQ, I tried looking it up but its usage of lambda functions confused me. Should I stick with this type of loops instead of LINQ and where can I learn LINQ, Microsoft's LINQ documentation is too complex for me. If I need to spend more time before I get into LINQ what should I accomplish / know?
22 replies
CC#
Created by koray bey on 5/6/2024 in #help
Confused about if statements
int sumPos = 0;
int sumNeg = 0;
bool isEmpty = false;
if (input is [])
{
Console.WriteLine("[] you entered an empty array");
isEmpty = true;
}
else //the line I talk about when I add else if(isEmpty = false)
{
Debug.WriteLine("isEmpty = false");
foreach (int i in input)
{
if (i > 0)
{
sumPos += i;
}
else if (i < 0)
{
sumNeg += i;
}
}
Console.WriteLine("Sum of the positives: " + sumPos + "\nSum of the negatives: " + sumNeg);
}

int sumPos = 0;
int sumNeg = 0;
bool isEmpty = false;
if (input is [])
{
Console.WriteLine("[] you entered an empty array");
isEmpty = true;
}
else //the line I talk about when I add else if(isEmpty = false)
{
Debug.WriteLine("isEmpty = false");
foreach (int i in input)
{
if (i > 0)
{
sumPos += i;
}
else if (i < 0)
{
sumNeg += i;
}
}
Console.WriteLine("Sum of the positives: " + sumPos + "\nSum of the negatives: " + sumNeg);
}

when I add else if instead of else the condition is met even in the debugging but it skips the else if statement
2 replies
CC#
Created by koray bey on 5/5/2024 in #help
Avalonia Beginner
should I design my app in MainWindow.axaml or MainView.axaml?
47 replies
CC#
Created by koray bey on 5/3/2024 in #help
Method generated Array printing.
int[] ArrayGenerator(int number, int length)
{
int[] returnArray = new int[length];
for (int i = 0; i < length; i++)
{
if (i > 0)
{
int result = i * number;
returnArray[i] = result;

}
else
{
returnArray[i] = number;

}
}
return returnArray;
}
int[] ArrayGenerator(int number, int length)
{
int[] returnArray = new int[length];
for (int i = 0; i < length; i++)
{
if (i > 0)
{
int result = i * number;
returnArray[i] = result;

}
else
{
returnArray[i] = number;

}
}
return returnArray;
}
How can I print the array this function returns and is the code problematic?
22 replies
CC#
Created by koray bey on 5/3/2024 in #help
Saving Feature Help
I got a list I use in my app want to add a saving feature but don't know which filetype to use besides txt whats your recommendation?
33 replies
CC#
Created by koray bey on 4/30/2024 in #help
if statement and scopes
A developer writes some code that includes an if statement code block. They initialize one integer variable to a value of 5 above (outside) of the code block. They initialize a second integer variable to a value of 6 on the first line inside of the code block. The Boolean expression for the code block evaluates to true if the first integer variable has a value greater than 0. On the second line inside the code block, they assign the sum of the two values to the first variable. On the first line after the code block, they write code to display the value of the first integer. What is the result when the code statement used to display the first integer is executed? I try to write this code in my IDE to answer the question although while I'm getting an error the answer is that the program runs without errors just displays the wrong value. Here is my code: static void Main() { int x = 5; if (x > 0) { int y = 6; int x = x + y; } Console.Read(); } I get the errors: Error CS0136 A local or parameter named 'x' cannot be declared in this scope because that name is used in an enclosing local scope to define a local or parameter Use of unassigned local variable 'x'
12 replies