C# C语言中的聊天应用
我已经尝试了这段代码来制作聊天应用程序。当我运行应用程序时,我遇到了一个异常:发送或接收数据的请求被禁止,因为套接字未连接,并且在使用sendto调用发送数据报套接字时,没有提供地址C# C语言中的聊天应用,c#,winforms,sockets,chat,C#,Winforms,Sockets,Chat,我已经尝试了这段代码来制作聊天应用程序。当我运行应用程序时,我遇到了一个异常:发送或接收数据的请求被禁止,因为套接字未连接,并且在使用sendto调用发送数据报套接字时,没有提供地址 您创建了套接字,但在创建后没有将其绑定到端口。当您想要发送数据时,可以绑定它 仅当您希望发送数据时绑定是不正确的行为 您还只需将套接字绑定到本地IPEndpoint。使用此功能发送数据报时,将使用远程IPEndpoint。使用套接字包括以下基本步骤: 创建套接字 绑定到网络接口 监听连接或进行套接字连接 拨打收发电
您创建了套接字,但在创建后没有将其绑定到端口。当您想要发送数据时,可以绑定它 仅当您希望发送数据时绑定是不正确的行为
您还只需将套接字绑定到本地IPEndpoint。使用此功能发送数据报时,将使用远程IPEndpoint。使用套接字包括以下基本步骤: 创建套接字 绑定到网络接口 监听连接或进行套接字连接 拨打收发电话 完成前三个步骤后,将建立套接字连接,您需要使用该连接进行发送和接收系统调用。这意味着,前三个列出的点仅执行一次,您将使用它进行任意次数的发送和接收调用,直到连接最终关闭
将进一步帮助您..那么,您的问题是什么?我的问题是如何删除我遇到的异常?谢谢,您能告诉我为了使代码正常工作,我必须对代码进行哪些修改吗。我是新手。谢谢。请告诉我,为了让代码正常工作,我必须在程序中进行哪些更正。
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());
}
}
}
}