/// <summary> /// This method is the heart of the server code, it asks the OS to listen for new connections, and when a client /// makes a connection request it calls the Accept_a_New_Client function. Note : This methos is an event loop, it /// continues to listen for new connections continually. /// </summary> /// <param name="callbackFcn">The desired function you wish to callback to when the Accept_a_New_Client method has finished.</param> public static void Server_Awaiting_Client(PreservedState.CallBackFunction callbackFcn, int port) { // Create a PreservedState object. PreservedState state = null; // Store the callback function in the PreservedState obj. PreservedState.CallBackFunction callBack = callbackFcn; // Create a new IPHostEntry based on the hostname. IPHostEntry ipHostInfo = Dns.GetHostEntry("localhost"); // Set IPAddress equal to null IPAddress ipAddress = null; // Loop through all the ipaddresses in the ipHostInfo List and find the correct one. foreach (IPAddress address in ipHostInfo.AddressList) { // If the address is of type InterNetwork, store this address as the ipAddress. if (address.AddressFamily == AddressFamily.InterNetworkV6) { ipAddress = address; break; } } // Extablish local end point for the socket. IPEndPoint localEP = new IPEndPoint(ipAddress, port); // Create a socket. Socket listener = new Socket(AddressFamily.InterNetworkV6, SocketType.Stream, ProtocolType.Tcp); listener.SetSocketOption(SocketOptionLevel.IPv6, SocketOptionName.IPv6Only, false); // Associate the listener socket to only this local end point. listener.Bind(localEP); // We will only allow up to 100 client connections to be backlogged. listener.Listen(100); // Create a new state object storing the listener socket and the callback function. state = new PreservedState(listener, callBack); // Print to the Server window, to allow users to see that the server is listening for more players. Console.WriteLine("Listening for new connections....."); // When a listener has detected a new client trying to add, call Accept_a_New_Client to accept the new cliet. listener.BeginAccept(Accept_a_New_Client, state); }
/// <summary> /// This is a constant value that acts as the port number for all server and client communications. /// </summary> //private const int port = 11000; /// <summary> /// This method begins the connection process between the server and the client. It also creates and returns the socket to be used in other methods in this class. It takes /// in a callback function to be stored in the PreservedState obj and the hostname of the desired server you wish to connect to. /// </summary> /// <param name="callBack">A callback function to be stored and used later to help in exiting and processing the server data./param> /// <param name="hostname">A string containing the server you wish to connect to.</param> /// <returns>A socket that enables communication from the server to client.</returns> public static Socket Connect_to_Server(PreservedState.CallBackFunction callBack, String hostname, int port) { // Create a PreservedState object. PreservedState state = null; // Store the callback function in the PreservedState obj. PreservedState.CallBackFunction callBackFcn = callBack; // Create a new IPHostEntry based on the hostname. IPHostEntry ipHostInfo = Dns.GetHostEntry(hostname); // Set IPAddress equal to null IPAddress ipAddress = null; // Loop through all the ipaddresses in the ipHostInfo List and find the correct one. foreach (IPAddress address in ipHostInfo.AddressList) { // If the address is of type InterNetwork, store this address as the ipAddress. if (address.AddressFamily == AddressFamily.InterNetworkV6) { ipAddress = address; break; } } // Create a IPEndPoint in order to begin connecting to the server. IPEndPoint remoteEP = new IPEndPoint(ipAddress, port); // Create a socket Socket socket = new Socket(AddressFamily.InterNetworkV6, SocketType.Stream, ProtocolType.Tcp); socket.SetSocketOption(SocketOptionLevel.IPv6, SocketOptionName.IPv6Only, false); // Create a PreservedState obj and store the callback function and socket so it can be used later. state = new PreservedState(socket, callBackFcn); // Begin connecting to the server by calling the Connected_To_Server callback. socket.BeginConnect(remoteEP, new AsyncCallback(Connected_to_Server), state); // Return the socket. return(state.socket); }
/// <summary> /// This method is called when the user wishes to send data to the server. The callBack parameter /// is optional. If a callback is provided, it will be called /// </summary> /// <param name="socket">The socket this connection is using to talk to the server</param> /// <param name="data">The information the user wishes to send to the server.</param> public static void Send(Socket socket, String data, PreservedState.CallBackFunction callBackFcn = null) { // Lock this method so multiple threads can be sending data at the same time. lock (locker) { // Create a new PreservedState object to store the socket and buffer. PreservedState state = new PreservedState(socket, null); // If the optional callback parameter was provided, add it to the state // object so it can be called once the send is complete. state.callBack = callBackFcn; // Translate the string of data into bytes state.buffer = encoding.GetBytes(data); // Began sending the bytes of data to the server, and call the callback SendCallBack. if (socket.Connected) { socket.BeginSend(state.buffer, 0, state.buffer.Length, SocketFlags.None, SendCallBack, state); } } }