如何在客户端应用程序中使我的winsock控件连接到服务器应用程序,该应用程序在c#windows窗体中也有一个winsock控件 [英] How to make my winsock control in client application to connect to server application which also has a winsock control in c# windows forms

查看:106
本文介绍了如何在客户端应用程序中使我的winsock控件连接到服务器应用程序,该应用程序在c#windows窗体中也有一个winsock控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好我的名字是过去1周的vishal我一直在打破如何在客户端应用程序中使我的winsock控件连接到服务器应用程序,该应用程序在c#windows窗体中也有一个winsock控件?

所以我有一个名为: Winsock客户端的客户端应用程序,它有一个名为: Form1 的表单,它还有一个名为: Winsock1 的winsock控件,协议: 0-sckTCPProtocol 并且可见: true



以下是我的 c#代码 Winsock客户端的Form1(客户端应用程序):

Hi my name is vishal for past 1 week i have been breaking my head on How to make my winsock control in client application to connect to server application which also has a winsock control in c# windows forms?
So i have a client application named:Winsock client which has a form named:Form1 which also has a winsock control named:Winsock1,Protocol:0-sckTCPProtocol and visible:true

Given below is my c# code of Form1 of Winsock client(client application):

namespace Winsock_client
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
private void btnConnect_Click(object sender, EventArgs e)
        {
            Winsock1.RemoteHost = "192.168.0.118" ;//Change this to your host ip
            Winsock1.RemotePort = 7777;
            Winsock1.Connect();
            shpGo.Visible=true;
            txtPrice.Focus();
        }
private void btnDisconnect_Click(object sender, EventArgs e)
        {
            Winsock1.Close();
            shpGo.Visible=false;
            shpWait.Visible =false;
            shpError.Visible = true;
        }
private void Winsock1_SendComplete(object sender, EventArgs e)
        {
            label3.Text = "Completed Data Transmission";
        }
 private void Winsock1_DataArrival(object sender, AxMSWinsockLib.DMSWinsockControlEvents_DataArrivalEvent e)
        {
            string sData="";
            Object cand = (object)sData;
            Winsock1.GetData(ref cand);
            sData = (String)cand;
            txtPrice.Text = sData;
            label3.Text = "Received Data";
            shpGo.Visible = true;
            shpWait.Visible = false;
            shpError.Visible = false;
        }
 private void btnSend_Click(object sender, EventArgs e)
        {
            
           if(Winsock1.CtlState==2)
           {
            Winsock1.SendData(txtPrice.Text);
            shpGo.Visible =true;
            label3.Text = "Sending Data";
           }
            else
           {
            shpGo.Visible=false;
            shpWait.Visible=false;
            shpError.Visible =true;
            label3.Text = "Not currently connected to host";
           }
        }



所以我还有一个名为: Winsock服务器的服务器应用程序,它有一个名为:<的表单b> Form1 并且还有一个名为: Socket 的winsock控件,协议: 0-sckTCPProtocol 并且可见: true



下面给出的是 Winsock服务器的Form1的c#代码(服务器应用程序):


So i also have a server application named:Winsock server which has a form named:Form1 and also has a winsock control named:Socket,Protocol:0-sckTCPProtocol and visible:true

Given below is my c# code of Form1 of Winsock server(Server application):

namespace Winsock_server
{
    public partial class Form1 : Form
    {
        int iSockets;
        string sServerMsg;
        string sRequestID;
        Socket sck;
        public Form1()
        {
            InitializeComponent();
            sck = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
            sck.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
        }
        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 Form1_Load(object sender, EventArgs e)
        {
            txtHostID.Text = Socket.LocalHostName;
            txtAddress.Text = Socket.LocalIP;
            Socket.LocalPort = 7777;
             sServerMsg = "Listening to port: " + Socket.LocalPort;
            listBox1.Items.Add(sServerMsg);
            Socket.Listen();
        }
private void Socket_CloseEvent(object sender, EventArgs e)
        {
            sServerMsg = "Connection closed: " + Socket.RemoteHostIP;
            listBox1.Items.Add(sServerMsg);
            Socket.Close();
            iSockets = iSockets - 1;
            txtConnections.Text = iSockets.ToString();
        }
private void Socket_ConnectionRequest(object sender, AxMSWinsockLib.DMSWinsockControlEvents_ConnectionRequestEvent e)
        {
            sServerMsg = "Connection request id " + e.requestID + " from " + Socket.RemoteHostIP;
            listBox1.Items.Add(sServerMsg);
            sRequestID = e.requestID.ToString();
            iSockets = iSockets + 1;
            txtConnections.Text = iSockets.ToString();
        }
private void Socket_DataArrival(object sender, AxMSWinsockLib.DMSWinsockControlEvents_DataArrivalEvent e)
        {
            string incommingData = "";
            Object rend = (object)incommingData;
            Socket.GetData(ref rend);
            incommingData = (String)rend;
            listBox1.Text = listBox1.Text + System.Environment.NewLine + incommingData;
        }



所以我执行我的客户端应用程序( Winsock客户端)和服务器应用程序( Winsock < b>服务器)同时 客户端 - 服务器通信

所以我能够连接我的客户端应用程序( Winsock客户端)部分,下面是c#代码:


So i execute my client application(Winsock client) and server application(Winsock server) simultaneously for client-server communication.
So i am able to connect my client application(Winsock client) partially,Given below is c# code of that:

private void btnConnect_Click(object sender, EventArgs e)
        {
            Winsock1.RemoteHost = "192.168.0.118" ;//Change this to your host ip
            Winsock1.RemotePort = 7777;
            Winsock1.Connect();
            shpGo.Visible=true;
            txtPrice.Focus();
        }



但是在客户端应用程序( Winsock 客户端)之间建立连接后出现此问题当我在 Winsock客户端 Form1 中的文本框中输入名称为 txtPrice 的文件框中的数据和服务器应用程序( Winsock服务器)时(客户端应用程序)当我在 Winsock客户端中单击 btnSend 时,将在文本框中输入的数据( txtPrice )发送到名为的列表框: listBox1 出现在 Winsock 服务器(服务器应用程序)的Form1中,下面给出的是c#代码:


But here is the problem after connection is established between client application(Winsock client) and Server application(Winsock server) when i enter data in textbox named:txtPrice in Form1 of Winsock client(client application) and when i click btnSend in Winsock client to send data entered in textbox(txtPrice) to listbox named:listBox1 which is present in Form1 of Winsock server(Server application) given below is c# code of that:

private void btnSend_Click(object sender, EventArgs e)
        {
           if(Winsock1.CtlState==2)
           {
            Winsock1.SendData(txtPrice.Text);
            shpGo.Visible =true;
            label3.Text = "Sending Data";
           }
            else
           {
            shpGo.Visible=false;
            shpWait.Visible=false;
            shpError.Visible =true;
            label3.Text = "Not currently connected to host";
           }
        }



Winsock客户端 txtPrice (Form1中的文本框控件)中输入数据后/ b>(客户端应用程序),当我点击/按 btnSend (Winsock客户端的Form1中的按钮控件)时,我总是收到消息当前未连接到主机 label3 Winsock客户端(客户端应用程序)的Form1中的标签控件)。



此错误让我惊叹不已如何在名为: Winsock客户端的客户端应用程序中创建/启用名为: Winsock1 的winsock控件,以连接到名为: Socket 的winsock控件服务器应用程序名称: Winsock服务器



任何人都可以帮我解决如何在客户端应用程序(Winsock客户端)和服务器应用程序之间建立连接( Winsock服务器)仅在c#windows窗体中使用Winsock控件?



由于我的 BOSS 命令,我必须仅使用Winsock控件。

有人可以帮帮我吗?任何人都可以帮助我/指导我如何解决我的问题?任何帮助/指导解决这个问题将不胜感激!


After entering data in txtPrice(textbox control in Form1) of Winsock client(Client application) and when i click/press btnSend(button control in Form1 of Winsock client) i always get message "Not currently connected to host" in my label3(label control in Form1 of Winsock client(client application)).

This error makes me wonder how to make/enable my winsock control named:Winsock1 present in client application named:Winsock client to connect to winsock control named:Socket present in server application name:Winsock server?

Can anyone help me on how to establish connection between client application(Winsock client) and server application(Winsock server) using Winsock controls only in c# windows forms?

I have to use only Winsock controls for this purpose because of my BOSS's orders.
Can anyone help me Please? Can anyone help me/guide me on how to solve my problem? Any help/guidance in solving of this problem would be greatly appreciated!

推荐答案

这篇关于如何在客户端应用程序中使我的winsock控件连接到服务器应用程序,该应用程序在c#windows窗体中也有一个winsock控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆