2014-01-29 175 views
0

我试过这段代码来制作聊天应用程序。当我运行该应用程序我得到一个Exception: A request to send or receive data was disallowed because the socket is not connected和(使用sendto调用发送数据报套接字时)no address was suppliedC#中的聊天应用程序

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 client_chatApplication 
{ 
    public partial class Form1 : Form 
    { 
     Socket sck; 
     EndPoint epLocal, epRemote; 
     public Form1() 
     { 
      InitializeComponent(); 
      sck = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); 
      sck.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true); 
      textLocalIP.Text = GetLocalIp(); 
      textFriendsIP.Text = GetLocalIp(); 
     } 

     //to get the local ip address 
     private string GetLocalIp() 
     { 
      IPHostEntry host; 
      host = Dns.GetHostEntry(Dns.GetHostName()); 
      foreach (IPAddress ip in host.AddressList) 
      { 
       if (ip.AddressFamily == AddressFamily.InterNetwork) 
       { 
        return ip.ToString(); 
       } 
      } 
      return "127.0.0.1"; 
     } 

     public void MessageCallBack(IAsyncResult aresult) 
     { 
      try 
      { 
       int size = sck.EndReceiveFrom(aresult, ref epRemote); 
       if (size > 0) 
       { 
        byte[] receivedData = new byte[1464]; 

        receivedData = (byte[])aresult.AsyncState; 
        ASCIIEncoding eEncoding = new ASCIIEncoding(); 
        string receivedMessage = eEncoding.GetString(receivedData); 
        listMessage.Items.Add("fiend" + receivedMessage); 
       } 
       byte[] buffer = new byte[1500]; 
       sck.BeginReceiveFrom(buffer, 0, buffer.Length, SocketFlags.None, ref epRemote, new AsyncCallback(MessageCallBack), buffer); 
      } 
      catch (Exception exp) 
      { 
       MessageBox.Show(exp.ToString()); 

      } 
     } 
     private void label1_Click(object sender, EventArgs e) 
     { 

     } 

     private void listBox1_SelectedIndexChanged(object sender, EventArgs e) 
     { 

     } 

     private void button2_Click(object sender, EventArgs e) 
     { 

     } 

     private void textLocalIP_TextChanged(object sender, EventArgs e) 
     { 
      try 
      { 

       System.Text.ASCIIEncoding enc = new System.Text.ASCIIEncoding(); 
       byte[] msg = new byte[1500]; 
       msg = enc.GetBytes(textMessage.Text); 
       sck.Send(msg); 
       listMessage.Items.Add("yoy" + textMessage.Text); 
       textMessage.Clear(); 

      } 
      catch (Exception exp) 
      { 
       MessageBox.Show(exp.ToString()); 
      } 
     } 

     private void button1_Click(object sender, EventArgs e) 
     { 
      try 
      { 
       epLocal = new IPEndPoint(IPAddress.Parse(textLocalIP.Text), Convert.ToInt32(textLocalPort.Text)); 
       sck.Bind(epLocal); 

       epRemote = new IPEndPoint(IPAddress.Parse(textFriendsIP.Text), Convert.ToInt32(textFriendsPort.Text)); 
       sck.Bind(epRemote); 

       byte[] buffer = new byte[1500]; 
       Socket dataSocket = AsyncSocket.EndAccept(_IAsyncResult); 
       sck.BeginReceiveFrom(buffer, 0, buffer.Length, SocketFlags.None, ref epRemote, new AsyncCallback(MessageCallBack), buffer); 
       button1.Text = "connected"; 
       button1.Enabled = false; 
       button2.Enabled = true; 
       textMessage.Focus(); 
      } 

      catch (Exception ex) 
      { 
       MessageBox.Show(ex.ToString()); 

      } 
     } 
    } 
} 
+2

那么,什么是你的问题? –

+0

我的问题是如何去除我得到的异常? – user2775241

回答

0

您创建的插座,但你不创建后它绑定到一个端口。您想要发送数据时将其绑定。

仅当您想要发送数据时绑定是不正确的行为。

您还必须将套接字绑定到本地IPEndpoint。当您使用功能SendTo()发送数据报时,使用远程IPEndpoint

+0

谢谢,能否请您告诉我,为了使代码正常工作,我必须做些什么修改。我是新手。 – user2775241

0

与插座涉及以下基本步骤:

创建插座

  • 绑定到网络接口
  • 监听连接或建立套接字连接
  • 使接收和发送呼叫

一旦你完成了前三个ste ps,套接字连接已经完成,您将需要使用该连接进行发送和接收系统调用。这意味着,前三个列出的要点是“只做一次”,您将使用它进行发送和接收呼叫任意次数直到连接终于关闭。

This link将帮助您进一步..

+0

谢谢you.please告诉我在我的程序中必须进行哪些修正才能使代码正常工作。 – user2775241