-
-
Save MultiWu/c52fedd442ce8a9309f15b1e312019ca to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// A C# Program for Server | |
using System; | |
using System.Net; | |
using System.Net.Sockets; | |
using System.Reflection.Metadata; | |
using System.Text; | |
namespace AC_Simulator_Server | |
{ | |
class Program | |
{ | |
// Main Method | |
static void Main(string[] args) | |
{ | |
ExecuteServer(); | |
} | |
public static void ExecuteServer() | |
{ | |
string[] id = new string[999]; | |
string[] user = new string[999]; | |
string[] pass = new string[999]; | |
string[] userBells = new string[999]; | |
string[] userTurnips = new string[999]; | |
int licznik = 1; | |
// Establish the local endpoint | |
// for the socket. Dns.GetHostName | |
// returns the name of the host | |
// running the application. | |
IPHostEntry ipHost = Dns.GetHostEntry(Dns.GetHostName()); | |
IPAddress ipAddr = ipHost.AddressList[0]; | |
IPEndPoint localEndPoint = new IPEndPoint(ipAddr, 11111); | |
// Creation TCP/IP Socket using | |
// Socket Class Costructor | |
Socket listener = new Socket(ipAddr.AddressFamily, | |
SocketType.Stream, ProtocolType.Tcp); | |
try | |
{ | |
// Using Bind() method we associate a | |
// network address to the Server Socket | |
// All client that will connect to this | |
// Server Socket must know this network | |
// Address | |
listener.Bind(localEndPoint); | |
// Using Listen() method we create | |
// the Client list that will want | |
// to connect to Server | |
listener.Listen(1); | |
while (true) | |
{ | |
Console.WriteLine("Waiting connection ... "); | |
// Suspend while waiting for | |
// incoming connection Using | |
// Accept() method the server | |
// will accept connection of client | |
Socket clientSocket = listener.Accept(); | |
// Data buffer | |
byte[] bytes = new Byte[1024]; | |
string data = null; | |
int numByte = clientSocket.Receive(bytes); | |
data += Encoding.ASCII.GetString(bytes, 0, numByte); | |
if (data == "REG") | |
{ | |
Console.WriteLine("NEW REGISTER: ", data); | |
byte[] message = Encoding.ASCII.GetBytes("OK REG"); | |
clientSocket.Send(message); | |
byte[] bytesReg1 = new Byte[1024]; | |
string dataReg1 = null; | |
int numByteReg1 = clientSocket.Receive(bytesReg1); | |
dataReg1 += Encoding.ASCII.GetString(bytesReg1, 0, numByteReg1); | |
licznik++; | |
id[licznik] = licznik.ToString(); | |
user[licznik] = dataReg1; | |
Console.WriteLine("ID: ", licznik); | |
Console.WriteLine("User: ", dataReg1); | |
byte[] messageReg1 = Encoding.ASCII.GetBytes("OK REG1"); | |
clientSocket.Send(messageReg1); | |
byte[] bytesReg2 = new Byte[1024]; | |
string dataReg2 = null; | |
int numByteReg2 = clientSocket.Receive(bytesReg2); | |
dataReg2 += Encoding.ASCII.GetString(bytesReg2, 0, numByteReg2); | |
pass[licznik] = dataReg2; | |
Console.WriteLine("Pass: ", dataReg2); | |
byte[] messageReg2 = Encoding.ASCII.GetBytes("OK REG2"); | |
clientSocket.Send(messageReg2); | |
byte[] messageReg3 = Encoding.ASCII.GetBytes(licznik.ToString()); | |
clientSocket.Send(messageReg3); | |
byte[] bytesReg3 = new Byte[1024]; | |
string dataReg3 = null; | |
int numByteReg3 = clientSocket.Receive(bytesReg3); | |
dataReg3 += Encoding.ASCII.GetString(bytesReg3, 0, numByteReg3); | |
if (data.IndexOf("REG END SUCC") > -1) | |
{ | |
Console.WriteLine("REGISTRATION ENDED SUCCESFULLY: ", data); | |
} | |
} | |
// Close client Socket using the | |
// Close() method. After closing, | |
// we can use the closed Socket | |
// for a new Client Connection | |
clientSocket.Shutdown(SocketShutdown.Both); | |
clientSocket.Close(); | |
} | |
} | |
catch (Exception e) | |
{ | |
Console.WriteLine(e.ToString()); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment