C#中的套接字发送和接收(while循环) [英] Socket Sending and Recieving in C# (while loop)

查看:136
本文介绍了C#中的套接字发送和接收(while循环)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的代码.我正在编写一个简单的Socket测试.

Here is my code. I am codding a simple Socket test.

    using System;
using System.Net;
using System.Net.Sockets;
using System.Runtime.InteropServices;
using System.IO;
using System.Text;
using Microsoft.Win32;

namespace HelloWorld
{
    class Program
    {
        public static void Main(string[] args)
        {

            Console.WriteLine("Connexion au serveur 62.210.130.212");
            using (client = new TcpClient("62.210.130.212", 35025))
            using (NetworkStream networkStream = client.GetStream())
            {
                byte[] usernameBytes = Encoding.ASCII.GetBytes(username);
                networkStream.Write(usernameBytes, 0, usernameBytes.Length);
            }

            while (true)
            {
                Byte[] data = new Byte[256];
                Int32 bytes = networkStream.Read(data, 0, data.Length);
                String responseData = System.Text.Encoding.ASCII.GetString(data, 0, bytes);
                Console.WriteLine("recieved: " + responseData);
            }
        }
    }
}

现在的问题是,我无法再在代码中使用networkStram,因为它已在using选项卡的末尾删除.

Now the problem is that i can't use the networkStram anymore in my code because it has been deleted on the end of the using tab.

有人可以帮助我解决这个问题吗,我是C#的新手,在Java中不存在.

Can someone help me with that problem, I am new to C#, this doesn't exist in Java.

谢谢! 朱利安.

推荐答案

您只需要扩展正在使用的对象的全部使用范围-如果您只是看一下,这很有道理请注意:括号内的所有代码都是使用括号内的对象.因此,这至少应该解决该特定问题:

You simply have to extend the using over all usages of the object you're using - this makes sense, if you just look at the word: All the code inside the curled brackets is using the object inside the brackets. So this should at least solve that specific problem:

using System;
using System.Net;
using System.Net.Sockets;
using System.Runtime.InteropServices;
using System.IO;
using System.Text;
using Microsoft.Win32;

namespace HelloWorld
{
    class Program
    {
        public static void Main(string[] args)
        {

            Console.WriteLine("Connexion au serveur 62.210.130.212");
            using (client = new TcpClient("62.210.130.212", 35025))
            using (NetworkStream networkStream = client.GetStream())
            {
                byte[] usernameBytes = Encoding.ASCII.GetBytes(username);
                networkStream.Write(usernameBytes, 0, usernameBytes.Length);

                while (true)
                {
                    Byte[] data = new Byte[256];
                    Int32 bytes = networkStream.Read(data, 0, data.Length);
                    String responseData = System.Text.Encoding.ASCII.GetString(data, 0, bytes);
                    Console.WriteLine("recieved: " + responseData);
                }
            }
        }
    }
}

这篇关于C#中的套接字发送和接收(while循环)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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