这是我的一个简单聊天程序的代码,它可以为客户端提供服务。问题是当我点击发送按钮时会抛出错误。
错误是......
System.Net.Sockets.SocketException(0x80004005):在ChatClientV1.MainForm.MessageCallBack(IAsynResult aResult)的System.Net.Sockets.SocketEndRecieveFrom(IAsyncResult asyncResult,EndPoint& endPoint)上的远程主机强制关闭现有连接。 ......下面的第36行是......
int size = sck.EndReceiveFrom(aResult,ref epRemote);
public MainForm()
{
InitializeComponent();
sck = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
sck.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
//localIP = GetLocalIP();
//friendIP = GetLocalIP();
localIP = "192.168.0.17";
friendIP = "192.168.0.17";
friendPortNumber = "81";
localPortNumber = "80";
username = "Shawn";
}
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";
}
private void MessageCallBack(IAsyncResult aResult)
{
try
{
int size = sck.EndReceiveFrom(aResult, ref epRemote);
if (size > 0)
{
byte[] recieveData = new byte[1464];
recieveData = (byte[])aResult.AsyncState;
ASCIIEncoding eEncoding = new ASCIIEncoding();
string recieveMessage = eEncoding.GetString(recieveData);
messageListBox.Items.Add(username+": "+recieveMessage);
}
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 currentSettingsToolStripMenuItem_Click(object sender, EventArgs e)
{
CurrentSettingsFrom aCurrentSettingsForm = new CurrentSettingsFrom();
aCurrentSettingsForm.ShowDialog();
}
private void sendFileToolStripMenuItem_Click(object sender, EventArgs e)
{
SendFileForm aSendFileForm = new SendFileForm();
aSendFileForm.ShowDialog();
}
private void exitToolStripMenuItem_Click(object sender, EventArgs e)
{
this.Close();
}
private void appearanceToolStripMenuItem_Click(object sender, EventArgs e)
{
AppearanceForm aAppearanceFrom = new AppearanceForm();
aAppearanceFrom.ShowDialog();
}
private void connectButton_Click(object sender, EventArgs e)
{
try
{
eplocal = new IPEndPoint(IPAddress.Parse(localIP), Convert.ToInt32(localPortNumber));
sck.Bind(eplocal);
epRemote = new IPEndPoint(IPAddress.Parse(friendIP), Convert.ToInt32(friendPortNumber));
sck.Connect(epRemote);
byte[] buffer = new byte[1500];
sck.BeginReceiveFrom(buffer, 0, buffer.Length, SocketFlags.None, ref epRemote, new AsyncCallback(MessageCallBack), buffer);
connectButton.Text = "Connected";
connectButton.Enabled = false;
sendMessageButton.Enabled = true;
messageEntryField.Focus();
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
private void sendMessageButton_Click(object sender, EventArgs e)
{
try
{
System.Text.ASCIIEncoding enc = new System.Text.ASCIIEncoding();
byte[] msg = new byte[1500];
msg = enc.GetBytes(messageEntryField.Text);
sck.Send(msg);
messageListBox.Items.Add(username + ": " + messageEntryField.Text);
messageEntryField.Clear();
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
}
}
答案 0 :(得分:2)
host = Dns.GetHostEntry(Dns.GetHostName());
由于在获取客户端IP的过程中出现此声明,您将获得例外。
用
替换代码protected string GetClientIP()
{
string result = string.Empty;
string ip = HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
if (!string.IsNullOrEmpty(ip))
{
string[] ipRange = ip.Split(',');
int le = ipRange.Length - 1;
result = ipRange[0];
}
else
{
result = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];
}
return result;
}
现在你不会得到那个错误。
答案 1 :(得分:1)
您已关闭服务器,以便从客户端收听请求。因此,如果您有一个或尝试在差异项目中创建服务器并运行它,请尝试打开它。
您可以拥有一个示例here