Re: [C#]TcpUdp Server/Client
Re: [C#]TcpUdp Server/Client
It shouldn't even compile at this part:
Code:
Console.WriteLine(Encoding.ASCII.GetString(data, 0, receivedDataLength));
{ <-
Console.WriteLine("What would you like to send to the server?");
line1 = Console.ReadLine();
UdpClient sock = new UdpClient();
byte[] data2 = Encoding.ASCII.GetBytes(line1);
udpClient.Send(data2, data2.Length, sender);
udpClient.Close();
//Console.Clear();
} <-
Re: [C#]TcpUdp Server/Client
Quote:
Originally Posted by
Chronologic
It shouldn't even compile at this part:
Code:
Console.WriteLine(Encoding.ASCII.GetString(data, 0, receivedDataLength));
{ <-
Console.WriteLine("What would you like to send to the server?");
line1 = Console.ReadLine();
UdpClient sock = new UdpClient();
byte[] data2 = Encoding.ASCII.GetBytes(line1);
udpClient.Send(data2, data2.Length, sender);
udpClient.Close();
//Console.Clear();
} <-
why not? that's called 'code scope' (or something like that, whatever)
it allows you to do stuff like:
Code:
{
string toPrint = Console.ReadLine(); // toPrint declaration #1
Console.WriteLine(toPrint); // prints toPrint #1
} // toPrint #1 is 'destroyed' (but not necessarily removed from memory)
{
string toPrint = Console.ReadLine().ToLowerInvariant(); // toPrint declaration #2
Console.WriteLine(toPrint); // prints toPrint #2
} // toPrint #2 is 'destroyed'
it's not like OMG I CANT LIVE WITHOUT THAT but it helps... so you can have 'multiple' variables with the same name (i use a lot of 'tmp' or 'temp' when i work with string parsers, for example)
@l1ght3r
u're making easy stuff look hard man... instead of: (also read comments)
Code:
IPEndPoint ip = new IPEndPoint(IPAddress.Parse("68.11.194.27"), 4567); // u dont really need this
byte[] data = new byte[1024]; // ur reserving 1KB for 'data'
string input, stringData; //
UdpClient udpClient = new UdpClient("68.11.194.27", 4568); // no need to set IP & port yet, you'll need to specify them later anyway
IPEndPoint sender = new IPEndPoint(IPAddress.Any, 4568); // i'm not even sure why u are using this
Socket server = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); // i always use SocketType.Raw on TCP, but idk how UDP works... so u handle that :P
string greeting = "Hello";
data = Encoding.ASCII.GetBytes(greeting); // u reserved 1KB for 'data' before, and now you're resizing this array to 5 bytes? u probably wasted 1KB here
server.SendTo(data, data.Length, SocketFlags.None, sender); // send this to what?
to
Code:
Socket server = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); // i always used SocketType.Raw... try that :P
//now u have two options, basically. choose one
{ // option 1: create a IPEndPoint instance and connect to it
IPEndPoint ip = new IPEndPoint(IPAddress.Parse("ip"), port);
server.Connect(ip);
}
{ // option 2: simply connect to the client/server :P
server.Connect("server_ip_here", (int)server_port_here);
}
string greeting = "Hello";
byte[] data = Encoding.ASCII.GetBytes(greeting); // i usually put this inside the server.Send() method, but no problems
server.Send(data); // self-explanatory, right?