public class ClientSock
    {
        public Socket clientsock;
        private NetworkStream SendStream = null;
        private NetworkStream RecvStream = null;
        Thread th = new Thread(Recv);

        public void InitClientSock()
        {
            try
            {
                IPEndPoint ServerEndPoint = new IPEndPoint(IPAddress.Parse("192.168.35.10"), 46000);
                clientsock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.IP);
                clientsock.Connect(ServerEndPoint);

                SendStream = new NetworkStream(clientsock);
                RecvStream = new NetworkStream(clientsock);

                th.IsBackground = true;
                th.Start(this);
            }
            catch (Exception e)
            {
                MessageBox.Show(e.Message);
            }
        }

        public void ExitSocket()
        {
            SendStream.Close();
            RecvStream.Close();
            clientsock.Shutdown(SocketShutdown.Both);
            clientsock.Close();
        }

        public void Send(string s)
        {
            BinaryFormatter bf = new BinaryFormatter();
            try
            {
                bf.Serialize(SendStream, s);
                SendStream.Flush();
            }
            catch (Exception)
            {
                return;
            }
        }

        static void Recv(object o)
        {
            ClientSock cs = o as ClientSock;
            string str = null;
            BinaryFormatter bf = new BinaryFormatter();

            if (cs != null)
            {
                try
                {
                    str = (string)bf.Deserialize(cs.RecvStream);
                    cs.RecvStream.Flush();
                }
                catch (Exception e)
                {
                    str = e.Message + "문제 발생";
                }
            }
            else
            {
                str = "Error";


                //Window1.w1.SetText(str);
                //처리 루틴.
            }
           
        }
    }

by 변경안대나 뽀씰 2009.01.02 11:47


직접 작성한(?) Listen 소켓

받아서 많이 고치고 수정하고 알맞게 만들어봤어요..


    public class Listen
    {
        Socket ListenSock = null;
        public ArrayList DoSockList = new ArrayList();
        public AsyncCallback AsyncAcceptCallback = new AsyncCallback(ProcessAcceptCallback);

        public Listen()
        {
            try
            {
                IPHostEntry IPHost;
                ListenSock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.IP);
                ListenSock.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);

                string HostName = Dns.GetHostName();
                IPHost = Dns.GetHostByName(HostName);

                IPEndPoint ServerEndPoint = new IPEndPoint(IPHost.AddressList[0], 46000);
                ListenSock.Bind(ServerEndPoint);
                ListenSock.Listen(5);
                ListenSock.BeginAccept(AsyncAcceptCallback, this);
            }
            catch (Exception e)
            {
                MessageBox.Show(e.Message);
            }
        }

        static void ProcessAcceptCallback(IAsyncResult ar)
        {
            Listen listen = (Listen)ar.AsyncState;
            try
            {
                Socket DoSock = listen.ListenSock.EndAccept(ar);

                Worker wk = new Worker(DoSock);

                listen.DoSockList.Add(wk);
                listen.ListenSock.BeginAccept(listen.AsyncAcceptCallback, listen);
            }
            catch (ObjectDisposedException)
            {
                return;
            }
            catch (SocketException)
            {
                return;
            }

        }

    }

    //클라속을 받아 처리하는 루틴.
    public class Worker
    {
        private Socket s = null;
        private NetworkStream SendStream = null;
        private NetworkStream RecvStream = null;
        Thread th = new Thread(Recv);

        public Worker(Socket _sock)
        {
            this.s = _sock;
            SendStream = new NetworkStream(s);
            RecvStream = new NetworkStream(s);

            th.IsBackground = true;
            th.Start(this);
        }

        public Socket GetSocket
        {
            get
            {
                return s;
            }
        }

        public void Send(string s)
        {
            BinaryFormatter bf = new BinaryFormatter();
            try
            {
                bf.Serialize(SendStream, s);
                SendStream.Flush();
            }
            catch (Exception)
            {
                return;
            }
        }

        static void Recv(object o)
        {
            Worker cs = o as Worker;
            string str = null;
            BinaryFormatter bf = new BinaryFormatter();

            if (cs != null)
            {
                try
                {
                    str = (string)bf.Deserialize(cs.RecvStream);
                    cs.RecvStream.Flush();
                }
                catch (Exception e)
                {
                    str = e.Message + "문제 발생";
                }
            }
            else
            {
                str = "Error";
            }
            Window1.w1.SetText(str);
        }
    }

by 변경안대나 뽀씰 2009.01.02 11:46
| 1 |