如何使用winsock控件在c#windows窗体中从客户端 - 服务器应用程序发送和接收数据 [英] how to send and receive datas from client-server application in c# windows forms using winsock control

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

问题描述

您好我的名字是vishal。最近我在c#windows窗体中使用winsock控件创建了一个客户端 - 服务器通信。

在我的服务器表单中,我有4个文本框,2个按钮控件和1个winsock控件命名:SockMain

我的服务器表单名称: frmServer

我有一个名为: frmClient 的客户端表单,其中包含3个文本框,1个winock控件名为:SockMain和2个按钮控件。

我可以通过以下c#代码连接 frmServer frmClient

以下是 frmClient 的c#代码:

  private   void  btnListen_Click( object  sender,EventArgs e)
{
SockMain.LocalPort = Convert.ToInt32(txtPort.Text);
SockMain.Listen();
}
private void SockMain_ConnectionRequest( object sender,AxMSWinsockLib.DMSWinsockControlEvents_ConnectionRequestEvent e)
{
if (SockMain.CtlState!= 0
{
SockMain.Close();
}
SockMain.Accept(e.requestID);
txtStatus.Text = txtStatus.Text + 接受的连接来自: + SockMain.RemoteHostIP + System.Environment.NewLine;
}



以下是 frmServer 的c#代码:

  private   void  btnConnect_Click( object  sender, EventArgs e)
{
SockMain.RemoteHost = txtHost.Text;
SockMain.RemotePort = Convert.ToInt32(txtPort.Text);
SockMain.Connect();
}



所以我能够使用 Winsock <连接 frmClient frmServer / b>控制在 c #windows窗体,结果我收到消息:接受的连接来自:192.168.0.105(我的系统的IP地址)在我的文本框中名为: txtStatus 在我的表格中 frmClient

但问题是我无法在我的客户端之间进行通信 frmClient )和服务器( frmServer ),我不知道为什么!。



下面给出了 frmClient的c#代码我卡住的地方:

  private   void  btnSend_Click( object  sender,EventArgs e)
{
byte [] abData;
string str = txtSend.Text;
abData = System.Text.Encoding.Default.GetBytes(str);
SockMain.SendData(abData);
}
私有 void SockMain_DataArrival( object sender,AxMSWinsockLib.DMSWinsockControlEvents_DataArrivalEvent e)
{
string strData = txtSend.Text;
Object buffer2 =( object )strData;
SockMain.GetData( ref buffer2);
txtStatus.Text = txtStatus.Text + strData + System.Environment.NewLine;
}



以下是 frmServer 的c#代码,我被卡住了:

  private   void  btnSend_Click( object  sender,EventArgs e)
{
byte [] abData;
string str = txtSend.Text;
abData = System.Text.Encoding.Default.GetBytes(str);
SockMain.SendData(abData);
}
私有 void SockMain_DataArrival( object sender,AxMSWinsockLib.DMSWinsockControlEvents_DataArrivalEvent e)
{
string strData = txtSend.Text;
Object buffer2 =( object )strData;
SockMain.GetData( ref buffer2);
txtStatus.Text = txtStatus.Text + strData + System.Environment.NewLine;
}



我想要的是一旦我的 frmServer frmClient 通过 winsock连接在c#windows窗体中控制我希望我的客户端表单(frmClient)和服务器表单(frmServer)基于文本异步进行通信通过按钮(btnSend)输入文本框( txtSend )。



我相信我在转移(字符串数据)和接收时遇到困难(字符串数据)使用 SendData 函数和winsock控件的 GetData 函数(SockMain)。



任何人都可以帮助如何使用winsock控件的SendData函数发送字符串数据并使用winsock控件的GetData函数接收字符串数据?有人可以帮我吗?任何帮助/指导解决这个问题将不胜感激!

解决方案

我的名字是这个论坛的vishal(会员10248768)。

嗯,通过上帝的恩典,我能够通过以下代码解决我的问题:

所以在frmServer中我做了一些修改,下面是c#代码:

  private   void  SockMain_DataArrival( object  sender,AxMSWinsockLib.DMSWinsockControlEvents_DataArrivalEvent e)
{
string strData = < span class =code-string>;
对象 tend =( object )strData;
SockMain.GetData( ref tend);
strData =( String )tend;
txtStatus.Text = txtStatus.Text + strData + System.Environment.NewLine;
}
私有 void btnSend_Click( object sender,EventArgs e)
{
byte [] abData;
string str = txtSend.Text;
abData = System.Text.ASCIIEncoding.Default.GetBytes(str);
SockMain.SendData(txtSend.Text);
}



在frmClient中我做了类似的修改,下面是c#代码:

 < span class =code-keyword> private   void  btnSend_Click( object  sender,EventArgs e )
{
byte [] abData;
string str = txtSend.Text;
abData = System.Text.ASCIIEncoding.Default.GetBytes(str);
SockMain.SendData(txtSend.Text);
}
私有 void SockMain_DataArrival( object sender,AxMSWinsockLib.DMSWinsockControlEvents_DataArrivalEvent e)
{
string strData = ;
Object send =( object )strData;
SockMain.GetData( ref send);
strData =( String )send;
txtStatus.Text = txtStatus.Text + strData + System.Environment.NewLine;
}



因为我能够通过c#windows窗体中的winsock控件连接我的客户端(frmClient)和服务器(frmServer)。

为了帮助我的客户端和服务器异步通信我已经使用了上面的代码并且它可以工作!

无论如何,感谢您的帮助,我感谢全能者帮助我自己解决问题!


Hi my name is vishal .Recently i have created a client-server communication using winsock control in c# windows forms.
In my server form i have 4textboxes,2 button control and 1 winsock control named:SockMain
my server form name:frmServer
I have a client form named:frmClient which contains 3 textboxes,1 winsock control named:SockMain and 2 button controls.
I am able to connect frmServer and frmClient through following c# code:
Given below is c# code of frmClient:

private void btnListen_Click(object sender, EventArgs e)
        {
            SockMain.LocalPort = Convert.ToInt32(txtPort.Text);
            SockMain.Listen();
        }
private void SockMain_ConnectionRequest(object sender, AxMSWinsockLib.DMSWinsockControlEvents_ConnectionRequestEvent e)
        {
            if (SockMain.CtlState != 0)
            {
                SockMain.Close();
            }
            SockMain.Accept(e.requestID);
            txtStatus.Text = txtStatus.Text + "Accepted Connection from: " + SockMain.RemoteHostIP + System.Environment.NewLine;
        }


Given below is c# code of frmServer:

private void btnConnect_Click(object sender, EventArgs e)
        {
            SockMain.RemoteHost = txtHost.Text;
            SockMain.RemotePort = Convert.ToInt32(txtPort.Text);
            SockMain.Connect();
        }


So i am able to connect between frmClient and frmServer using Winsock control in c# windows forms which as a result i get message:"Accepted Connection from: 192.168.0.105(which is ip address of my system) in my textbox named:txtStatus in my form in frmClient.
However the problem is i am not able to communicate asynchronously between my client(frmClient) and server(frmServer) and i dont know why! .

Given below is c# code of frmClient where i get stuck:

private void btnSend_Click(object sender, EventArgs e)
        {
            byte[] abData;
           string str=txtSend.Text;
           abData = System.Text.Encoding.Default.GetBytes(str);
            SockMain.SendData(abData);
        }
private void SockMain_DataArrival(object sender, AxMSWinsockLib.DMSWinsockControlEvents_DataArrivalEvent e)
        {
            string strData=txtSend.Text;
            Object buffer2 = (object)strData;
            SockMain.GetData(ref buffer2);
            txtStatus.Text = txtStatus.Text + strData + System.Environment.NewLine;
        }


Given below is c# code of frmServer where i get stuck:

private void btnSend_Click(object sender, EventArgs e)
        {
            byte[] abData;
            string str = txtSend.Text;
            abData = System.Text.Encoding.Default.GetBytes(str);
            SockMain.SendData(abData);
        }
private void SockMain_DataArrival(object sender, AxMSWinsockLib.DMSWinsockControlEvents_DataArrivalEvent e)
        {
            string strData=txtSend.Text;
            Object buffer2 = (object)strData;
            SockMain.GetData(ref buffer2);
            txtStatus.Text = txtStatus.Text + strData + System.Environment.NewLine;
        }


What i want is once my frmServer and frmClient is connected through winsock control in c# windows forms i want my client form(frmClient) and server form(frmServer) to communicate asynchronously based on text entered in textbox(txtSend) through button(btnSend).

I believe i am getting stuck at transferring(string data) and receiving(string data) data using SendData function and GetData function of winsock control(SockMain).

Can anyone help me how to send string data using SendData function of winsock control and receive string data using GetData function of winsock control? Can anyone help me please? Any help/guidance in solving of this problem would be greatly appreciated!

解决方案

Hi my name is vishal(Member 10248768) of this forum.
Well by God's grace i was able to solve my problem through following code:
So in frmServer i made some modifications given below is c# code:

private void SockMain_DataArrival(object sender, AxMSWinsockLib.DMSWinsockControlEvents_DataArrivalEvent e)
        {
            string strData="";
            Object tend = (object)strData;
            SockMain.GetData(ref tend);
            strData = (String)tend;
            txtStatus.Text = txtStatus.Text + strData + System.Environment.NewLine;
        }
private void btnSend_Click(object sender, EventArgs e)
        {
            byte[] abData;
            string str = txtSend.Text;
            abData = System.Text.ASCIIEncoding.Default.GetBytes(str);
            SockMain.SendData(txtSend.Text);
        }


In frmClient i made similar modifications given below is it's c# code:

private void btnSend_Click(object sender, EventArgs e)
        {
            byte[] abData;
           string str=txtSend.Text;
           abData = System.Text.ASCIIEncoding.Default.GetBytes(str);
            SockMain.SendData(txtSend.Text);
        }
 private void SockMain_DataArrival(object sender, AxMSWinsockLib.DMSWinsockControlEvents_DataArrivalEvent e)
        {
            string strData="";
            Object send = (object)strData;
            SockMain.GetData(ref send);
            strData = (String)send;
            txtStatus.Text = txtStatus.Text + strData + System.Environment.NewLine;
        }


Since i was able to connect between my client(frmClient) and server(frmServer) through winsock control in c# windows forms.
In order to help my client and server communicate asynchronously i have used the above code and it works!
Thanks for your help anyway and i thank the Almighty in helping me solve the problem by myself!


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

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