Tcp客户端 [英] Tcp client
问题描述
你好,
我有问题,不要不知道如何解决这个问题。然后我正在尝试构建我的代码我犯了两个错误:
CS0118'TcpClient'是一个命名空间,但用作类型
CS0118'TcpClient'是一个命名空间,但用作类型
这个代码行错了:
TcpClient客户端=新TcpClient();
也许有人告诉我如何解决我的问题
我的代码:
使用 系统 ;
使用 系统 。 集合 。 通用 ;
使用 系统 。 ComponentModel ;
使用 系统 。 数据 ;
使用 系统 。 绘图 ;
使用 系统 。 Linq ;
使用 系统 。 文字 ;
使用 系统 。 线程 。 任务 ;
使用 系统 。 Windows 。 表格 ;
使用 系统 。 净 。 套接字 ;
使用 系统 。 净 ;
使用 系统 。 IO ;
使用 系统 。 线程 ;
命名空间 TcpClient
{
公共 部分 类 Form1 : 表格
{
公共 Form1 ()
{
InitializeComponent ();
}
私人 无效 Form1_Load ( 对象 发件人 , EventArgs e )
{
}
私人 无效 bConnect_Click ( 对象 发件人 , EventArgs e )
{
线程 mThread = 新 线程 ( 新 ThreadStart ( ConnectAsClient ));
mThread 。 开始 ();
}
私人 无效 ConnectAsClient ()
{
TcpClient 客户 = 新 TcpClient ();
client 。 conenct ( IPAddress 。 Parse ( " 192.168.1.178" ), 5004 );
NetworkStream stream = 客户 。 GetStream ();
字符串 s = "来自客户的问候" ;
字节 [] message = 编码 。 ASCII 。 GetBytes ( s );
stream 。 写 ( 消息 , 0 < span class ="pun"style ="margin:0px; padding:0px; border:0px; font-size:13.008px; vertical-align:baseline; background:transparent; color:#000000">, < span class ="pln"style ="margin:0px; padding:0px; border:0px; font-size:13.008px; vertical-align:baseline; background:transparent"> message 。 长度 );
updateUI ( "消息发送" );
stream 。 关闭 ();
client 。 关闭 ();
}
私人 无效 updateUI ( 字符串 s )
{
Func < int> del = 委托 ()
{
textBox1 。 AppendText ( s + System 。 Environment 。 NewLine );
返回 0 ;
};
调用 ( del );
}
}
}
这是你自己的代码:
命名空间 TcpClient这优先于从System.Net继承的TcpClient类。您可以明确地引用System.Net.TcpClient ,或者更改项目的命名空间。您不仅要在该文件中,而且还要在Project-> Properties
以及您已创建的任何其他文件中执行此操作。
Hello,
I have problem and don't know how solve this problem. Then I am trying building my code I got two mistake:
CS0118 'TcpClient' is a namespace but is used like a type
CS0118 'TcpClient' is a namespace but is used like a type
Something wrong is this code line:
TcpClient client = new TcpClient();
Maybe somebody tell me how solve my problem
My code :
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using System.Net.Sockets; using System.Net; using System.IO; using System.Threading; namespace TcpClient { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { } private void bConnect_Click(object sender, EventArgs e) { Thread mThread =new Thread(new ThreadStart(ConnectAsClient)); mThread.Start(); } private void ConnectAsClient() { TcpClient client = new TcpClient(); client.conenct(IPAddress.Parse("192.168.1.178"), 5004); NetworkStream stream = client.GetStream(); String s = "Hello from client"; byte[] message = Encoding.ASCII.GetBytes(s); stream.Write(message, 0, message.Length); updateUI("Message Send"); stream.Close(); client.Close(); } private void updateUI(string s) { Func<int> del = delegate () { textBox1.AppendText(s + System.Environment.NewLine); return 0; }; Invoke(del); } } }
This is in your own code:
namespace TcpClientThat's taking precedence over the TcpClient class inherited from System.Net. You could refer to System.Net.TcpClient explicitly, or change your project's namespace. You'd have to do it not just in that file, but also in Project->Properties and any other file you already created, too.
这篇关于Tcp客户端的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!