求C#版的wince socket server and client源码,谢谢

解决方案 »

  1.   

    dll要不? 同时支持IPV6 双栈路由
      

  2.   

    Server:using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;using System.Net;
    using System.Net.Sockets;
    using System.Threading;namespace SocketServer
    {
        /// <summary>
        /// 声明一个委托
        /// 用于在线程中
        /// 访问客户端界
        /// 面的
        /// </summary>
        /// <param name="text"></param>
        delegate void SetText(string text);
        public partial class SocketForm : Form
        {
            /// <summary>
            /// 字节数组最大长度
            /// </summary>
            const int MAXBUFFERSIZE = 1024 * 1024;
            /// <summary>
            /// 声明本地IP地址
            /// </summary>
            IPAddress ipAdress;
            /// <summary>
            /// 声明端口号
            /// </summary>
            int port;
            /// <summary>
            /// 本机节点
            /// </summary>
            EndPoint endPoint;
            /// <summary>
            /// 用于连接监听的
            /// </summary>
            Socket listenSocket;
            /// <summary>
            /// 用于接收发送的
            /// </summary>
            Socket acceptSocket;
            /// <summary>
            /// 执行监听的线程
            /// </summary>
            Thread thread;
            public SocketForm()
            {
                InitializeComponent();
            }
            /// <summary>
            /// 此方法用于为文本框赋值
            /// </summary>
            void DoSetText(string text)
            {
                textBox3.Text = text;
            }
            private void button1_Click(object sender, EventArgs e)
            {
                ///创建一个线程,后台执行
                ///如果不创建一个线程,窗体
                ///则假死机
                thread = new Thread(new ThreadStart(BeginListen));
                thread.Start();
                ///可以试一试这个
                ///如果不用线程,直接用下
                ///面注释的方法,页面将空白
                //BeginListen();
                button1.Enabled = false;
                button2.Enabled = true;
            }
            /// <summary>
            /// 监听的方法
            /// </summary>
            private void BeginListen()
            {
                ///IP地址
                ipAdress = IPAddress.Parse(textBox1.Text);
                ///端口号
                port = Convert.ToInt32(textBox2.Text);
                ///本机节点
                endPoint = new IPEndPoint(ipAdress, port);
                ///声明监听Socket实例
                using (listenSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp))
                {
                    ///绑定Socket与本机实例
                    listenSocket.Bind(endPoint);
                    ///开始监听
                    ///10为监听的最大数(有多少个连接正在等待)
                    listenSocket.Listen(10);
                    ///等待连接,当有连接时,分配一个Socket与客户端通信
                    using (acceptSocket = listenSocket.Accept())
                    {
                        ///声明网络流,用于读取写入数据
                        //NetworkStream networkStream = new NetworkStream(acceptSocket);
                        ///从流中读取数据,此操作会阻塞当前线程执行、
                        ///声明字节数组,用于从流中读取数据
                        byte[] buffer = new byte[MAXBUFFERSIZE];
                        int readLength;
                        do
                        {
                            ///接收消息
                            readLength = acceptSocket.Receive(buffer);
                            ///readLength = networkStream.Read(buffer, 0, buffer.Length);
                            ///将字节数组解码,得到传输内容
                            string message = Encoding.BigEndianUnicode.GetString(buffer).Replace("\0","");
                            ///显示内容
                            SetText setText = new SetText(DoSetText);
                            this.Invoke(setText, message);
                            Application.DoEvents();
                            buffer = new byte[MAXBUFFERSIZE];
                        } while (readLength > 0);
                    }
                }
            }
            /// <summary>
            /// 退出时判断是否有线程或者socket处于未关闭状态
            /// </summary>
            private void SocketForm_FormClosing(object sender, FormClosingEventArgs e)
            {
                if (thread != null)
                {
                    thread.Abort();
                }
                if (listenSocket != null)
                {
                    listenSocket.Close();
                }
                if (acceptSocket != null)
                {
                    acceptSocket.Close();
                }
            }
            /// <summary>
            /// 停止监听
            /// </summary>
            private void button2_Click(object sender, EventArgs e)
            {
                if (thread != null)
                {
                    thread.Abort();
                }
                if (listenSocket != null)
                {
                    listenSocket.Close();
                }
                if (acceptSocket != null)
                {
                    acceptSocket.Close();
                }
                button1.Enabled = true;
                button2.Enabled = false;
            }
        }
    }Client:using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;using System.Net;
    using System.Net.Sockets;namespace SoketClient
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
            Socket socket;
            /// <summary>
            /// 连接服务端
            /// </summary>
            private void button1_Click(object sender, EventArgs e)
            {
                IPAddress ipAddress = IPAddress.Parse(textBox1.Text);
                int port = int.Parse(textBox2.Text);
                EndPoint endPoint = new IPEndPoint(ipAddress, port);
                socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                try
                {
                    socket.Connect(endPoint); 
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                    socket.Close();
                }
                if (socket.Connected)
                {
                    button1.Enabled = false;
                    button2.Enabled = true;
                }
            }
            /// <summary>
            /// 发送消息
            /// </summary>
            private void button2_Click(object sender, EventArgs e)
            {
                byte[] buffer = Encoding.BigEndianUnicode.GetBytes(textBox3.Text);
                //NetworkStream stream = new NetworkStream(socket);
                //stream.Write(buffer, 0, buffer.Length);
                //stream.Close();
                try
                {
                    socket.Send(buffer);
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
            {
                if (socket != null)
                {
                    socket.Close();
                }
            }    }
    }
      

  3.   

    Server:using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;using System.Net;
    using System.Net.Sockets;
    using System.Threading;namespace SocketServer
    {
        /// <summary>
        /// 声明一个委托
        /// 用于在线程中
        /// 访问客户端界
        /// 面的
        /// </summary>
        /// <param name="text"></param>
        delegate void SetText(string text);
        public partial class SocketForm : Form
        {
            /// <summary>
            /// 字节数组最大长度
            /// </summary>
            const int MAXBUFFERSIZE = 1024 * 1024;
            /// <summary>
            /// 声明本地IP地址
            /// </summary>
            IPAddress ipAdress;
            /// <summary>
            /// 声明端口号
            /// </summary>
            int port;
            /// <summary>
            /// 本机节点
            /// </summary>
            EndPoint endPoint;
            /// <summary>
            /// 用于连接监听的
            /// </summary>
            Socket listenSocket;
            /// <summary>
            /// 用于接收发送的
            /// </summary>
            Socket acceptSocket;
            /// <summary>
            /// 执行监听的线程
            /// </summary>
            Thread thread;
            public SocketForm()
            {
                InitializeComponent();
            }
            /// <summary>
            /// 此方法用于为文本框赋值
            /// </summary>
            void DoSetText(string text)
            {
                textBox3.Text = text;
            }
            private void button1_Click(object sender, EventArgs e)
            {
                ///创建一个线程,后台执行
                ///如果不创建一个线程,窗体
                ///则假死机
                thread = new Thread(new ThreadStart(BeginListen));
                thread.Start();
                ///可以试一试这个
                ///如果不用线程,直接用下
                ///面注释的方法,页面将空白
                //BeginListen();
                button1.Enabled = false;
                button2.Enabled = true;
            }
            /// <summary>
            /// 监听的方法
            /// </summary>
            private void BeginListen()
            {
                ///IP地址
                ipAdress = IPAddress.Parse(textBox1.Text);
                ///端口号
                port = Convert.ToInt32(textBox2.Text);
                ///本机节点
                endPoint = new IPEndPoint(ipAdress, port);
                ///声明监听Socket实例
                using (listenSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp))
                {
                    ///绑定Socket与本机实例
                    listenSocket.Bind(endPoint);
                    ///开始监听
                    ///10为监听的最大数(有多少个连接正在等待)
                    listenSocket.Listen(10);
                    ///等待连接,当有连接时,分配一个Socket与客户端通信
                    using (acceptSocket = listenSocket.Accept())
                    {
                        ///声明网络流,用于读取写入数据
                        //NetworkStream networkStream = new NetworkStream(acceptSocket);
                        ///从流中读取数据,此操作会阻塞当前线程执行、
                        ///声明字节数组,用于从流中读取数据
                        byte[] buffer = new byte[MAXBUFFERSIZE];
                        int readLength;
                        do
                        {
                            ///接收消息
                            readLength = acceptSocket.Receive(buffer);
                            ///readLength = networkStream.Read(buffer, 0, buffer.Length);
                            ///将字节数组解码,得到传输内容
                            string message = Encoding.BigEndianUnicode.GetString(buffer).Replace("\0","");
                            ///显示内容
                            SetText setText = new SetText(DoSetText);
                            this.Invoke(setText, message);
                            Application.DoEvents();
                            buffer = new byte[MAXBUFFERSIZE];
                        } while (readLength > 0);
                    }
                }
            }
            /// <summary>
            /// 退出时判断是否有线程或者socket处于未关闭状态
            /// </summary>
            private void SocketForm_FormClosing(object sender, FormClosingEventArgs e)
            {
                if (thread != null)
                {
                    thread.Abort();
                }
                if (listenSocket != null)
                {
                    listenSocket.Close();
                }
                if (acceptSocket != null)
                {
                    acceptSocket.Close();
                }
            }
            /// <summary>
            /// 停止监听
            /// </summary>
            private void button2_Click(object sender, EventArgs e)
            {
                if (thread != null)
                {
                    thread.Abort();
                }
                if (listenSocket != null)
                {
                    listenSocket.Close();
                }
                if (acceptSocket != null)
                {
                    acceptSocket.Close();
                }
                button1.Enabled = true;
                button2.Enabled = false;
            }
        }
    }Client:using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;using System.Net;
    using System.Net.Sockets;namespace SoketClient
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
            Socket socket;
            /// <summary>
            /// 连接服务端
            /// </summary>
            private void button1_Click(object sender, EventArgs e)
            {
                IPAddress ipAddress = IPAddress.Parse(textBox1.Text);
                int port = int.Parse(textBox2.Text);
                EndPoint endPoint = new IPEndPoint(ipAddress, port);
                socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                try
                {
                    socket.Connect(endPoint); 
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                    socket.Close();
                }
                if (socket.Connected)
                {
                    button1.Enabled = false;
                    button2.Enabled = true;
                }
            }
            /// <summary>
            /// 发送消息
            /// </summary>
            private void button2_Click(object sender, EventArgs e)
            {
                byte[] buffer = Encoding.BigEndianUnicode.GetBytes(textBox3.Text);
                //NetworkStream stream = new NetworkStream(socket);
                //stream.Write(buffer, 0, buffer.Length);
                //stream.Close();
                try
                {
                    socket.Send(buffer);
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
            {
                if (socket != null)
                {
                    socket.Close();
                }
            }    }
    }
      

  4.   

    那我就发个异步接受有发送的吧:
    Client:
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Net;
    using System.Net.Sockets;
    using System.Threading;
    namespace MyClient
    {
        class Program
        {
            public static ManualResetEvent ConnectDone = new ManualResetEvent(false);
            public static ManualResetEvent SendDone = new ManualResetEvent(false);
            public static ManualResetEvent ReceiveDone = new ManualResetEvent(false);
            static string content = string.Empty;
            static byte[] buffer = new byte[1024];
            static void Main(string[] args)
            {
                IPAddress ip = IPAddress.Parse("127.0.0.1");
                IPEndPoint endpoint = new IPEndPoint(ip, 8000);
                Socket Client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                Client.BeginConnect(endpoint, new AsyncCallback(ConnectCallBack), Client);
                ConnectDone.WaitOne();            string sendstring = "this is a book hahha.";
                byte[] sendbytes = Encoding.ASCII.GetBytes(sendstring);
                Client.BeginSend(sendbytes, 0, sendbytes.Length, 0, new AsyncCallback(SendCallBack), Client);
                SendDone.WaitOne();            Client.BeginReceive(buffer, 0, buffer.Length, 0, new AsyncCallback(ReceiveCallBack), Client);
                ReceiveDone.WaitOne();
                Console.WriteLine("Receive from Server Data:\n");
                Console.WriteLine(content);
                Client.Shutdown(SocketShutdown.Both);
                Client.Close();
                Console.Read();
            }
            private static void ConnectCallBack(IAsyncResult result)
            {
                Console.WriteLine("Connecting...");
                Socket handler = (Socket)result.AsyncState;
                handler.EndConnect(result);
                Console.WriteLine("Connected Finished");
                ConnectDone.Set();
            }
            private static void SendCallBack(IAsyncResult result)
            {
                Console.WriteLine("Start to Sending..");
                Socket handler = (Socket)result.AsyncState;
                int sendbytes = handler.EndSend(result);
                Console.WriteLine("Send finish.{0} bytes has send", sendbytes);
                SendDone.Set();
            }
            private static void ReceiveCallBack(IAsyncResult result)
            {
                Console.WriteLine("Start to receive...");
                Socket handler = (Socket)result.AsyncState;
                int receivebytes = handler.EndReceive(result);
                if (receivebytes > 0)
                {
                    content += Encoding.ASCII.GetString(buffer);
                    handler.BeginReceive(buffer, 0, receivebytes, 0, new AsyncCallback(ReceiveCallBack), handler);
                }
                else
                {
                    ReceiveDone.Set();
                }
            }
        }
    }serverusing System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Net;
    using System.Net.Sockets;
    using System.Threading;
    namespace myServer
    {
        class Program
        {
            static ManualResetEvent EventDone = new ManualResetEvent(false);
            static byte[] buffer = new byte[1024];
            static string content;
            static void Main(string[] args)
            {
                IPEndPoint endpoint = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 8000);
                Socket server = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                server.Bind(endpoint);
                server.Listen(10);
                server.BeginAccept(new AsyncCallback(AccepteCallBack), server);
                EventDone.WaitOne();
                Console.Read();
            }
            private static void AccepteCallBack(IAsyncResult result)
            {
                Console.WriteLine("Waiting for Connect...");
                Socket handler = (Socket)result.AsyncState;
                Socket acceptsocket = handler.EndAccept(result);
                acceptsocket.BeginReceive(buffer, 0, buffer.Length, 0, new AsyncCallback(ReceiveCallBack), acceptsocket);
            }
            private static void ReceiveCallBack(IAsyncResult result)
            {
                Console.WriteLine("Start to Receiving...");
                Socket handler = (Socket)result.AsyncState;
                int bytescount = handler.EndReceive(result);
                if (bytescount > 0)
                {
                    content += Encoding.ASCII.GetString(buffer);
                    if (content.IndexOf(".") > -1)
                    {
                        Console.WriteLine("Read {0} bytes from socket.\n Data:{1}", content.Length, content.Trim());
                        byte[] byteData = Encoding.ASCII.GetBytes(content);
                        handler.BeginSend(byteData, 0, byteData.Length, 0, new AsyncCallback(SendCallBack), handler);
                    }
                    else
                        handler.BeginReceive(buffer, 0, buffer.Length, 0, new AsyncCallback(ReceiveCallBack), handler);
                }
                //else
                //{
                //    Console.WriteLine("Receive from client.Data:");
                //    Console.WriteLine(content);
                //    string serversendString="I have Receive you Data...";
                //    byte[] byteserversend=Encoding.ASCII.GetBytes(serversendString);
                //    handler.BeginSend(byteserversend, 0, byteserversend.Length, 0, new AsyncCallback(SendCallBack), handler);
                //}
            }
            private static void SendCallBack(IAsyncResult result)
            {
                Console.WriteLine("Receive Finished");
                Socket handler = (Socket)result.AsyncState;
                int bytecount = handler.EndSend(result);
                Console.WriteLine("{0} bytes data have Send", bytecount);
                handler.Shutdown(SocketShutdown.Both);
                handler.Close();
                EventDone.Set();            //Console.WriteLine("Send CallBack Thread ID:" + AppDomain.GetCurrentThreadId());
                //Socket handler = (Socket)result.AsyncState;
                //int byteSent = handler.EndSend(result);
                //Console.WriteLine("Send {0} bytes to Client", byteSent);
                //handler.Shutdown(SocketShutdown.Both);
                //handler.Close();
                //socketEvent.Set();
            }    }
    }
      

  5.   

    需要能在wince下面运行的代码,谢谢
      

  6.   

    http://topic.csdn.net/u/20080623/08/4bbd2475-45f1-42e3-a613-16b094759ade.html
      

  7.   

    SOCKET WINCE我做过,和电脑PC的一样,你就查SOCKET就行了,一样的
    最关键的是:
    兄弟我没分了,咋还得有分才能发帖子呢?   能给我点不?
    兄弟我没分了,咋还得有分才能发帖子呢?   能给我点不?
    兄弟我没分了,咋还得有分才能发帖子呢?   能给我点不?
    兄弟我没分了,咋还得有分才能发帖子呢?   能给我点不?
    兄弟我没分了,咋还得有分才能发帖子呢?   能给我点不?
    兄弟我没分了,咋还得有分才能发帖子呢?   能给我点不?
    兄弟我没分了,咋还得有分才能发帖子呢?   能给我点不?
    兄弟我没分了,咋还得有分才能发帖子呢?   能给我点不?
    兄弟我没分了,咋还得有分才能发帖子呢?   能给我点不?
    兄弟我没分了,咋还得有分才能发帖子呢?   能给我点不?
    兄弟我没分了,咋还得有分才能发帖子呢?   能给我点不?
    兄弟我没分了,咋还得有分才能发帖子呢?   能给我点不?
    兄弟我没分了,咋还得有分才能发帖子呢?   能给我点不?
    兄弟我没分了,咋还得有分才能发帖子呢?   能给我点不?
    兄弟我没分了,咋还得有分才能发帖子呢?   能给我点不?
    兄弟我没分了,咋还得有分才能发帖子呢?   能给我点不?
    兄弟我没分了,咋还得有分才能发帖子呢?   能给我点不?
    兄弟我没分了,咋还得有分才能发帖子呢?   能给我点不?
    兄弟我没分了,咋还得有分才能发帖子呢?   能给我点不?
    兄弟我没分了,咋还得有分才能发帖子呢?   能给我点不?
    兄弟我没分了,咋还得有分才能发帖子呢?   能给我点不?
    兄弟我没分了,咋还得有分才能发帖子呢?   能给我点不?
    兄弟我没分了,咋还得有分才能发帖子呢?   能给我点不?
    兄弟我没分了,咋还得有分才能发帖子呢?   能给我点不?
    兄弟我没分了,咋还得有分才能发帖子呢?   能给我点不?
    兄弟我没分了,咋还得有分才能发帖子呢?   能给我点不?
    兄弟我没分了,咋还得有分才能发帖子呢?   能给我点不?
    兄弟我没分了,咋还得有分才能发帖子呢?   能给我点不?
    兄弟我没分了,咋还得有分才能发帖子呢?   能给我点不?
    兄弟我没分了,咋还得有分才能发帖子呢?   能给我点不?
    兄弟我没分了,咋还得有分才能发帖子呢?   能给我点不?
    兄弟我没分了,咋还得有分才能发帖子呢?   能给我点不?
    兄弟我没分了,咋还得有分才能发帖子呢?   能给我点不?
    兄弟我没分了,咋还得有分才能发帖子呢?   能给我点不?
    兄弟我没分了,咋还得有分才能发帖子呢?   能给我点不?