Beispiel #1
0
        public bool reserve(int port)
        {
            try
            {
                clientList  = new List <Socket>();
                localSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                IPEndPoint localEndpoint = new IPEndPoint(IPAddress.Any, port);

                ConnectionObject co = new ConnectionObject
                {
                    workingBytes = new byte[BUFFER]
                };

                localSocket.Bind(localEndpoint);
                localSocket.Listen(100);
                localSocket.BeginAccept(new AsyncCallback(OnAccept), co);

                return(true);
            }
            catch (Exception exc)
            {
                removeUnusedSocket();
                ExceptionRaised(this, new ExceptionRaiseEventArgs(exc));
                return(false);
            }
        }
Beispiel #2
0
        // tcp async call back
        private void OnAccept(IAsyncResult ar)
        {
            try
            {
                if (ar.IsCompleted)
                {
                    ConnectionObject recObj = ar.AsyncState as ConnectionObject;
                    recObj.clientSocket = localSocket.EndAccept(ar);
                    localSocket.BeginAccept(new AsyncCallback(OnAccept), recObj);

                    ConnectionObject co = new ConnectionObject
                    {
                        workingBytes = new byte[BUFFER],
                        clientSocket = recObj.clientSocket
                    };

                    clientList.Add(recObj.clientSocket);
                    recObj.clientSocket.BeginReceive(
                        co.workingBytes,
                        0,
                        co.workingBytes.Length,
                        SocketFlags.None, new AsyncCallback(OnReceive),
                        co);
                }
            }
            catch (Exception exc)
            {
                removeUnusedSocket();
                ExceptionRaised(this, new ExceptionRaiseEventArgs(exc));
            }
        }
Beispiel #3
0
        public bool send(byte[] data)
        {
            try
            {
                if (localSocket == null)
                {
                    return(false);
                }
                else
                {
                    ConnectionObject co = new ConnectionObject
                    {
                        workingBytes = data,
                        clientSocket = localSocket
                    };

                    if (clientList == null)
                    {
                        localSocket.BeginSend(data, 0, data.Length, SocketFlags.None, new AsyncCallback(OnSend), co);
                    }
                    else
                    {
                        foreach (var client in clientList)
                        {
                            co = new ConnectionObject
                            {
                                workingBytes = data,
                                clientSocket = client
                            };
                            client.BeginSend(data, 0, data.Length, SocketFlags.None, new AsyncCallback(OnSend), co);
                        }
                    }
                }

                return(true);
            }
            catch (Exception exc)
            {
                removeUnusedSocket();
                ExceptionRaised(this, new ExceptionRaiseEventArgs(exc));
                return(false);
            }
        }
Beispiel #4
0
        public bool sendBySpecificSocket(byte[] data, Socket sockAddr)
        {
            try
            {
                ConnectionObject co = new ConnectionObject
                {
                    workingBytes = data,
                    clientSocket = sockAddr
                };

                sockAddr.BeginSend(data, 0, data.Length, SocketFlags.None, new AsyncCallback(OnSend), co);

                return(true);
            }
            catch (Exception exc)
            {
                removeUnusedSocket();
                ExceptionRaised(this, new ExceptionRaiseEventArgs(exc));
                return(false);
            }
        }
Beispiel #5
0
        public bool connect(IPEndPoint remoteEndpoint)
        {
            try
            {
                localSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                ConnectionObject co = new ConnectionObject
                {
                    workingBytes = new byte[BUFFER],
                    clientSocket = localSocket
                };

                localSocket.BeginConnect(remoteEndpoint, new AsyncCallback(OnConnect), co);

                return(true);
            }
            catch (Exception exc)
            {
                removeUnusedSocket();
                ExceptionRaised(this, new ExceptionRaiseEventArgs(exc));
                return(false);
            }
        }
Beispiel #6
0
        private void OnReceive(IAsyncResult ar)
        {
            try
            {
                if (ar.IsCompleted)
                {
                    ConnectionObject recObj = ar.AsyncState as ConnectionObject;

                    Socket workingSock = recObj.clientSocket;

                    int    receivedBytes = workingSock.EndReceive(ar);
                    byte[] recBytes      = recObj.workingBytes.Take(receivedBytes).ToArray();

                    ReceiveCompleted(this, new ReceiveCompletedEventArgs(recBytes, workingSock));

                    ConnectionObject co = new ConnectionObject
                    {
                        workingBytes = new byte[BUFFER],
                        clientSocket = workingSock
                    };

                    workingSock.BeginReceive(co.workingBytes, 0, co.workingBytes.Length, SocketFlags.None, new AsyncCallback(OnReceive), co);
                }
            }
            catch (Exception exc)
            {
                removeUnusedSocket();
                ConnectionObject recObj = ar.AsyncState as ConnectionObject;
                if (recObj.clientSocket == default(Socket))
                {
                    ExceptionRaised(this, new ExceptionRaiseEventArgs(exc));
                }
                else
                {
                    ExceptionRaised(recObj.clientSocket, new ExceptionRaiseEventArgs(exc));
                }
            }
        }