Unix Network Programming – Big or little endian Que

#include "header.h"
#include "unp.h"
int main(int argc, char** argv) 
{
    union {
        short s;
        char c[sizeof(short)];
    } un;
    un.s = 0x0102;
    printf("%s: ", CPU_VENDOR_OS);
    if (sizeof(short) == 2) {
        if (un.c[0] == 1 && un.c[1] == 2) {
            printf("big-endian\n");
        } else if (un.c[0] == 2 && un.c[1] == 1) {
            printf("little-endian\n");
        } else {
            printf("unknown\n");
        }
    } else {
        printf("sizeof(short)=%d\n", (int)sizeof(short));
    }
    exit(0);
}

C# – Socket开发

using System;
using System.Net.Sockets;
using System.Net;  
using System.Threading;
using System.Text;

namespace TsetC001
{
	class MainClass
	{
		// 服务器
		private static byte[] result = new byte[1024];  
		private static int myProt = 8885;   //端口  
		static Socket serverSocket;  
		static void Main(string[] args)  
		{  
			//服务器IP地址  
			IPAddress ip = IPAddress.Parse("127.0.0.1");  
			serverSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);  
			serverSocket.Bind(new IPEndPoint(ip, myProt));  //绑定IP地址:端口  
			serverSocket.Listen(10);    //设定最多10个排队连接请求  
			Console.WriteLine("启动监听{0}成功", serverSocket.LocalEndPoint.ToString());  
			//通过Clientsoket发送数据  
			Thread myThread = new Thread(ListenClientConnect);  
			myThread.Start();  
			Console.ReadLine();  
		}  

		///   
		/// 监听客户端连接  
		///   
		private static void ListenClientConnect()  
		{  
			while (true)  
			{  
				Socket clientSocket = serverSocket.Accept();  
				clientSocket.Send(Encoding.ASCII.GetBytes("Server Say Hello"));  
				Thread receiveThread = new Thread(ReceiveMessage);  
				receiveThread.Start(clientSocket);  
			}  
		}  

		///   
		/// 接收消息  
		///   
		///   
		private static void ReceiveMessage(object clientSocket)  
		{  
			Socket myClientSocket = (Socket)clientSocket;  
			while (true)  
			{  
				try  
				{  
					//通过clientSocket接收数据  
					int receiveNumber = myClientSocket.Receive(result);  
					Console.WriteLine("接收客户端{0}消息{1}", myClientSocket.RemoteEndPoint.ToString(), Encoding.ASCII.GetString(result, 0, receiveNumber));  
				}  
				catch(Exception ex)  
				{  
					Console.WriteLine(ex.Message);  
					myClientSocket.Shutdown(SocketShutdown.Both);  
					myClientSocket.Close();  
					break;  
				}  
			}  
		}

		// 客户端
		/*private static byte[] result = new byte[1024];  
		static void Main(string[] args)  
		{  
			//设定服务器IP地址  
			IPAddress ip = IPAddress.Parse("127.0.0.1");  
			Socket clientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);  
			try  
			{  
				clientSocket.Connect(new IPEndPoint(ip, 8885)); //配置服务器IP与端口  
				Console.WriteLine("连接服务器成功");  
			}  
			catch  
			{  
				Console.WriteLine("连接服务器失败,请按回车键退出!");  
				return;  
			}  
			//通过clientSocket接收数据  
			int receiveLength = clientSocket.Receive(result);  
			Console.WriteLine("接收服务器消息:{0}",Encoding.ASCII.GetString(result,0,receiveLength));  
			//通过 clientSocket 发送数据  
			for (int i = 0; i < 10; i++)  
			{  
				try  
				{  
					Thread.Sleep(1000);    //等待1秒钟  
					string sendMessage = "client send Message Hellp" + DateTime.Now;  
					clientSocket.Send(Encoding.ASCII.GetBytes(sendMessage));  
					Console.WriteLine("向服务器发送消息:{0}" + sendMessage);  
				}  
				catch  
				{  
					clientSocket.Shutdown(SocketShutdown.Both);  
					clientSocket.Close();  
					break;  
				}  
			}  
			Console.WriteLine("发送完毕,按回车键退出");  
			Console.ReadLine();  
		}*/
	}
}

Install Mono on Linux

Mono

官方网站上的解释为:Cross platform, open source .NET framework。既然他是支持跨平台的.NET框架,那么我们可以通过这个将C#做的服务器运行到Linux上。
这里我们可以对应系统所需要的Mono版本。

  • CentOS 7, Fedora 19 (and later), and derivatives
  • Add the Mono Project GPG signing key and the package repository in a root shell with:

    yum install yum-utils
    rpm --import "http://keyserver.ubuntu.com/pks/lookup?op=get&search=0x3FA7E0328081BFF6A14DA29AA6A19B38D3D831EF"
    yum-config-manager --add-repo http://download.mono-project.com/repo/centos/
    

    日志参考如下

    # yum install yum-utils
    已加载插件:fastestmirror, langpacks
    Repository epel is listed more than once in the configuration
    Repodata is over 2 weeks old. Install yum-cron? Or run: yum makecache fast
    epel                                                     | 4.3 kB     00:00     
    extras                                                   | 3.4 kB     00:00     
    os                                                       | 3.6 kB     00:00     
    updates                                                  | 3.4 kB     00:00     
    (1/4): epel/7/x86_64/updateinfo                            | 791 kB   00:00     
    (2/4): extras/7/x86_64/primary_db                          | 151 kB   00:00     
    (3/4): updates/7/x86_64/primary_db                         | 4.8 MB   00:00     
    (4/4): epel/7/x86_64/primary_db                            | 4.7 MB   00:00     
    Determining fastest mirrors
    正在解决依赖关系
    --> 正在检查事务
    ---> 软件包 yum-utils.noarch.0.1.1.31-34.el7 将被 升级
    ---> 软件包 yum-utils.noarch.0.1.1.31-40.el7 将被 更新
    --> 正在处理依赖关系 yum >= 3.4.3-143,它被软件包 yum-utils-1.1.31-40.el7.noarch 需要
    --> 正在检查事务
    ---> 软件包 yum.noarch.0.3.4.3-132.el7.centos.0.1 将被 升级
    ---> 软件包 yum.noarch.0.3.4.3-150.el7.centos 将被 更新
    --> 正在处理依赖关系 python-urlgrabber >= 3.10-8,它被软件包 yum-3.4.3-150.el7.centos.noarch 需要
    --> 正在检查事务
    ---> 软件包 python-urlgrabber.noarch.0.3.10-7.el7 将被 升级
    ---> 软件包 python-urlgrabber.noarch.0.3.10-8.el7 将被 更新
    --> 解决依赖关系完成
    
    依赖关系解决
    
    ================================================================================
     Package                 架构         版本                       源        大小
    ================================================================================
    正在更新:
     yum-utils               noarch       1.1.31-40.el7              os       116 k
    为依赖而更新:
     python-urlgrabber       noarch       3.10-8.el7                 os       108 k
     yum                     noarch       3.4.3-150.el7.centos       os       1.2 M
    
    事务概要
    ================================================================================
    升级  1 软件包 (+2 依赖软件包)
    
    总下载量:1.4 M
    Is this ok [y/d/N]: y
    Downloading packages:
    Delta RPMs disabled because /usr/bin/applydeltarpm not installed.
    (1/3): python-urlgrabber-3.10-8.el7.noarch.rpm             | 108 kB   00:00     
    (2/3): yum-utils-1.1.31-40.el7.noarch.rpm                  | 116 kB   00:00     
    (3/3): yum-3.4.3-150.el7.centos.noarch.rpm                 | 1.2 MB   00:00     
    --------------------------------------------------------------------------------
    总计                                                11 MB/s | 1.4 MB  00:00     
    Running transaction check
    Running transaction test
    Transaction test succeeded
    Running transaction
      正在更新    : python-urlgrabber-3.10-8.el7.noarch                         1/6 
      正在更新    : yum-3.4.3-150.el7.centos.noarch                             2/6 
      正在更新    : yum-utils-1.1.31-40.el7.noarch                              3/6 
      清理        : yum-utils-1.1.31-34.el7.noarch                              4/6 
      清理        : yum-3.4.3-132.el7.centos.0.1.noarch                         5/6 
      清理        : python-urlgrabber-3.10-7.el7.noarch                         6/6 
      验证中      : yum-utils-1.1.31-40.el7.noarch                              1/6 
      验证中      : yum-3.4.3-150.el7.centos.noarch                             2/6 
      验证中      : python-urlgrabber-3.10-8.el7.noarch                         3/6 
      验证中      : yum-utils-1.1.31-34.el7.noarch                              4/6 
      验证中      : yum-3.4.3-132.el7.centos.0.1.noarch                         5/6 
      验证中      : python-urlgrabber-3.10-7.el7.noarch                         6/6 
    
    更新完毕:
      yum-utils.noarch 0:1.1.31-40.el7                                              
    
    作为依赖被升级:
      python-urlgrabber.noarch 0:3.10-8.el7    yum.noarch 0:3.4.3-150.el7.centos   
    
    完毕!
    # rpm --import "http://keyserver.ubuntu.com/pks/lookup?op=get&search=0x3FA7E0328081BFF6A14DA29AA6A19B38D3D831EF"
    # yum-config-manager --add-repo http://download.mono-project.com/repo/centos/
    已加载插件:fastestmirror, langpacks
    Repository epel is listed more than once in the configuration
    adding repo from: http://download.mono-project.com/repo/centos/
    
    [download.mono-project.com_repo_centos_]
    name=added from: http://download.mono-project.com/repo/centos/
    baseurl=http://download.mono-project.com/repo/centos/
    enabled=1
    

    同样我们从http://download.mono-project.com/sources/mono/mono-4.2.1.102.tar.bz2这里地址下载源码,然后进行编译运行!
    我们直接运行yum install mono-devel
    安装完成后可以进行验证,如下即可

    # mono -V
    Mono JIT compiler version 4.8.1 (Stable 4.8.1.0/22a39d7 Wed Apr 12 11:42:39 UTC 2017)
    Copyright (C) 2002-2014 Novell, Inc, Xamarin Inc and Contributors. www.mono-project.com
    	TLS:           __thread
    	SIGSEGV:       altstack
    	Notifications: epoll
    	Architecture:  amd64
    	Disabled:      none
    	Misc:          softdebug 
    	LLVM:          supported, not enabled.
    	GC:            sgen
    [root@VM_74_138_centos mono-4.2.1]# 
    

    下面我们测试一下运行.NET的exe程序

    # mono ConsoleApplication1.exe 
    Hello World!
    
    #