【发布时间】:2016-05-02 05:10:54
【问题描述】:
我正在使用 Visual Studio 2015 C# Windows 应用程序。我将 winsock 连接到 c# 应用程序以从服务器获取来电数据。 在这里,我清楚地解释了我为此做了什么。
将 WINSOCK 控件连接到我的表单
右键工具箱->选择项目
代码:
using System;
using System.Windows.Forms;
namespace 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 isConnected = false;
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;
}
private void w1_ConnectEvent(object sender, EventArgs e)
{
DataInput.Text += "\n - Connect Event : " + w1.RemoteHostIP;
isConnected = true;
}
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_Error(object sender, AxMSWinsockLib.DMSWinsockControlEvents_ErrorEvent e)
{
DataInput.Text += "\n- Error : " + e.description;
isConnected = 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();
}
}
}
}
我需要连接 pbx 服务器 10.0.0.68。 请帮助我为什么它没有连接?为什么它给出了一些错误?有人帮我解决我的问题。
【问题讨论】:
-
您是否有理由要使用该控件而不仅仅是常规 Socket?
-
@Anjana 谢谢你提出这个问题
标签: c# server connection winsock pbx