예제 #1
0
파일: Service1.cs 프로젝트: nspodar/labs
        void WorkerThread()
        {
            mustStop = false;
            IPEndPoint endPoint = new IPEndPoint(IPAddress.Any, port);
            Socket     listener = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

            try
            {
                listener.Bind(endPoint);
                listener.Listen(10);

                while (!mustStop)
                {
                    using (Socket handler = listener.Accept())
                    {
                        XDocument docReq = UR.ReceiveXML(handler);
                        handler.SendFile(HandleRequest(docReq));
                        handler.Shutdown(SocketShutdown.Both);
                    }
                }
            }
            catch (Exception e)
            {
                UR.WriteLog(e.ToString());
            }
        }
예제 #2
0
        private static XDocument TalkOutIn(string requestPath, IPAddress serverIP)
        {
            XDocument resXml = null;

            using (Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp))
            {
                IPEndPoint epThere = new IPEndPoint(serverIP, port);
                try
                {
                    IAsyncResult result  = s.BeginConnect(epThere, null, null);
                    bool         success = result.AsyncWaitHandle.WaitOne(3000, true);

                    if (s.Connected)
                    {
                        s.SendFile(requestPath);
                        resXml = UR.ReceiveXML(s);
                        s.Shutdown(SocketShutdown.Both);
                        s.EndConnect(result);
                    }
                    else
                    {
                        throw new ApplicationException("Failed to connect server.");
                    }
                }
                catch (Exception e)
                {
                    MessageBox.Show("Can`t connect.\n" + e.Message);
                    if (s.Connected)
                    {
                        s.Shutdown(SocketShutdown.Both);
                    }
                }
            }
            return(resXml);
        }