-
[c#] sockets
ok im new to c# and tried some tuturials around and readed some ebooks about the general stuff of c#.
I made this from some online tuturial:
PHP Code:
using System;
using System.Text;
using System.Net;
using System.Net.Sockets;
public class serv {
public static void Main() {
try {
IPAddress ipAd = IPAddress.Parse("127.0.0.1");
TcpListener myList=new TcpListener(ipAd,912);
int lol = 0;
for (int l = 0; l == l; )
{
myList.Start();
if (lol == 0)
{
Console.WriteLine("The server is running...");
Console.WriteLine("The local End point is :" + myList.LocalEndpoint);
lol = 1;
}
Console.WriteLine("");
Socket s = myList.AcceptSocket();
Console.WriteLine("Connection accepted from " + s.RemoteEndPoint);
byte[] b = new byte[100];
int k = s.Receive(b);
char cc = Convert.ToChar(b[0]);
string gg = Convert.ToString(cc);
switch (gg)
{
case "U":
//stuff
break;
default:
Console.WriteLine("Unknown data received: \"" + gg + "\".");
break;
}
Console.WriteLine("Disconnected " + s.RemoteEndPoint);
s.Close();
myList.Stop();
}
}
catch (Exception e) {
Console.WriteLine("Error..... " + e.StackTrace);
}
}
}
Im pretty sure i did some horrible stuff in this code,
could somebody optimize this and add comments of like why and how things are being used.
Thanks
-
Re: [c#] sockets
PHP Code:
using System;
//using System.Text; (Unnecessary reference)
using System.Net;
using System.Net.Sockets;
namespace Program
{
//public class serv { (Bad class name. Should be PascalCase. Also avoid abbreviations)
public class Server
{
public static void Main()
{
try
{
//ipAd/myList (Avoid abbreviations)
IPAddress ipAddress = IPAddress.Parse("127.0.0.1");
TcpListener listener = new TcpListener(ipAddress, 912);
//int lol = 0; (Bad variable name, and use proper types. Changed it to 'isRunning')
bool isRunning = true;
listener.Start();
Console.WriteLine("The server is running...");
//Console.WriteLine("The local end point is: " + listener.LocalEndpoint); (Use formatting)
Console.WriteLine("The local end point is: {0}", listener.LocalEndpoint);
Console.WriteLine();
Socket socket;
byte[] buffer = new byte[100];
char packetId;
//for (int l = 0; l == l; ) (use while loops instead)
while(isRunning)
{
//myList.Start(); (Why should this be called inside an infinite loop?
/*if (lol == 0)
{
Console.WriteLine("The server is running...");
Console.WriteLine("The local End point is :" + myList.LocalEndpoint);
lol = 1;
}*/
// Console.WriteLine(""); (Don't have to use "" for a blank line. Use no args)
//Socket s = myList.AcceptSocket(); (Shouldn't create a new variable repeatedly)
socket = listener.AcceptSocket();
if (socket != null) // New connection
{
Console.WriteLine("Connection accepted from {0}", socket.RemoteEndPoint);
//byte[] b = new byte[100]; (Shouldn't create a new variable repeatedly)
//int k = socket.Receive(b); (No need to store return value)
socket.Receive(buffer);
packetId = (char)buffer[0];
//string gg = Convert.ToString(cc); (unnecessary, your packet are only chars)
switch (packetId)
{
//case "U": (" = string, ' = char)
case 'U':
// Do stuff here
break;
default:
Console.WriteLine("Unknown data received: \"{0}\"", packetId);
break;
}
Array.Clear(buffer, 0, buffer.Length); // clear buffer
Console.WriteLine("Disconnected {0}", socket.RemoteEndPoint);
socket.Close();
}
}
listener.Stop();
}
catch (Exception e)
{
//Console.WriteLine("Error..... " + e.StackTrace);
Console.WriteLine(e.ToString()); // (Get the entire exception besides stack trace)
}
}
}
}
Without comments:
PHP Code:
using System;
using System.Net;
using System.Net.Sockets;
namespace Program
{
public class Server
{
public static void Main()
{
try
{
IPAddress ipAddress = IPAddress.Parse("127.0.0.1");
TcpListener listener = new TcpListener(ipAddress, 912);
bool isRunning = true;
listener.Start();
Console.WriteLine("The server is running...");
Console.WriteLine("The local end point is: {0}", listener.LocalEndpoint);
Console.WriteLine();
Socket socket;
byte[] buffer = new byte[100];
char packetId;
while(isRunning)
{
socket = listener.AcceptSocket();
if (socket != null) // New connection
{
Console.WriteLine("Connection accepted from {0}", socket.RemoteEndPoint);
socket.Receive(buffer);
packetId = (char)buffer[0];
switch (packetId)
{
case 'U':
// Do stuff here
break;
default:
Console.WriteLine("Unknown data received: \"{0}\"", packetId);
break;
}
Array.Clear(buffer, 0, buffer.Length);
Console.WriteLine("Disconnected {0}", socket.RemoteEndPoint);
socket.Close();
}
}
listener.Stop();
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
}
}
}
-
Re: [c#] sockets
Your my hero, Thanks, i will learn much of this for sure!
-
Re: [c#] sockets
Bad using chars
using strings:
Code:
using System;
using System.Net;
using System.Net.Sockets;
namespace Program
{
public class Server
{
public static void Main()
{
try
{
IPAddress ipAddress = IPAddress.Parse("127.0.0.1");
TcpListener listener = new TcpListener(ipAddress, 912);
bool isRunning = true;
listener.Start();
Console.WriteLine("The server is running...");
Console.WriteLine("The local end point is: {0}", listener.LocalEndpoint);
Console.WriteLine();
Socket socket;
byte[] buffer = new byte[100];
while (isRunning)
{
socket = listener.AcceptSocket();
if (socket != null) // New connection
{
Console.WriteLine("Connection accepted from {0}", socket.RemoteEndPoint);
socket.Receive(buffer);
string data = System.Text.Encoding.ASCII.GetString(buffer);
string packet = data.Substring(0, 4);
string[] args = data.Substring(4).Split(Convert.ToChar(2));
switch (packet)
{
case "TEST":
Console.WriteLine("Testing command. " + args[0]);
break;
}
Array.Clear(buffer, 0, buffer.Length);
Console.WriteLine("Disconnected {0}", socket.RemoteEndPoint);
try
{
socket.Close();
}
catch (Exception e) { throw new Exception("Something has gone wrong while closing connection...", e); }
}
}
listener.Stop();
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
}
}
}
the "packet" key is the first 4 letters of the string, you can change though:
Code:
string packet = data.Substring(0, 4);
string[] args = data.Substring(4).Split(Convert.ToChar(2));
-
Re: [c#] sockets
Chars are good for packets.