1、服务器程序
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net;
using System.Net.Sockets;
using System.IO;
using System.Net.Sockets;
using System.IO;
namespace Service
{
class Program
{
static void Main(string[] args)
{
const int BufferSize = 8192;
//ConsoleKey key;
Console.WriteLine("service is running.....");
IPAddress ip=new IPAddress(new byte[]{127,0,0,1});
TcpListener listener = new TcpListener(ip, 8500);
{
class Program
{
static void Main(string[] args)
{
const int BufferSize = 8192;
//ConsoleKey key;
Console.WriteLine("service is running.....");
IPAddress ip=new IPAddress(new byte[]{127,0,0,1});
TcpListener listener = new TcpListener(ip, 8500);
listener.Start();
Console.WriteLine("start listening.......");
Console.WriteLine("start listening.......");
//获取一个链接,中断方法
TcpClient remoteClient = listener.AcceptTcpClient();
Console.WriteLine("{0},{1}",remoteClient.Client.LocalEndPoint,remoteClient.Client.RemoteEndPoint);
TcpClient remoteClient = listener.AcceptTcpClient();
Console.WriteLine("{0},{1}",remoteClient.Client.LocalEndPoint,remoteClient.Client.RemoteEndPoint);
NetworkStream streamToClient = remoteClient.GetStream();
byte[] buffer = new byte[BufferSize];
int bytesRead = streamToClient.Read(buffer, 0, BufferSize);
Console.WriteLine("Reading data,{0} bytes....",bytesRead);
byte[] buffer = new byte[BufferSize];
int bytesRead = streamToClient.Read(buffer, 0, BufferSize);
Console.WriteLine("Reading data,{0} bytes....",bytesRead);
//获取请求的字符串
string msg = Encoding.Unicode.GetString(buffer, 0, bytesRead);
Console.WriteLine("Received:{0}", msg);
string msg = Encoding.Unicode.GetString(buffer, 0, bytesRead);
Console.WriteLine("Received:{0}", msg);
获取字符串
//byte[] buffer = new byte[BufferSize];
//int bytesRead; // 读取的字节数
//MemoryStream msStream = new MemoryStream();
//do
//{
// bytesRead = streamToClient.Read(buffer, 0, BufferSize);
// msStream.Write(buffer, 0, bytesRead);
//} while (bytesRead > 0);
//buffer = msStream.GetBuffer();
//string msg = Encoding.Unicode.GetString(buffer);
//Console.WriteLine("Received:{0}", msg);
}
}
}
2、客户端程序
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net;
using System.Net.Sockets;
using System.Net.Sockets;
namespace Client
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Client Running ...");
TcpClient client;
try
{
client = new TcpClient();
client.Connect("localhost", 8500); // 与服务器连接
}
catch (Exception ex)
{
Console.WriteLine(ex.Message); return;
}
// 打印连接到的服务端信息
Console.WriteLine("Server Connected!{0} --> {1}",
client.Client.LocalEndPoint, client.Client.RemoteEndPoint);
// string msg = "\"Welcome To TraceFact.Net\"";
string msg = "我是。。。。。。";
NetworkStream streamToServer = client.GetStream();
byte[] buffer = Encoding.Unicode.GetBytes(msg); // 获得缓存
streamToServer.Write(buffer, 0, buffer.Length); // 发往服务器
Console.WriteLine("Sent: {0}", msg);
}
}
}
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Client Running ...");
TcpClient client;
try
{
client = new TcpClient();
client.Connect("localhost", 8500); // 与服务器连接
}
catch (Exception ex)
{
Console.WriteLine(ex.Message); return;
}
// 打印连接到的服务端信息
Console.WriteLine("Server Connected!{0} --> {1}",
client.Client.LocalEndPoint, client.Client.RemoteEndPoint);
// string msg = "\"Welcome To TraceFact.Net\"";
string msg = "我是。。。。。。";
NetworkStream streamToServer = client.GetStream();
byte[] buffer = Encoding.Unicode.GetBytes(msg); // 获得缓存
streamToServer.Write(buffer, 0, buffer.Length); // 发往服务器
Console.WriteLine("Sent: {0}", msg);
}
}
}