TCP编程(6):客户端接收程序TcpClient
/*--===------------------------------------------===---
客户端接收程序:
TcpClient, NetworkStream
许明会 2007年12月9日 22:57:13
--===------------------------------------------===---*/
using System;
using System.Collections.Generic;
using System.Text;
namespace NetCommunication
{
class tcpClient
{
private const int port = 7788;
private const string hostname = "127.0.0.1";
static void Main()
{
try
{
Console.WriteLine("尝试连接服务器{0}:{1}\t{2}", hostname, port, DateTime.Now.ToString());
System.Net.Sockets.TcpClient tc = new System.Net.Sockets.TcpClient(hostname, port);
System.Net.Sockets.NetworkStream ns = tc.GetStream();
byte[] bytes = new byte[1024];
int nCount = ns.Read(bytes, 0, bytes.Length);
if(nCount>0)
Console.WriteLine(System.Text.Encoding.ASCII.GetString(bytes, 0, nCount));
tc.Close();
Console.ReadLine();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
}
}
客户端接收程序:
TcpClient, NetworkStream
许明会 2007年12月9日 22:57:13
--===------------------------------------------===---*/
using System;
using System.Collections.Generic;
using System.Text;
namespace NetCommunication
{
class tcpClient
{
private const int port = 7788;
private const string hostname = "127.0.0.1";
static void Main()
{
try
{
Console.WriteLine("尝试连接服务器{0}:{1}\t{2}", hostname, port, DateTime.Now.ToString());
System.Net.Sockets.TcpClient tc = new System.Net.Sockets.TcpClient(hostname, port);
System.Net.Sockets.NetworkStream ns = tc.GetStream();
byte[] bytes = new byte[1024];
int nCount = ns.Read(bytes, 0, bytes.Length);
if(nCount>0)
Console.WriteLine(System.Text.Encoding.ASCII.GetString(bytes, 0, nCount));
tc.Close();
Console.ReadLine();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
}
}