问题描述
服务器窗口打开客户端窗口
客户端连接服务器on portno = 514
连接成功完成
但是当从客户端发送消息时服务器给出错误消息
代码
服务器程序
server window opens client window
client connects server on portno=514
connection done successfully
but when send message from the client to server give error message
Code
Server Program
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WinsockClient
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
this.w1.ConnectionRequest += new AxMSWinsockLib.DMSWinsockControlEvents_ConnectionRequestEventHandler(this.w1_ConnectionRequest);
}
Boolean isConnected = false;
private void start_Click(object sender, EventArgs e)
{
if (portText.Text != "")
{
w1.LocalPort = Int32.Parse(portText.Text);
w1.Listen();
}
}
private void Disconnect_Click(object sender, EventArgs e)
{
w1.Close();
w1.LocalPort = Int32.Parse(portText.Text);
w1.Listen();
DataInput.Text += "\n - Disconnected";
}
private void w1_ConnectionRequest(object sender, AxMSWinsockLib.DMSWinsockControlEvents_ConnectionRequestEvent e)
{
if (isConnected == true)
{
w1.Close();
}
w1.Accept(e.requestID);
isConnected = true;
DataInput.Text += "\n - Client Connected :" + w1.RemoteHostIP;
}
}
}
第二客户计划
2nd Client Program
using System;
using System.Net.Sockets;
using System.Threading;
using System.Windows.Forms;
namespace Winsock_Client
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
this.w1.Error += new AxMSWinsockLib.DMSWinsockControlEvents_ErrorEventHandler(this.w1_Error);
this.w1.ConnectEvent += new System.EventHandler(this.w1_ConnectEvent);
this.w1.DataArrival += new AxMSWinsockLib.DMSWinsockControlEvents_DataArrivalEventHandler(this.w1_DataArrival);
}
Boolean isConnect = false;
private void Connect_Click(object sender, EventArgs e)
{
try
{
w1.Close();
w1.Connect(IPText.Text, PortText.Text);
}
catch (System.Windows.Forms.AxHost.InvalidActiveXStateException g)
{
DataInput.Text += "\n" + g.ToString();
}
}
private void Send_Click(object sender, EventArgs e)
{
try
{
if (isConnect)
{
w1.SendData(SendText.Text);
DataInput.Text += "\nClent(You ;-) : " + SendText.Text;
SendText.Text = "";
}
else
MessageBox.Show("You are not connect to any host ");
}
catch (AxMSWinsockLib.AxWinsock.InvalidActiveXStateException g)
{
DataInput.Text += "\n" + g.ToString();
}
catch (Exception ex)
{
DataInput.Text += "\n" + ex.Message;
}
}
private void w1_Error(object sender, AxMSWinsockLib.DMSWinsockControlEvents_ErrorEvent e)
{
DataInput.Text += "\n- Error : " + e.description;
isConnect = false;
}
private void w1_DataArrival(object sender, AxMSWinsockLib.DMSWinsockControlEvents_DataArrivalEvent e)
{
String data = " ";
Object dat = (object)data;
w1.GetData(ref dat);
data = (String)dat;
DataInput.Text += "\nServer - " + w1.RemoteHostIP + " : " + data;
}
private void w1_ConnectEvent(object sender, EventArgs e)
{
DataInput.Text += "\n - Connect Event : " + w1.RemoteHostIP;
isConnect = true;
}
}
}
行中出错
w1.SendData(SendText.Text);
客户
表格。
任何人帮我解决我的问题:-(
我是什么尝试过:
通过谷歌搜索解决我的问题,但我无法得到任何关于此错误
Error in the line
w1.SendData(SendText.Text);
in client form.
Any one Help me to solve my problem :-(
What I have tried:
Searching through googling to solve my problem but i could not get any this about this error
推荐答案
请求的事务或请求的协议或连接状态错误
"Wrong protocol or connection state for the requested transaction or request"
因此您的连接可能处于错误的发送状态。您是否使用调试器检查实际状态?
为什么使用AxMSWinsockLib?有任何特殊原因,因为我认为您可能会发现使用 [] class。
这里也有一些很好的入门示例: []
So your connection might be in the wrong state for sending. Have you used the debugger to check the actual state?
And why using AxMSWinsockLib? Any special reason, because I think you might find it easier to use the System.Net.Sockets.Socket[^] class.
You also have some good starter examples here: Socket Code Examples[^]
这篇关于附加信息:来自HRESULT的异常:0x800a9c46的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!