VC的链接顺序

CRT库在链接new, delete, DllMain这些符号的时候,使用的是弱外链接
MFC库同样也包含了new, delete, DllMain这几个符号,而MFC库必须在CRT库之前被链接才会成功。

如果link时出现了符号冲突,做下面的设置即可
project>properties>configuration properties>linker>input
在”Additional dependency”中依次增加 Nafxcwd.lib Libcmtd.lib
在add to “ignore specific library”中依次增加Nafxcwd.lib;Libcmtd.lib

存储单位

Kilobyte〈KB〉=1024 Bytes
Megabyte〈MB〉=1024 Kilobytes
Gigabyte〈GB〉=1024 Megabytes
Terabyte〈TB〉=1024 Gigabytes
Petabyte〈PB〉=1024 Terabytes
Exabyte〈EB〉 =1024 Petabyte,
Zettabyte〈ZB〉=1024 Exabyte
Yottabyte〈YB〉=1024 Zettabyte

1KB相当于一则短篇故事的文字内容;
1MB相当于阅读一则短篇小说的文字内容;
1GB相当于贝多芬第五章交响曲的乐谱内容;
1TB相当于一家大型医院中所有X光图片资讯量;
1PB相当于50%的全美学术研究图书馆藏书资讯内容;
5EB相当于至今全世界人类所讲过的话语;
1ZB如同全世界海滩的沙子总和;
1YB相当于7000位人类体内的微细胞总和;

CS访问网络资源

using System.Runtime.InteropServices;
 
public class WNetHelper
{
	 [DllImport("mpr.dll", EntryPoint = "WNetAddConnection2")]
	 private static extern uint WNetAddConnection2(NetResource lpNetResource, string lpPassword, string lpUsername, uint dwFlags);
 
	 [DllImport("Mpr.dll", EntryPoint = "WNetCancelConnection2")]
	 private static extern uint WNetCancelConnection2(string lpName, uint dwFlags, bool fForce);
 
	 [StructLayout(LayoutKind.Sequential)]
	 public class NetResource
	 {
		  public int dwScope;
		  public int dwType;
		  public int dwDisplayType;
		  public int dwUsage;
		  public string lpLocalName;
		  public string lpRemoteName;
		  public string lpComment;
		  public string lpProvider;
	 }
 
	 public static uint WNetAddConnection(string username, string password, string remoteName, string localName)
	 {
		  NetResource netResource = new NetResource();
 
		  netResource.dwScope = 2;
		  netResource.dwType = 1;
		  netResource.dwDisplayType = 3;
		  netResource.dwUsage = 1;
		  netResource.lpLocalName = localName;
		  netResource.lpRemoteName = remoteName.TrimEnd('\\');
		  uint result = WNetAddConnection2(netResource, password, username, 0);
 
		  return result;
	 }
 
	 public static uint WNetCancelConnection(string name, uint flags, bool force)
	 {
		  uint nret = WNetCancelConnection2(name, flags, force);
		  return nret;
	 }
}

VC服务程序调用远程资源

当VC服务程序调用远程资源时,经常返回路径不存在等问题
这是因为Windows中,服务程序以System用户登录,而不是桌面用户登录
这样就导致,虽然桌面程序已经映射网络资源,但System用户仍无法访问的问题
为了可以访问远程资源,可以调用API:WNetAddConnection2

BOOL AcessNetworkDrtive(CString szDevice,CString szDeviceName,CString szUsername,CString szPassword)
{
	DWORD dwRetVal;
	NETRESOURCE nr;

	memset(&nr, 0, sizeof (NETRESOURCE));
	nr.dwType = RESOURCETYPE_ANY;
	nr.lpLocalName = strDevice.GetBuffer(szDevice.GetLength());
	nr.lpRemoteName = szDeviceName.GetBuffer(szDeviceName.GetLength());
	nr.lpProvider = NULL;

	dwRetVal = WNetAddConnection2(&nr, szUsername, szUsername, CONNECT_UPDATE_PROFILE);

	if (dwRetVal != NO_ERROR)
	{
		CString cError;
		cError.Format(TEXT("[ERROR]WNetAddConnection2 Failed: %u\n"), dwRetVal);
		LogEvent(cError,TEXT("With remote name "),nr.lpRemoteName);
		return dwRetVal;
	}

	return 	dwRetVal;
}

Nginx反向代理upstream跳转错误

前两天架设Nginx反向代理时,用ip hash的方法设置了几个upstream(backend1,backend2,backend3…)

发现有一个网站总是去查找
http://backend2/
最后域名解析失败。
可其他网站都是好的啊。

无奈用Firebug看了一下网页,http header中赫然写着一行
baseurl=http://backend2/

找出jsp文件,发现是MyEclipse生成模板是,会加这么几行

<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<head>
    <base href="<%=basePath%>"]
</head>

Java多线程同步的几种方式

1、原子变量
AtomicBoolean
AtomicInteger
AtomicLong
AtomicReference
AtomicStampedReference
AtomicIntegerArray
AtomicLongArray
AtomicReferenceArray

2、Unsafe类
Unsafe.monitorEnter(this);
Unsafe.monitorExit(this);

3、synchronized关键字
同步静态方法
同步普通方法
同步代码块

4、锁
Object.wait()方法和Object.notify()方法:

5、ReentrantLock重入锁
Condition

6、ReadWriteLock读写锁
StampedLock

7、Semaphore信号量

8、BlockingQueue阻塞队列
ArrayBlockingQueue
LinkedBlockingQueue
PriorityBlockingQueue
LinkedBlockingDeque
SynchronousQueue
DelayQueue

9、CyclicBarrier

10、CountDownLatch

11、Phaser

12、Exchanger

13、ForkJoinPool
RecursiveTask

13、Future
CompletionService
CompletableFuture
FutureTask

14、Collections
Collections.synchronizedList
Collections.synchronizedSet
Collections.synchronizedMap

15、volatile变量
可以解决并发读的问题,无法单独解决并发写的问题

Java多线程实现的几种方式

1、继承Thread类

2、实现Runnable接口

3、实现Callable接口+Future封装

4、定时器

5、使用Executors提供的ExecutorService
Executors.newCachedThreadPool
Executors.newFixedThreadPool
Executors.newScheduledThreadPool
Executors.newSingleThreadExecutor
Executors.newWorkStealingPool

6、自定义ThreadPoolExecutor+ThreadFactory

7、parallelStream

8、Fork Join
ForkJoinPool
RecursiveTask
RecursiveAction

9、调用本地方法
使用JNI等

用VC编译Nginx

1、安装Mysis和VC
2、用svn从Nginx取回源代码
3、调用下面的bat

@call "D:\Microsoft Visual Studio 9.0\VC\bin\vcvars32.bat"
@call "D:\GNUstep\msys.bat"
cmd

4、Config

./auto/configure --prefix= --sbin-path=nginx --with-cc=cl --with-cc-opt="" --without-http_rewrite_module --without-http_gzip_module

5、Make

nmake

Nginx做负载均衡

只需要3步:

1、在nginx.conf中的http段增加upstream服务器

    upstream backend{  
        server 192.168.140.15:18080 weight=2;  
        server 192.168.140.81:18080;  
        server 192.168.140.82:18080;  
    }

2、在nginx.conf中的http段增加server

    server {
        listen       18080;
        server_name  localhost;

        location / {
            proxy_pass http://backend/;
            proxy_redirect off;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        }
    }

3、重启Nginx

正向代理和反向代理

1、forward proxy
正向代理是一个位于client和origin server之间的proxy,为了从origin server取得内容,client向proxy发送一个请求并指定目标(origin server),然后proxy向origin server转交request并将获得的resopnse返回给client。client必须要进行一些特别的设置才能使用正向代理。

2、reverse proxy
反向代理对于client而言就像是origin server,并且clinet不需要进行任何特别的设置。client向反proxy发送普通请求,接着proxy将判断向何处(origin server)转交request,并将response返回给client,proxy在client看起来就像origin server一样。

3、两者区别
从网络角色来讲:
正向代理隐藏的是client,clinet可以感觉到正向代理的存在,proxy对server隐藏了client的细节。
反向代理隐藏的是origin server,client感觉不到反向代理的存在,proxy对clinet隐藏了server的细节。

从用途上来讲:
正向代理的典型用途是为在防火墙内的局域网客户端提供访问Internet的途径,正向代理还可以使用缓冲特性减少网络使用率。
反向代理的典型用途是将防火墙后面的服务器提供给Internet用户访问,反向代理还可以为后端的多台服务器提供负载平衡,或为后端较慢的服务器提供缓冲服务。另外,反向代理还可以启用高级URL策略和管理技术,从而使处于不同web服务器系统的web页面同时存在于同一个URL空间下。

从安全性来讲:
正向代理允许client通过它访问任意网站并且隐藏client自身,因此你必须采取安全措施以确保仅为经过授权的客户端提供服务。
反向代理对clinet都是透明的,clinet并不知道自己访问的是一个代理。

4、透明代理
透明代理技是指客户端感觉不到代理的存在,不需要在浏览器中设置任何代理,客户只需要设置缺省网关,客户的访问外部网络的数据包被发送到缺省网关,而这时缺省网关运行有一个代理服务器,数据实际上被被重定向到代理服务器的代理端口,即由本地代理服务器向外请求所需数据然后拷贝给客户端。理论上透明代理可以对任何协议通用。一般透明代理用于防火墙技术比较多。