1。关于多线程
。。
try{
int i = 0;
sSocket = new ServerSocket(YOUR_PORT);
//a dead circle for listening
while(true){
//accept the socket from client
Socket sin = sSocket.accept();
i++;
if (i>99999999) i = 1;
//bring the socket to other class to operate some action
ThreadProc tp = new ThreadProc(sin,i);
}
}catch(Exception ex){
ex.printStackTrace();
}
finally{
try{
endServer();
}catch(Exception ex){}
}
.........................
对每个客户端创建一个新的线程2。去看看java api的手册,关于的线程的方法说明,java里面已将有现成的方法。
3。控制同步;使用synchronized关键字
如:
public synchronized void setDateIntoDatabase(....){....}
这个方法就被同步控制了
多看看api手册.
good luck!
。。
try{
int i = 0;
sSocket = new ServerSocket(YOUR_PORT);
//a dead circle for listening
while(true){
//accept the socket from client
Socket sin = sSocket.accept();
i++;
if (i>99999999) i = 1;
//bring the socket to other class to operate some action
ThreadProc tp = new ThreadProc(sin,i);
}
}catch(Exception ex){
ex.printStackTrace();
}
finally{
try{
endServer();
}catch(Exception ex){}
}
.........................
对每个客户端创建一个新的线程2。去看看java api的手册,关于的线程的方法说明,java里面已将有现成的方法。
3。控制同步;使用synchronized关键字
如:
public synchronized void setDateIntoDatabase(....){....}
这个方法就被同步控制了
多看看api手册.
good luck!
ServerSocket(int port, int backlog)
Creates a server socket and binds it to the specified local port number, with the specified backlog.这个backlog怎么理解,这就是第二个问题的答案
while(true){
newConnection = serverSocket.accept();
ServerHelper sHelper =
new ServerHelper(newConnection,dbHelper);
sHelper.start();
}ServerHelper是个类,生成它的实例来处理一个客户的请求