小弟我刚开始学习C# Socket,遇到了不解的问题,请各位高手指教,我的代码如下:
Socket socketSend = new Socket AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);
byte[] msg = System.Text.Encoding.ASCII.GetBytes(txtSend.Text.Trim());
IPHostEntry IPHE = System.Net.Dns.Resolve(System.Net.Dns.GetHostName());
IPAddress IPAD = IPHE.AddressList[0];
string dd = IPAD.ToString();
IPEndPoint IPEP = new IPEndPoint(IPAD,1979);
socketSend.Connect(IPEP);
int count = socketSend.Send(msg);
socketSend.Shutdown(SocketShutdown.Both);
socketSend.Close();
以上代码运行正常,但我想接受刚发出的消息时却遇到了"通常每个套接字地址(协议/网络地址/端口)只允许使用一次"的问题,不解,代码如下,请各位高手忙我看看,在此多谢了.
Socket socketRecieve = new Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);
IPHostEntry IPHE = System.Net.Dns.Resolve(System.Net.Dns.GetHostName());
IPAddress IPAD = IPHE.AddressList[0];
IPEndPoint IPEP = new IPEndPoint(IPAD,1979);
socketRecieve.Bind(IPEP);
socketRecieve.Listen(10);//在该处出的弹出提示"....如上"
Socket hanlder = socketRecieve.Accept();
byte[] RecieveMsg = new byte[1024];
int count = hanlder.Receive(RecieveMsg);
string str = System.Text.Encoding.ASCII.GetString(RecieveMsg,0,count);
txtReceive.Text = str;
hanlder.Shutdown(SocketShutdown.Both);
hanlder.Close();小弟焦急等待,多谢各位高手了

解决方案 »

  1.   

    小弟我刚开始学习C# Socket,遇到了不解的问题,请各位高手指教,我的代码如下:
    Socket socketSend = new Socket AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);
    byte[] msg = System.Text.Encoding.ASCII.GetBytes(txtSend.Text.Trim());
    IPHostEntry IPHE = System.Net.Dns.Resolve(System.Net.Dns.GetHostName());
    IPAddress IPAD = IPHE.AddressList[0];
    string dd = IPAD.ToString();
    IPEndPoint IPEP = new IPEndPoint(IPAD,1979); //这句,改为
     //socketSend.Connect(192.168.0.1(服务器IP),1979);
    socketSend.Connect(IPEP);
    int count = socketSend.Send(msg);
    socketSend.Shutdown(SocketShutdown.Both);
    socketSend.Close();
      

  2.   

    加个循环
    IPHostEntry iphost=Dns.Resolve(System.Environment.MachineName);
    IPAddress ipadr=iphost.AddressList[0];
    IPEndPoint ipendpoint=new IPEndPoint(ipadr,10000);//终端
    this.statusBar1.Panels[0].Text=" 正在侦听....";
    this.button1.Enabled=false;

               int number=10;

    //create a tcp/ip socker
    Socket socket1=new Socket(System.Net.Sockets.AddressFamily.InterNetwork,SocketType.Stream,System.Net.Sockets.ProtocolType.Tcp); try
    {
    //绑定端口
    socket1.Bind(ipendpoint);

    //开始侦听
    socket1.Listen(number);
    while(true)
    {
    //program is suspended while waiting for an incoming connection
    Socket mys=socket1.Accept();
    string data=null;
    Application.DoEvents();
    //接受数据
    while(true)
    {
    Application.DoEvents();
    byte [] box=new byte[1024];
    int len=mys.Receive(box);//len为字节数
    data+=Encoding.Default.GetString(box,0,len);
    if(data.IndexOf("<TheEnd>")>=0)
    {

    break;
    }

    }
    data=data.Substring(0,data.Length-8);
    this.richTextBox1.Text=data;
    data=null;
    string s_reply="has recevied";
    byte []msg=Encoding.Default.GetBytes(s_reply);
    mys.Send(msg);
     MessageBox.Show("已经接受到!");
    mys.Shutdown(SocketShutdown.Both);
    Application.DoEvents();
    mys.Close();
    }
    }
     
    catch(SocketException e0)
    {
                      MessageBox.Show(e0.Message.ToString());
    }
    catch(ArgumentNullException e1)
    {
    MessageBox.Show(e1.Message.ToString());
    }
    catch(Exception e2)
    {
    MessageBox.Show(e2.Message.ToString());
    }