如何使用C#在套接字编程中单独发送数据 [英] How send data sepratly in socket programming with c#

查看:197
本文介绍了如何使用C#在套接字编程中单独发送数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,这是我的问题,我想将两个整数数据与客户端程序分别发送到服务器程序:
我使用此功能发送:

<pre lang="cs">void sendata(int d)
{
    byte[] data = new byte[1024];
    data = BitConverter.GetBytes(d);
    sock.Send(data, data.Length, SocketFlags.None);
}



当计时器运行时,请在下面执行以下操作:

<pre lang="cs">private void timer1_Tick(object sender, EventArgs e)
{
    int a ;
    try
    {
        a =+ a;
        sendata(a);
    }
    catch
    {
        //MessageBox.Show("error","er");
        //sock.Shutdown(SocketShutdown.Both);
        //sock.Close();
        //Close();
    }



好的,如果我想发送两个整数数据,例如int a,例如数据#1和int b,用于另一个数据,我该如何在send函数中将它们分开,并在另一个程序中接收它们时将它们分开? >解决方案

一些常识性方法可能涉及发送一个用逗号分隔的两个数字的字符串,或者只是简单地知道int中有多少个字节,然后将其插入并提取到您的字节流中.


ok , it`s my problem there are two integer data that i want send them sepratly with my client program to my server program :
i use this function for send :

<pre lang="cs">void sendata(int d)
{
    byte[] data = new byte[1024];
    data = BitConverter.GetBytes(d);
    sock.Send(data, data.Length, SocketFlags.None);
}



when timer run do like this in below :

<pre lang="cs">private void timer1_Tick(object sender, EventArgs e)
{
    int a ;
    try
    {
        a =+ a;
        sendata(a);
    }
    catch
    {
        //MessageBox.Show("error","er");
        //sock.Shutdown(SocketShutdown.Both);
        //sock.Close();
        //Close();
    }



ok , if i want the sending two integer data like int a for example data #1 and int b for another data , how could i seprate them in send function and , seprate them when recieve them in another program ?

解决方案

Some common sense approaches might involve sending a string with two numbers seperated by a comma, or just plain knowing how many bytes there are in an int and so inserting and extracting them into your byte stream.


这篇关于如何使用C#在套接字编程中单独发送数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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