예제 #1
0
 /// <summary>
 /// GetData is just a wrapper for BeginReceive.
 /// This is the public entry point for asking for data.
 /// Necessary so that we can separate networking concerns from client concerns.
 /// </summary>
 /// <param name="state"></param>
 public static void GetData(SocketState state)
 {
     state.theSocket.BeginReceive(state.messageBuffer, 0, state.messageBuffer.Length,
                                  SocketFlags.None, ReceiveCallback, state);
 }
예제 #2
0
        /// <summary>
        /// Start attempting to connect to the server
        /// </summary>
        /// <param name="host_name"> server to connect to </param>
        /// <returns></returns>
        public static SocketState ConnectToServer(Action <SocketState> connecEst, string hostName)
        {
            SocketState theServer;

            System.Diagnostics.Debug.WriteLine("connecting  to " + hostName);

            // Connect to a remote device.
            try
            {
                // Establish the remote endpoint for the socket.
                IPHostEntry ipHostInfo;
                IPAddress   ipAddress = IPAddress.None;

                // Determine if the server address is a URL or an IP
                try
                {
                    ipHostInfo = Dns.GetHostEntry(hostName);
                    bool foundIPV4 = false;
                    foreach (IPAddress addr in ipHostInfo.AddressList)
                    {
                        if (addr.AddressFamily != AddressFamily.InterNetworkV6)
                        {
                            foundIPV4 = true;
                            ipAddress = addr;
                            break;
                        }
                    }
                    // Didn't find any IPV4 addresses
                    if (!foundIPV4)
                    {
                        SocketState d = new SocketState(null, -1);

                        System.Diagnostics.Debug.WriteLine("Invalid addres: " + hostName);
                        return(d);
                    }
                }
                catch (Exception e1)
                {
                    // see if host name is actually an ipaddress, i.e., 155.99.123.456
                    System.Diagnostics.Debug.WriteLine("using IP");
                    ipAddress = IPAddress.Parse(hostName);
                }

                // Create a TCP/IP socket.
                Socket socket = new Socket(ipAddress.AddressFamily, SocketType.Stream, ProtocolType.Tcp);

                socket.SetSocketOption(SocketOptionLevel.IPv6, SocketOptionName.IPv6Only, false);


                theServer = new SocketState(socket, 0);

                //Save CallBack Delegate for connection established..
                theServer.callBack = connecEst;

                theServer.theSocket.BeginConnect(ipAddress, DEFAULT_PORT, ConnectedToServer, theServer);

                // theServer.theSocket.Close();
                return(theServer);
            }
            catch (Exception e)
            {
                System.Diagnostics.Debug.WriteLine("Unable to connect to server. Error occured: " + e);
                return(null);
            }
        }
예제 #3
0
 /// <summary>
 /// Start receiving more data from the socket
 /// </summary>
 /// <param name="ss">The socketstate to receive data from</param>
 public static void GetData(SocketState ss)
 {
     // Starts the event loop again to receive data
     ss.theSocket.BeginReceive(ss.messageBuffer, 0, ss.messageBuffer.Length, SocketFlags.None, Networking.ReceiveCallback, ss);
 }
        /// <summary>
        /// This function (along with its helper 'SendCallback') will allow a program to send data over a socket.
        /// This function needs to convert the data into bytes and then send them using socket.BeginSend.
        /// </summary>
        /// <param name="socket"></param>
        /// <param name="data"></param>
        public static void Send(SocketState ss, String data)
        {
            byte[] messageBytes = Encoding.UTF8.GetBytes(data + "\n");

            ss.theSocket.BeginSend(messageBytes, 0, messageBytes.Length, SocketFlags.None, SendCallback, ss);
        }
예제 #5
0
        /// <summary>
        /// Connects to a server at the given address and then performs the Action
        /// </summary>
        /// <param name="Action">Action to be performed hen connection occurs</param>
        /// <param name="Address">IP address of the desired server</param>
        /// <returns>The SocketState that is created by the connection</returns>
        public static SocketState ConnectToServer(SocketState.Callback Action, String Address)
        {
            System.Diagnostics.Debug.WriteLine("connecting  to " + Address);


            SocketState resultSocket;

            // Connect to a remote device.
            try
            {
                // Establish the remote endpoint for the socket.
                IPHostEntry ipHostInfo;
                IPAddress   ipAddress = IPAddress.None;

                // Determine if the server address is a URL or an IP
                try
                {
                    ipHostInfo = Dns.GetHostEntry(Address);
                    bool foundIPV4 = false;
                    foreach (IPAddress addr in ipHostInfo.AddressList)
                    {
                        if (addr.AddressFamily != AddressFamily.InterNetworkV6)
                        {
                            foundIPV4 = true;
                            ipAddress = addr;
                            break;
                        }
                    }
                    // Didn't find any IPV4 addresses
                    if (!foundIPV4)
                    {
                        System.Diagnostics.Debug.WriteLine("Invalid address: " + Address);
                        return(null);
                    }
                }
                catch (Exception e1)
                {
                    // see if host name is actually an ipaddress, i.e., 155.99.123.456
                    System.Diagnostics.Debug.WriteLine("using IP");
                    ipAddress = IPAddress.Parse(Address);
                }

                // Create a TCP/IP socket.
                Socket socket = new Socket(ipAddress.AddressFamily, SocketType.Stream, ProtocolType.Tcp);

                // set some options
                socket.SetSocketOption(SocketOptionLevel.IPv6, SocketOptionName.IPv6Only, false);

                // creates the SocketState
                resultSocket = new SocketState(socket, -1);

                // Sets the Callback function
                resultSocket.CallMe = Action;

                // Begins event loop
                resultSocket.theSocket.BeginConnect(ipAddress, Networking.DEFAULT_PORT, ConnectedCallback, resultSocket);
            }
            // catches when stuff breaks
            catch (Exception e)
            {
                System.Diagnostics.Debug.WriteLine("Unable to connect to server. Error occured: " + e);
                throw e;
            }

            // return the socketstate
            return(resultSocket);
        }