Client:
public class XCli
{
private static int bufSize = 32;
private static byte[] buff = new byte[bufSize];
private static FileInputStream fis;
private static ObjectInputStream ois;
private static BufferedOutputStream bos;
private static BufferedInputStream bis;
private static Socket s;
public static void main(String[] args)
{
try
{
s = new Socket("127.0.0.1",20003);
bis =new BufferedInputStream(s.getInputStream());
bos = new BufferedOutputStream(s.getOutputStream());
int s2cType;
while((s2cType = bis.read())!=-1) //问题出在这里吗?第一个线程的请求接收到了,第二个线程的请求就没有接收到
{
if(s2cType == 1)
{
ois = new ObjectInputStream(s.getInputStream());
String name = (String)ois.readObject();
bos = new BufferedOutputStream(s.getOutputStream());
bos.write(1);
bos.flush();
sendFile(name);
}
}
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
服务器主线程:
public class Xserv {
private static String savepath = "C:\\hello.txt";
private static int bufSize = 32;
private static byte[] buff = new byte[bufSize];
private static final int serverPort = 20003;
private static Socket s;
private static FileOutputStream fos;
private static BufferedInputStream bis;
public static final ReentrantLock socketWriteLock = new ReentrantLock();
public static RequestThread t1;
public static RequestThread t2;
public static Object obj = new Object();
public static void main(String[] args)
{
try
{
ServerSocket ss = new ServerSocket(serverPort);
s = ss.accept();
t1 = new RequestThread(s,"C:\\Codes\\test.java");
t1.start();
t2 = new RequestThread(s,"C:\\Codes\\test2.java");
t2.start();
bis = new BufferedInputStream(s.getInputStream());
int type = 0;
服务器是否一直在监听?还是运行一次就完不跑了?(新手说的不好谅解下)
[/Quote]可能我说的不清楚,抱歉。服务器对于每一个用户单独启用一个线程用来服务,我采用的是自制协议的方式交流,服务器的模型为
BufferedInputStream bis = new BufferedInputStream(socket.getInputStream);
int orderType;
while(ture)
{
orderType = bis.read();