[socket] c# server , as3 client


pc1 ( server ): c# (windows 7)








pc2 ( client ) : AS3 (windows xp)



c# code (partial)



Created with colorer-take5 library. Type 'csharp'

/*
 * SharpDevelop으로 작성��었습니다.
 * 사용자: jjongun
 * 날짜: 2010-02-09
 * 시간: ��전 9:05
 * 
 * 
 */
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
using System.Threading;
using System.Net.Sockets;
using System.Net;
using System.IO;
using System.Diagnostics;
using System.Timers;


namespace testServer3
{
    /// <summary>
    /// Description of MainForm.
    /// </summary>
    public partial class MainForm : Form
    {
        public MainForm()
        {
            //
            // The InitializeComponent() call is required for Windows Forms designer support.
            //
            InitializeComponent();
            
            //
            // TODO: Add constructor code after the InitializeComponent() call.
            //
        }
        private delegate void serverMrgDele(string str);
        private System.Timers.Timer catchClient;
        private System.Timers.Timer readTimer;
        private IPAddress address;
        private Int32 port;
        private TcpListener server;
        private serverMrgDele mrg;
        private TcpClient client;
        private StreamWriter writer;
        private StreamReader reader;
        private NetworkStream stream;
        
        private Thread serverThread;
        private Thread checkThread;
        private Thread readThread;
        void ConnectBtClick(object sender, EventArgs e)
        {
            if(ipTx.Text == "")
                ipTx.Text = "127.0.0.1";
            if(portTx.Text == "")
                portTx.Text = "1000";
            try{
                address = IPAddress.Parse(ipTx.Text);
                port = Convert.ToInt32(portTx.Text);
            }catch(FormatException fe)
            {
                Trace.WriteLine(fe);
                ipTx.Clear();
                portTx.Clear();
            }
            
            mrg = new serverMrgDele(traceBox.AppendText);
            
            Thread serverThread = new Thread( new ThreadStart(startServer));
            readThread = new Thread(new ThreadStart(readClinet));
            checkThread = new Thread(new ThreadStart(checkClinetStatus));
            serverThread.Start();
        }
        
        //thread1
        private void startServer()
        {
            server = new TcpListener(address , port);
            Invoke(mrg , "start" + Environment.NewLine);
            server.Start();
            client = server.AcceptTcpClient();
            Invoke(mrg , "client connect" + Environment.NewLine);
            stream = client.GetStream();
            
            writer = new StreamWriter(stream);
            
            readThread.Start();
            checkThread.Start();
        }
        
        //thrad2
        private void readClinet()
        {
            reader = new StreamReader(stream);
            readTimer = new System.Timers.Timer();
            readTimer.Elapsed+= new ElapsedEventHandler(readTimer_Elapsed);
            readTimer.Interval=100;
            readTimer.Start();
        }
        
        void readTimer_Elapsed(object sender, ElapsedEventArgs e)
        {
            Invoke(mrg , reader.ReadLine() + Environment.NewLine);
        }
        
        
        
        //thread3
        private void checkClinetStatus()
        {
            catchClient = new System.Timers.Timer();
            catchClient.Elapsed += new ElapsedEventHandler(catchClient_Elapsed);
            catchClient.Interval = 100;
            catchClient.Start();
        }

        void catchClient_Elapsed(object sender, ElapsedEventArgs e)
        {
            //????
            //Trace.WriteLine(client.Connected);
        }
        
                
        private int checknum = 0;
        void SendBtClick(object sender, EventArgs e)
        {
            checknum ++ ;
            if(sendTx.Text =="")
                sendTx.Text = "mrg form C#" + checknum.ToString();
            writer.WriteLine(sendTx.Text + "\n");
            writer.Flush();
        }
        
        void DisconnectBtClick(object sender, EventArgs e)
        {
            try{
                client.Close();
            }catch( Exception ex){}
            writer.Close();
            reader.Close();
            server.Stop();
        }
    }
}
 
 
 
 
 
 AS3 code (flex4)
 
 
 
 
 
 
 
 
 
<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" 
               xmlns:s="library://ns.adobe.com/flex/spark" 
               xmlns:mx="library://ns.adobe.com/flex/halo" minWidth="1024" minHeight="768"
               width="299" height="522">

    <fx:Script>
        <![CDATA[
            
            private var client:Socket;
            protected function connetBt_clickHandler(event:MouseEvent):void
            {
                
                client = new Socket;
                
                if(ipTx.text == "")
                    ipTx.text = "127.0.0.1";
                if(portTx.text == "")
                    portTx.text = "1000";
                
                client.connect(ipTx.text , int(portTx.text));
                client.addEventListener(Event.CONNECT , serverConnect);
                client.addEventListener(ProgressEvent.SOCKET_DATA , onData);
                client.addEventListener(Event.CLOSE , disconnect);
                
                // TODO Auto-generated method stub
            }
            private function serverConnect(e:Event):void
            {
                traceBox.appendText("connect server" + "\n");
            }
            private function onData(e:ProgressEvent):void
            {
                trace("data");
                traceBox.appendText("c#Mrg : " + client.readUTFBytes(client.bytesAvailable) + "\n");
            }
            private function disconnect(e:Event):void
            {
                traceBox.appendText("close server" +"\n");
                client.close();
            }

            protected function sendBt_clickHandler(event:MouseEvent):void
            {
                if(sendTx.text == "")
                    sendTx.text = "mrg from As3";
                client.writeUTFBytes(sendTx.text + "\n");
                client.flush();
            }

        ]]>
    </fx:Script>

    <fx:Declarations>
        <!-- Place non-visual elements (e.g., services, value objects) here -->
    </fx:Declarations>
    <s:TextArea x="10" y="89" width="279" height="276" id="traceBox"/>
    <s:TextArea x="11" y="390" width="279" height="26" id="sendTx"/>
    <s:TextArea x="10" y="10" height="24" width="149" id="ipTx"/>
    <s:TextArea x="167" y="10" height="24" width="122" id="portTx"/>
    <s:Button x="10" y="42" label="connect" width="279" id="connetBt" click="connetBt_clickHandler(event)"/>
    <s:Button x="10" y="474" label="disconnect" width="279" id="disconnectBt"/>
    <s:Button x="10" y="425" label="sendMrg" width="279" id="sendBt" click="sendBt_clickHandler(event)"/>
</s:Application>
 
 
 
c#에서는 stream을 readLine 으로 받는다. 따라서 
as3 에도 string 값을 "  somstring  " + " \n" 으로 줄을 넘겨줘야 c#에서 read 할수 있다.
Yamecoder 야매코더_
tags : , , ,
C# 2010.12.04 00:14
Powerd by Tistory, designed by criuce
rss