在socket通信中连接建立好,怎么才能实现让客户端和服务器端一直监听端口?除了用timer,我不知道一般的软件,譬如qq之类的即时通信软件用的是不是类似TImer的东西刷的,我感觉应该不会,太弱智了点吧。请教下,如何实现的?(我也不知道我说的清楚不清楚,就是类似于串口通信中的DataReceived方法似的,打开串口,这个监听一直存在,只要串口有数据上来就会收到,像这样的怎么能实现?)

解决方案 »

  1.   

    我觉得从软件效率的角度考虑还是用VC加汇编作这个比较好c#对这种截获数据包的操作性能相对还是低一些如果非要用c#作,还是做个服务吧,当个守护进程放在客户端
      

  2.   

    呵呵,我只是自己做着玩的,类似于qq这种网络通信的小东西,感觉还到不ly302说的这个地步吧,纯属个人感觉,
      

  3.   

    codeproject上有很多
    自己搜索下An Asynchronous Socket Server and Client
    By Andre Azevedo
    An asynchronous socket server and client with encryption and compression.
    http://www.codeproject.com/KB/IP/AsyncSocketServerandClien.aspxA Chat Application Using Asynchronous UDP sockets
    By Hitesh Sharma
    This article shows how to develop a chat application using UDP sockets.
    http://www.codeproject.com/KB/IP/ChatAppAsynchUDPSocks.aspx
      

  4.   

    主要就是socket监听端口
    http://www.codeproject.com/KB/IP/serversocket.aspx
      

  5.   

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;
    using System.Net;
    using System.Net.Sockets;
    using System.Threading;namespace WindowsApplication1
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
                 CheckForIllegalCrossThreadCalls = false;
            }         Socket ClientServer;//定义全局变量
            /// <summary>
            /// 连接
            /// </summary>
            /// <param name="sender"></param>
            /// <param name="e"></param>
            private void button1_Click(object sender, EventArgs e)
            {
                Thread th = new Thread(new ThreadStart(start));
                th.IsBackground = true;
                th.Start();
            }        private void button2_Click(object sender, EventArgs e)
            {
                byte[] Data = UnicodeEncoding.UTF8.GetBytes(this.textBox4.Text+"说:"+this.textBox3.Text+"\n");
                ClientServer.Send(Data);
                this.richTextBox1.AppendText(this.textBox3.Text+"\n");
             }
             void start()
            {
                IPAddress ip=IPAddress.Parse(this.textBox1.Text);
                int port=int.Parse(this.textBox2.Text);
                IPEndPoint MyIPEnd = new IPEndPoint(ip, port);
                ClientServer = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                try
                {
                    ClientServer.Connect(MyIPEnd);
                    MessageBox.Show("连接成功!");
                    this.button2.Enabled = true;
                    this.toolStripStatusLabel1.Text = "与服务器建立了连接!";
                    while (true)
                    {
                        byte[] msg = new byte[1024];
                        int i = ClientServer.Receive(msg);
                        string str = UnicodeEncoding.UTF8.GetString(msg, 0, i);
                        this.richTextBox1.AppendText(str);
                    }            }
                catch
                {
                    MessageBox.Show("无法建立连接!");
                }        }        private void Form1_Load(object sender, EventArgs e)
            {
                this.button2.Enabled = false;
            }
        }
    }
      

  6.   

    const   int   portNumber   =   13;   
                  TcpListener   tcpListener   =   new   TcpListener(portNumber);   
        
                  tcpListener.Start();   
        
      Console.WriteLine("Waiting   for   a   connection....");   
            
      try{   
        
                //Accept   the   pending   client   connection   and   return   a   TcpClient   initialized   for   communication.   
                TcpClient   tcpClient   =   tcpListener.AcceptTcpClient();   
                            Console.WriteLine("Connection   accepted.");   
        
                            NetworkStream   networkStream   =   tcpClient.GetStream();   
        
                string   responseString   =   "You   have   successfully   connected   to   me";   
        
                Byte[]   sendBytes   =   Encoding.ASCII.GetBytes(responseString);   
                networkStream.Write(sendBytes,   0,   sendBytes.Length);   
        
                Console.WriteLine("Message   Sent   />   :   "   +   responseString);   
                  
        //Any   communication   with   the   remote   client   using   the   TcpClient   can   go   here.   
        //   
        ////////   
        
        //Close   TcpListener   and   TcpClient.   
        tcpClient.Close();   
                            tcpListener.Stop();   
        
        }   
        catch   (Exception   e)   {   
                                        Console.WriteLine(e.ToString());   
                            }