Exemple #1
0
        private void ClientListen(object args)
        {
            object[] argsTmp = (object[])args;
            int      cnt     = int.Parse((string)(argsTmp[0].ToString()));
//            NetworkStream stream = tcpclient[cnt].GetStream();
            NetworkStream stream = socketclass[cnt].client.GetStream();

            Byte[] bytes = new Byte[100];

            dlgWriteText dlgText = new dlgWriteText(WriteReadText);

            while (true)
            {
                try
                {
                    int intCount = stream.Read(bytes, 0, bytes.Length);

                    if (intCount != 0)
                    {
                        //受信部分だけ切り出す
                        Byte[] getByte = new byte[intCount];
                        for (int i = 0; i < intCount; i++)
                        {
                            getByte[i] = bytes[i];
                        }
                        byte[] uniBytes;

                        //'S-Jisからユニコードに変換
                        Encoding ecSjis = Encoding.GetEncoding("shift-jis");
                        Encoding ecUni  = Encoding.GetEncoding("utf-16");
                        uniBytes = Encoding.Convert(ecSjis, ecUni, bytes);

                        string strGetText = ecUni.GetString(uniBytes);

                        //受信文字を切り出す
                        strGetText = strGetText.Substring(0, strGetText.IndexOf((char)0));
                        txtReceive.Invoke(dlgText, strGetText);

                        //サーバから切断
                        CloseClient();
                    }
                    else
                    {
                        //サーバから切断
                        CloseClient();

                        return;
                    }
                }
                catch (System.Threading.ThreadAbortException)
                {
                    return;
                }
                catch (Exception ex)
                {
                    return;
                }
            }
        }
Exemple #2
0
        // クライアント サーバへの送信処理本体
        private void ClientListen(object args)
        {
            try
            {
                TcpClient client = new TcpClient("127.0.0.1", int.Parse(txtSendPort.Text));

                NetworkStream stream = client.GetStream();

                Byte[] bytes = new Byte[100];

                dlgWriteText dlgText = new dlgWriteText(WriteReadText);

                try
                {
                    //sift-jisに変換して送る
                    Encoding ecSjis = Encoding.GetEncoding("shift-jis");
                    Byte[]   data   = ecSjis.GetBytes(txtSend.Text);

                    stream.Write(data, 0, data.Length);
                }
                catch (Exception ex)
                {
                    MessageBox.Show("送信できませんでした。", "送信エラー");
                }

                while (true)
                {
                    try
                    {
                        int intCount = stream.Read(bytes, 0, bytes.Length);

                        if (intCount != 0)
                        {
                            //受信部分だけ切り出す
                            Byte[] getByte = new byte[intCount];
                            for (int i = 0; i < intCount; i++)
                            {
                                getByte[i] = bytes[i];
                            }
                            byte[] uniBytes;

                            //'S-Jisからユニコードに変換
                            Encoding ecSjis = Encoding.GetEncoding("shift-jis");
                            Encoding ecUni  = Encoding.GetEncoding("utf-16");
                            uniBytes = Encoding.Convert(ecSjis, ecUni, getByte);

                            string strGetText = ecUni.GetString(uniBytes);

                            //受信文字を切り出す
                            strGetText = strGetText.Substring(0, strGetText.IndexOf((char)0));
                            txtReceive.Invoke(dlgText, strGetText);

                            //サーバと切断
                            if (client != null && client.Connected)
                            {
                                client.Close();
                            }
                        }
                        else
                        {
                            //サーバと切断
                            if (client != null && client.Connected)
                            {
                                client.Close();
                            }

                            return;
                        }
                    }
                    catch (System.Threading.ThreadAbortException)
                    {
                        return;
                    }
                    catch (Exception ex)
                    {
                        return;
                    }

                    btnSend.Enabled = true;
                }
            }
            catch (SocketException socketEx)
            {
                string msg = "ERROR SocketErrorCode=:" + socketEx.SocketErrorCode
                             + " ERROR NativeErrorCodee=:" + socketEx.NativeErrorCode
                             + " Message=:" + socketEx.Message;

                MessageBox.Show(msg);
            }
            catch (Exception ex)
            {
            }
        }