就是打//??????????????????????????????的地方,这两个地方我看不太懂,哪位大侠指点一下??
import java.io.*;
import java.net.*;
import java.util.*;
//CRobot类模拟机器人的行为
//worker类继承Thread类
class worker extends Thread {
Socket sock;
int nos=8;
long svals[];
final long s_max_val = 10000;
final int max_change = 500;
Random R;
//构造函数,以Socket为参数,Socket为来自客户端的连接
worker(Socket s)
{
int i;
sock = s;
R=new Random();
svals = new long[nos];
for (i=0;i<nos;i++)
{
//????????????????????????????????????????????????????????????
svals[i]=(long)Math.abs(R.nextLong()/((double)Long.MAX_VALUE/s_max_val));
}
}
//线程类的run方法,在每个线程中自动运行
public void run(){
int i;
byte bs[];
System.out.println("Thread running:"+currentThread() );
// Get I/O streams from the socket
//定义输入输出流
PrintStream out = null;
InputStreamReader isr = null;
try {
//从socket中得到输入输出流
out = new PrintStream( sock.getOutputStream() );
isr = new InputStreamReader( sock.getInputStream() );
BufferedReader in = new BufferedReader(isr);
while(true)
{
String reply = new String("");
byte[] b={0};
//InSt为客户端输入流
InputStream InSt=sock.getInputStream();
String bst;
// get the request
// As Ascii
//以字节方式从客户端接收用户输入,打印到控制台上,再把该输入并入reply字符串上,直至客户端回车
do
{
b[0]=(byte)InSt.read(); /* read the next byte */
bst=new String(b); /* convert to UNICODE */
System.out.print(bst);
reply=reply + bst;
}while(bst.compareTo("\n")!=0);
//把客户端输入输出到控制台
System.out.println("Got : " + reply);
//当客户端输入含有“sweep”时
if (reply.indexOf("sweep") > -1)
{
// modify the values and prepare the string
//????????????????????????????????????????????????????????????
String query="sweep/"+(nos-1);
for (i=0;i<nos-1;i++)
{
svals[i]+=R.nextInt(max_change*2)-max_change;
svals[i]=Math.abs(svals[i] % s_max_val);
query=query+":"+i+1+"/"+svals[i];
}
bs=(query+"\n").getBytes(); //convert to ascii
out.write(bs,0,query.length()+1); //send the string as ASCII over the network
System.out.print("Send to Server :" + new String(bs));
out.flush();
//????????????????????????????????????????????????????????????
}
}
} catch(IOException ioe){System.out.println(ioe); }
}
}
//CRobot 类模拟机器人的行为
public class CRobot {
public static void main(String a[]) throws IOException {
//程序入口
int q_len = 6;
int port = 4444;
Socket sock;
//建立一个服务器Socket,指定4444端口,连接数为6
ServerSocket servsock = new ServerSocket(port, q_len);
while (true) {
// 接收客户端连接
sock=servsock.accept();
// 开始一个新的Worker线程
new worker( sock ).start();
}
}
}
svals[i]=(long)Math.abs(R.nextLong()/((double)Long.MAX_VALUE/s_max_val));
Math.abs是取R.nextLong的绝对值
Long.MAX_VALUE 是 263-1
这样可以明白么