Created
July 27, 2019 03:40
-
-
Save kw26/644a73d08f71ea5b9d2d26f95ff640c3 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Runtime.CompilerServices; | |
using System; | |
using System.Net; | |
using System.Net.Sockets; | |
using System.IO; | |
public class Script : ScriptBase, System.IDisposable | |
{ | |
private bool changed; | |
public Script() | |
{ | |
} | |
public void InitClient() | |
{ | |
TcpClient tcpClient = new TcpClient();//创建一个对象,自动分配主机IP和端口号 | |
tcpClient.Connect("127.0.0.1", 7000); | |
if (tcpClient != null) | |
{ | |
Console.WriteLine("连接服务器成功"); | |
this.info = "connect successfully"; | |
NetworkStream networkStream = tcpClient.GetStream(); | |
BinaryReader reader = new BinaryReader(networkStream); | |
BinaryWriter writer = new BinaryWriter(networkStream); | |
string local = "127.0.0.1";//存储本地IP,默认值为127.0.0.1 | |
IPAddress[] ips = Dns.GetHostAddresses(Dns.GetHostName()); | |
foreach (var item in ips) | |
{ | |
if (!item.IsIPv6LinkLocal && !item.IsIPv6Teredo) | |
{ | |
local = item.ToString(); | |
} | |
} | |
writer.Write(local + " 你好服务器,我是客户端");//向服务器发消息 | |
while (true) | |
{ | |
try | |
{ | |
string strReader = reader.ReadString(); | |
if (strReader != null) | |
{ | |
this.info = "rec:" + strReader; | |
Console.WriteLine("来自服务器的消息:" + strReader); | |
} | |
} | |
catch (Exception) | |
{ | |
break; | |
} | |
} | |
} | |
} | |
public virtual void Dispose() | |
{ | |
} | |
public override void Validate() | |
{ | |
} | |
public override bool Generate() | |
{ | |
if (changed) | |
{ | |
changed = false; | |
return true; | |
} | |
return false; | |
} | |
// This Method is called if the function/method init is invoked by the user or a bound event. | |
// Return true, if this component has to be revalidated! | |
public bool Oninit(int arg) | |
{ | |
this.info = "init client"; | |
InitClient(); | |
return false; | |
} | |
} |
Author
kw26
commented
Jul 27, 2019
input | item |
---|---|
init | model |
output | item |
---|---|
info | string |
response | string |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment