Program.cs
上传用户:jsz11269
上传日期:2017-01-14
资源大小:450k
文件大小:1k
- using System;
- using System.Collections.Generic;
- using System.Text;
- using System.Net.Sockets;
- namespace TcpClientExample
- {
- class Program
- {
- private const int portNum = 13;//服务器端口,可以随意修改
- private const string hostName = "127.0.0.1";//服务器地址,127.0.0.1指本机
- static void Main(string[] args)
- {
- try
- {
- Console.Write("Try to connect to " + hostName + ":" + portNum.ToString() + "rn");
- TcpClient client = new TcpClient(hostName, portNum);
- NetworkStream ns = client.GetStream();
- byte[] bytes = new byte[1024];
- int bytesRead = ns.Read(bytes, 0, bytes.Length);
- Console.WriteLine(Encoding.ASCII.GetString(bytes, 0, bytesRead));
- client.Close();
- Console.ReadLine();//由于是控制台程序,故为了清楚的看到结果,可以加上这句
- }
- catch (Exception e)
- {
- Console.WriteLine(e.ToString());
- }
- }
- }
- }