《Unity3D网络游戏实战》第1章

34 篇文章 3 订阅
12 篇文章 2 订阅

查看本机IP地址

ipconfig

VS2019命令行编译C#

//搜索栏打开命令行编译窗口
Developer Command Prompt for VS 2019

//csc *.cs

//帮助
-help

//指定生成文件名
-out:<file>

//指定生成文件类型
/target:appcontainerexe
	To create an .exe file for Windows 8.x Store apps.
/target:exe
    To create an .exe file.
/target:library
    To create a code library.
/target:module
    To create a module.
/target:winexe
    To create a Windows program.
/target:winmdobj
    To create an intermediate .winmdobj file.
//Compiles File.cs producing File.exe:最简命令
csc File.cs 

//Compiles File.cs producing File.dll:动态库
csc /target:library File.cs

//Compiles File.cs and creates My.exe:指定生成文件名
csc /out:My.exe File.cs

//Compiles all the C# files in the current directory, with optimizations on and defines the //DEBUG symbol. The output is File2.exe:
csc /define:DEBUG /optimize /out:File2.exe *.cs

//Compiles all the C# files in the current directory producing a debug version of File2.dll. No //logo and no warnings are displayed:
csc /target:library /out:File2.dll /warn:0 /nologo /debug *.cs

//Compiles all the C# files in the current directory to Something.xyz (a DLL):
csc /target:library /out:Something.xyz *.cs

//多个cs文件编译
csc /out:main.exe *.cs

//生成和引用dll
csc /target:library /out:lib/Class1.dll Class1.cs
// /lib:lib指定lib路径,可多个(,分开)
//HelloWorld.exe运行时,需要和Class1.dll在同一个目录
csc /lib:lib /reference:Class1.dll HelloWorld.cs

//编译二个文件,但输出文件中的Main方法来自Main1 Class
csc myMain1.cs myMain2.cs /main:Main1

Echo服务器端

EchoServer.cs

using System;
using System.Net;
using System.Net.Sockets;

namespace EchoServer {
	class MainClass {
		public static void Main(string[] args) {
			//Socket
			Socket listenfd = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
			
			//Bind
			IPAddress ipAdr = IPAddress.Parse("127.0.0.1");
			IPEndPoint ipEp = new IPEndPoint(ipAdr, 8888);
			listenfd.Bind(ipEp);
			
			//Listen
			listenfd.Listen(0);
			
			Console.WriteLine("[服务器]启动成功");
			
			//Accept
			Socket connfd = listenfd.Accept();
			Console.WriteLine("[服务器]Accept");
			
			while(true) {
				//Receive
				byte[] readBuff = new byte[1024];
				int count = connfd.Receive(readBuff);
				if(count<=0)
					break;
				string readStr = System.Text.Encoding.UTF8.GetString(readBuff, 0, count);
				Console.WriteLine("[服务器接收]" + readStr);
				
				//Send
				byte[] sendBytes = System.Text.Encoding.Default.GetBytes(readStr);
				connfd.Send(sendBytes);
				Console.WriteLine("[服务器发送]" + readStr);
			}
			
			//Close
			connfd.Close();
			listenfd.Close();
		}
	}
}
csc EchoServer.cs
EchoServer

Echo客户端

EchoClient.cs

using System;
using System.Net.Sockets;

namespace EchoClient {
	class MainClass {
		public static void Main(string[] args) {
			//Socket
			Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
			
			//Connect
			socket.Connect("127.0.0.1", 8888);
		
			while(true) {
				string sendStr = Console.ReadLine();
				if(sendStr=="quit")
					break;
				
				//Send
				byte[] sendBytes = System.Text.Encoding.Default.GetBytes(sendStr);
				socket.Send(sendBytes);
				Console.WriteLine("[客户端发送]" + sendStr);
				
				//Recv
				byte[] readBuff = new byte[1024]; 
				int count = socket.Receive(readBuff);
				string recvStr = System.Text.Encoding.UTF8.GetString(readBuff, 0, count);
				Console.WriteLine("[客户端接收]" + recvStr);
			}
			
			//Close
			socket.Close();
		}
	}
}
csc EchoClient.cs
EchoClient
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值