本文整理汇总了C#中System.Net.Sockets.TcpClient.GetStream方法的典型用法代码示例。如果您正苦于以下问题:C# System.Net.Sockets.TcpClient.GetStream方法的具体用法?C# System.Net.Sockets.TcpClient.GetStream怎么用?C# System.Net.Sockets.TcpClient.GetStream使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Net.Sockets.TcpClient
的用法示例。
在下文中一共展示了System.Net.Sockets.TcpClient.GetStream方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: TryPrint
public void TryPrint(string zplCommands)
{
try
{
// Open connection
System.Net.Sockets.TcpClient client = new System.Net.Sockets.TcpClient();
client.Connect(this.m_IPAddress, this.m_Port);
// Write ZPL String to connection
System.IO.StreamWriter writer = new System.IO.StreamWriter(client.GetStream());
writer.Write(zplCommands);
writer.Flush();
// Close Connection
writer.Close();
client.Close();
}
catch (Exception ex)
{
this.m_ErrorCount += 1;
if(this.m_ErrorCount < 10)
{
System.Threading.Thread.Sleep(5000);
this.TryPrint(zplCommands);
}
else
{
throw ex;
}
}
}
示例2: connect
public static string connect(String server,int port,String ouath)
{
System.Net.Sockets.TcpClient sock = new System.Net.Sockets.TcpClient ();
sock.Connect (server, port);
if (!sock.Connected) {
Console.Write ("not working hoe");
}
System.IO.TextWriter output;
System.IO.TextReader input;
output = new System.IO.StreamWriter (sock.GetStream ());
input = new System.IO.StreamReader (sock.GetStream ());
output.Write (
"PASS " + ouath + "\r\n" +
"NICK " + "Sail338" + "\r\n" +
"USER " + "Sail338" + "\r\n" +
"JOIN " + "#sail338" + "" + "\r\n"
);
output.Flush ();
for (String rep = input.ReadLine ();; rep = input.ReadLine ()) {
string[] splitted = rep.Split (':');
if (splitted.Length > 2) {
string potentialVote = splitted [2];
if (Array.Exists (validVotes, vote => vote.Equals(potentialVote)))
return potentialVote;
}
}
}
示例3: openSocket
/*
*
*/
public virtual void openSocket()
{
socket = new System.Net.Sockets.TcpClient(host, port);
socket.LingerState = new System.Net.Sockets.LingerOption(true, 1000);
os = socket.GetStream();
is_Renamed = socket.GetStream();
}
示例4: TcpConnection
public TcpConnection(System.Net.Sockets.TcpClient socket)
{
if (socket == null)
throw new System.NullReferenceException();
this.socket = socket;
in_stream = new System.IO.StreamReader(socket.GetStream());
out_stream = new System.IO.StreamWriter(socket.GetStream());
}
示例5: PerformWhois
public static string PerformWhois(string WhoisServerHost, int WhoisServerPort, string Host)
{
string result="";
try {
String strDomain = Host;
char[] chSplit = {'.'};
string[] arrDomain = strDomain.Split(chSplit);
// There may only be exactly one domain name and one suffix
if (arrDomain.Length != 2) {
return "";
}
// The suffix may only be 2 or 3 characters long
int nLength = arrDomain[1].Length;
if (nLength != 2 && nLength != 3) {
return "";
}
System.Collections.Hashtable table = new System.Collections.Hashtable();
table.Add("de", "whois.denic.de");
table.Add("be", "whois.dns.be");
table.Add("gov", "whois.nic.gov");
table.Add("mil", "whois.nic.mil");
String strServer = WhoisServerHost;
if (table.ContainsKey(arrDomain[1])) {
strServer = table[arrDomain[1]].ToString();
}
else if (nLength == 2) {
// 2-letter TLD's always default to RIPE in Europe
strServer = "whois.ripe.net";
}
System.Net.Sockets.TcpClient tcpc = new System.Net.Sockets.TcpClient ();
tcpc.Connect(strServer, WhoisServerPort);
String strDomain1 = Host+"\r\n";
Byte[] arrDomain1 = System.Text.Encoding.ASCII.GetBytes(strDomain1.ToCharArray());
System.IO.Stream s = tcpc.GetStream();
s.Write(arrDomain1, 0, strDomain1.Length);
System.IO.StreamReader sr = new System.IO.StreamReader(tcpc.GetStream(), System.Text.Encoding.ASCII);
System.Text.StringBuilder strBuilder = new System.Text.StringBuilder();
string strLine = null;
while (null != (strLine = sr.ReadLine())) {
strBuilder.Append(strLine+"\r\n");
}
result = strBuilder.ToString();
tcpc.Close();
}catch(Exception exc) {
result="Could not connect to WHOIS server!\r\n"+exc.ToString();
}
return result;
}
示例6: initSocket
//UPGRADE_NOTE: Synchronized keyword was removed from method 'initSocket'. Lock expression was added. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1027'"
private void initSocket()
{
lock (this)
{
if (socket == null)
{
socket = new System.Net.Sockets.TcpClient(host, port);
outStream = socket.GetStream();
inStream = socket.GetStream();
oos = new System.IO.BinaryWriter(outStream);
ois = new System.IO.BinaryReader(inStream);
}
}
}
示例7: Communicator
/// <summary>
/// Constructs a Communicator. Automatically starts a thread for receiving from the client.
/// </summary>
/// <param name="connection">A client for the Communicator to connect with.</param>
public Communicator(System.Net.Sockets.TcpClient connection)
{
client = connection;
writer = new StreamWriter(client.GetStream());
reader = new StreamReader(client.GetStream());
authenticated = false;
username = "";
password = "";
receiveMessage = new Thread(new ThreadStart(readMessage));
receiveMessage.IsBackground = true;
}
示例8: Request
/// <summary>
///
/// </summary>
/// <remarks>
/// SocketException is resolved at higher level in the
/// RemoteObjectProxyProvider.ProxyInterceptor.Intercept() method.
/// </remarks>
/// <param name="command"></param>
/// <param name="data"></param>
/// <returns></returns>
/// <exception cref="System.Net.Sockets.SocketException">
/// When it is not possible to connect to the server.
/// </exception>
public string Request(string command, string[] data)
{
var client = new System.Net.Sockets.TcpClient(ServerAddress.IPAddress.ToString(), ServerAddress.Port);
using (var clientStream = client.GetStream())
{
var sw = new StreamWriter(clientStream);
command = command
.Replace("\r", TcpServer.CarriageReturnReplacement)
.Replace("\n", TcpServer.LineFeedReplacement);
sw.WriteLine(command);
sw.WriteLine(data.Length.ToString());
foreach (string item in data)
{
string encodedItem = item
.Replace("\r", TcpServer.CarriageReturnReplacement)
.Replace("\n", TcpServer.LineFeedReplacement);
sw.WriteLine(encodedItem);
}
sw.Flush();
var sr = new StreamReader(clientStream);
string result = sr.ReadLine();
if (result != null)
{
result = result
.Replace(TcpServer.CarriageReturnReplacement, "\r")
.Replace(TcpServer.LineFeedReplacement, "\n");
}
return result;
}
}
示例9: Connect
public bool Connect()
{
System.Net.IPAddress ipAdd = System.Net.IPAddress.Parse(this.ipString);
if (objSck != null)
{
objSck.Close();
objSck = null;
}
objSck = new System.Net.Sockets.TcpClient();
try
{
objSck.Connect(ipAdd, port);
}
catch(Exception)
{
return false;
}
//catch (Exception)
//{
// throw;
//}
//NetworkStreamを取得
ns = objSck.GetStream();
return true;
}
示例10: printButton_Click
private void printButton_Click(object sender, EventArgs e)
{
// Printer IP Address and communication port
string ipAddress = printerIpText.Text;
int port = 9100;
try
{
// Open connection
System.Net.Sockets.TcpClient client = new System.Net.Sockets.TcpClient();
client.Connect(ipAddress, port);
// Write ZPL String to connection
System.IO.StreamWriter writer = new System.IO.StreamWriter(client.GetStream());
writer.Write(zplCode);
writer.Flush();
// Close Connection
writer.Close();
client.Close();
MessageBox.Show("Print Successful!", "Success");
this.Close();
}
catch (Exception ex)
{
Console.WriteLine("Error: {0}", ex);
MessageBox.Show("No printer installed corresponds to the IP address given", "No response");
}
}
示例11: OpenAsync
/// <summary>
/// Open 通信要求受付(非同期)
/// </summary>
/// <param name="ipAddr">IPアドレス</param>
/// <param name="ipPort">ポート</param>
/// <returns></returns>
public async void OpenAsync()
{
sw.Start();
//ListenするIPアドレス
System.Net.IPAddress ipAdd = System.Net.IPAddress.Parse(listenIP);
//TcpListenerオブジェクトを作成する
listener =
new System.Net.Sockets.TcpListener(ipAdd, listenPort);
//Listenを開始する
listener.Start();
Console.WriteLine("Listenを開始しました({0}:{1})。",
((System.Net.IPEndPoint)listener.LocalEndpoint).Address,
((System.Net.IPEndPoint)listener.LocalEndpoint).Port);
//接続要求があったら受け入れる
//client = listener.AcceptTcpClient();
try
{
client = await listener.AcceptTcpClientAsync();
Console.WriteLine("クライアント({0}:{1})と接続しました。",
((System.Net.IPEndPoint)client.Client.RemoteEndPoint).Address,
((System.Net.IPEndPoint)client.Client.RemoteEndPoint).Port);
//NetworkStreamを取得
ns = client.GetStream();
}
catch (Exception)
{
}
}
示例12: CreateAsync
public static async Task<OpcWriter> CreateAsync(string host, int port = DefaultPort)
{
var client = new System.Net.Sockets.TcpClient();
await client.ConnectAsync(host, port).ConfigureAwait(false);
var stream = client.GetStream();
return new OpcWriter(stream, true);
}
示例13: Main
static void Main(string[] args)
{
Protocol protocol;
string ip;
int port;
V2DLE dle;
System.Net.Sockets.TcpClient tcp;
protocol = new Protocol();
protocol.Parse(System.IO.File.ReadAllText(Protocol.CPath(AppDomain.CurrentDomain.BaseDirectory+"TIME.txt")),false);
ip = protocol.ip;
port = protocol.port;
while (true)
{
try
{
bool isCommErr = false;
tcp = new System.Net.Sockets.TcpClient();
tcp = ConnectTask(ip, port);
dle = new V2DLE("DigitTimer", tcp.GetStream());
dle.OnCommError+=(s,a)=>
{
isCommErr = true;
dle.Close();
};
while (!isCommErr)
{
System.Data.DataSet ds = protocol.GetSendDataSet("report_system_time");
SendPackage pkg = protocol.GetSendPackage(ds, 0xffff);
pkg.cls = CmdClass.A;
dle.Send(pkg);
if (pkg.result == CmdResult.ACK)
{
System.Data.DataSet retDs = protocol.GetReturnDsByTextPackage(pkg.ReturnTextPackage);
int yr,mon,dy,hr,min,sec;
yr=System.Convert.ToInt32(retDs.Tables[0].Rows[0]["year"]);
mon = System.Convert.ToInt32(retDs.Tables[0].Rows[0]["month"]);
dy = System.Convert.ToInt32(retDs.Tables[0].Rows[0]["day"]);
hr = System.Convert.ToInt32(retDs.Tables[0].Rows[0]["hour"]);
min = System.Convert.ToInt32(retDs.Tables[0].Rows[0]["minute"]);
sec = System.Convert.ToInt32(retDs.Tables[0].Rows[0]["second"]);
DateTime dt = new DateTime(yr, mon, dy, hr, min, sec);
Console.WriteLine(dt.ToString());
RemoteInterface.Util.SetSysTime(dt);
}
else
{
Console.WriteLine(pkg.result.ToString());
}
System.Threading.Thread.Sleep(1000 * 60 );
}
}
catch (Exception ex)
{
;
}
}
}
示例14: Create
public static OpcWriter Create(string host, int port = DefaultPort)
{
var client = new System.Net.Sockets.TcpClient();
client.Connect(host, port);
var stream = client.GetStream();
return new OpcWriter(stream, true);
}
示例15: TcpClient
public TcpClient(string hostName, int port)
{
_buffer = new byte[ReadSize];
_tcpClient = new System.Net.Sockets.TcpClient(hostName, port);
if (_tcpClient.Connected)
_stream = _tcpClient.GetStream();
}