server.cs

来自「Csharp实例编程百例.rar」· CS 代码 · 共 32 行

CS
32
字号
using System;
using System.Text;
using System.Net;
using System.Net.Sockets;

public class server
{
	public static void Main()
	{
			IPAddress ipAd=IPAddress.Parse("127.0.0.1");
			TcpListener myList=new TcpListener(ipAd,8001);
			myList.Start();
			Console.WriteLine("start the service at port 8001...");
			Console.WriteLine("local node : "+myList.LocalEndpoint);
			Console.WriteLine("wait for the connection...");

			Socket s=myList.AcceptSocket();
			Console.WriteLine("connection from "+s.RemoteEndPoint);
			byte[] b=new byte[100];
			int k=s.Receive(b);
			Console.WriteLine("have accpeted..");
			for (int i=0;i<k;i++)
			{
				Console.Write(Convert.ToChar(b[i]));
			}
			ASCIIEncoding asen=new ASCIIEncoding();
			s.Send(asen.GetBytes("The string was recieved by the server."));
			Console.WriteLine("\n");
			s.Close();
			myList.Stop();
	}
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?