Objective-C中@class和#import的问题。参考。
假设,有两个类:ClassA和ClassB,两个之间相互使用到,即构成了circular dependency(循环依赖)。如果在头文件里面只用#import把对方的头文件包含进来(构成circular inclusions,循环包含),则编译器会报错:
Expected specifier-qualifier-list before ‘ClassA’
或者
Expected specifier-qualifier-list before ‘ClassB’
为了避免循环包含,在ClassA.h文件里面用@class classB把classB包含进来,同样,在ClassB.h文件里面用@class ClassA把ClassA包含进来。@class指令只是告诉编译器,这是个类,保留个空间来存放指针就可以了。
接下来,很可能在ClassA.m和ClassB.m中会有访问包含进来对象的成员的情况,这时必须让编译器知道更多信息,比如那个类有些什么方法可以调用,就必须用#import,再次把用到的类包含进来,告诉编译器所需要的额外信息。
否则,编译器会警告:
warning: receiver ‘ClassA’ is a forward class and corresponding @interface may not exist
还有另一种情况,使用有Categories的类,要在.h头文件里用#import把Categories包含进来。
总之,使用原则是:
判断当前版本是否需要升级时,首先与服务端的配置文件里内容作比较,如果当前版本小于服务端版本,就升级,当前的版本号放在AndroidMainifest.xml配置文件里
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.thunder.ktv.activity" android:versionCode="1" android:versionName="1.0">
versionCode为版本号,versionName为版本名称,我们可以得到这两个值,然后再通赤请求,从服务器得到最新版本信息,通过比较,判断是否需要升级。
得到配置文件里面版本信息,我们可以通过以下代码来实现
int versionCode = 0; PackageManager packageManager = this.getPackageManager(); try { PackageInfo packageInfo = packageManager.getPackageInfo(this .getPackageName(), 0); versionCode = packageInfo.versionCode; } catch (NameNotFoundException e) { e.printStackTrace(); }
采用Java 5的ExecutorService来进行线程池的方式实现多线程,模拟客户端多用户向同一服务器端发送请求.1.服务端
package sterning;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.*;
import java.util.concurrent.*;
public class MultiThreadServer {
private int port=8821;
private ServerSocket serverSocket;
private ExecutorService executorService;//线程池
private final int POOL_SIZE=10;//单个CPU线程池大小
public MultiThreadServer() throws IOException{
serverSocket=new ServerSocket(port);
//Runtime的availableProcessor()方法返回当前系统的CPU数目.
executorService=Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors()*POOL_SIZE);
System.out.println("服务器启动");
}
public void service(){
while(true){
Socket socket=null;
try {
//接收客户连接,只要客户进行了连接,就会触发accept();从而建立连接
socket=serverSocket.accept();
executorService.execute(new Handler(socket));
} catch (Exception e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) throws IOException {
new MultiThreadServer().service();
}
}
class Handler implements Runnable{
private Socket socket;
public Handler(Socket socket){
this.socket=socket;
}
private PrintWriter getWriter(Socket socket) throws IOException{
OutputStream socketOut=socket.getOutputStream();
return new PrintWriter(socketOut,true);
}
private BufferedReader getReader(Socket socket) throws IOException{
InputStream socketIn=socket.getInputStream();
return new BufferedReader(new InputStreamReader(socketIn));
}
public String echo(String msg){
return "echo:"+msg;
}
public void run(){
try {
System.out.println("New connection accepted "+socket.getInetAddress()+":"+socket.getPort());
BufferedReader br=getReader(socket);
PrintWriter pw=getWriter(socket);
String msg=null;
while((msg=br.readLine())!=null){
System.out.println(msg);
pw.println(echo(msg));
if(msg.equals("bye"))
break;
}
} catch (IOException e) {
e.printStackTrace();
}finally{
try {
if(socket!=null)
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
2.客户端
package sterning;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.Socket;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class MultiThreadClient {
public static void main(String[] args) {
int numTasks = 10;
ExecutorService exec = Executors.newCachedThreadPool();
for (int i = 0; i < numTasks; i++) {
exec.execute(createTask(i));
}
}
// 定义一个简单的任务
private static Runnable createTask(final int taskID) {
return new Runnable() {
private Socket socket = null;
private int port=8821;
public void run() {
System.out.println("Task " + taskID + ":start");
try {
socket = new Socket("localhost", port);
// 发送关闭命令
OutputStream socketOut = socket.getOutputStream();
socketOut.write("shutdown\r\n".getBytes());
// 接收服务器的反馈
BufferedReader br = new BufferedReader(
new InputStreamReader(socket.getInputStream()));
String msg = null;
while ((msg = br.readLine()) != null)
System.out.println(msg);
} catch (IOException e) {
e.printStackTrace();
}
}
};
}
}
从而实现了多个客户端向服务器端发送请求,服务器端采用多线程的方式来处理的情况