我刚开始学SOCKET,写了个简单的SOCKET,为什么只能连接一次?
//服务端
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.Sockets;
using System.Net;namespace WindowsApplication2
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            int post = 3333;
            string host = "192.168.1.22";
            IPAddress ip = IPAddress.Parse(host);
            IPEndPoint ipe = new IPEndPoint(ip, post);
            Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            s.Bind(ipe);
            s.Listen(100);
            Socket temp = s.Accept();
            byte[] recBytes = new byte[1024];
            int bytes;
            bytes = temp.Receive(recBytes, recBytes.Length, 0);
            recvBox.Text += Encoding.ASCII.GetString(recBytes, 0, bytes);
            string sendstr = "ok";
            byte[] bs = Encoding.ASCII.GetBytes(sendstr);
            temp.Send(bs, bs.Length, 0);
            temp.Close();
            s.Close();
 }
        private void Form1_Load(object sender, EventArgs e)
        {        }
    }
}//客户端
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.Sockets;
using System.Net;namespace WindowsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private void Form1_Load(object sender, EventArgs e)
        {
        }
        private void button1_Click(object sender, EventArgs e)
        {
            
            int port = 3333;
            string host = "192.168.1.22";
            IPAddress ip = IPAddress.Parse(host);
            IPEndPoint ipe = new IPEndPoint(ip, port);
            Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            s.Connect(ipe);
            string sendstr = customerid.Text;
            byte[] bs = Encoding.ASCII.GetBytes(sendstr);
            s.Send(bs, bs.Length, 0);
            string recvstr = "";
            byte[] recvBytes = new byte[1024];
            int bytes;
            bytes = s.Receive(recvBytes,recvBytes .Length ,0);
            recvstr += Encoding.ASCII.GetString(recvBytes, 0, bytes);
            MessageBox.Show(recvstr);
            s.Close();
        }    }
}

解决方案 »

  1.   

    while(true)
    {
                Socket temp = s.Accept(); 
                byte[] recBytes = new byte[1024]; 
                int bytes; 
                bytes = temp.Receive(recBytes, recBytes.Length, 0); 
                recvBox.Text += Encoding.ASCII.GetString(recBytes, 0, bytes); 
                string sendstr = "ok"; 
                byte[] bs = Encoding.ASCII.GetBytes(sendstr); 
                temp.Send(bs, bs.Length, 0); 
    }
      

  2.   


      static void Main()
      {
       try
       {
           while (true)
           {
               int port = 2000;
               string host = "127.0.0.1";
               IPAddress ip = IPAddress.Parse(host);
               IPEndPoint ipe = new IPEndPoint(ip, port);
               Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);//创建一个Socket类
               s.Bind(ipe);//绑定2000端口
               s.Listen(0);//开始监听
               Console.WriteLine("Wait for connect");
               Socket temp = s.Accept();//为新建连接创建新的Socket。
               Console.WriteLine("Get a connect");
               string recvStr = "";
               byte[] recvBytes = new byte[1024];
               int bytes;
               bytes = temp.Receive(recvBytes, recvBytes.Length, 0);//从客户端接受信息
               recvStr += Encoding.ASCII.GetString(recvBytes, 0, bytes);
               
               Console.WriteLine("Server Get Message:{0}", recvStr);//把客户端传来的信息显示出来
               File.AppendText("d:\\1.txt");
               string sendStr = "Ok!Client Send Message Sucessful!";
               byte[] bs = Encoding.ASCII.GetBytes(sendStr);
               temp.Send(bs, bs.Length, 0);//返回客户端成功信息
               temp.Close();
               s.Close();
           }
       }
       catch (ArgumentNullException e)
       {
        Console.WriteLine("ArgumentNullException: {0}", e);
       }
       catch (SocketException e)
       {
        Console.WriteLine("SocketException: {0}", e);
    }
       Console.WriteLine("Press Enter to Exit");
       Console.ReadLine();
      }
      

  3.   

    这样下面temp.close();
    会找不到对象temp.
      

  4.   

    还有你为什么要把服务器端得代码写在窗口的构造函数里?直接写在load里面就行了啊
      

  5.   

    写在LOAD里,还是不行。就是我客户端发送信息,我服务端没有窗口界面显示,但有返回客户端信息。
      

  6.   

    没用线程么线程和socket基本不分家的
      

  7.   


    就好比你用WORD打印文档,如果打印的时候你的WORD呈死机状态,你也受不了。所以打印部分都是另开一个线程去处理。 你这个也是一样。
      

  8.   

    就是同步才要用到线程,不然socket侦听是会堵塞的。
      

  9.   

    同步要用新开线程,不然receive会一直阻止