谁能帮帮我!

daowang 2002-09-19 08:10:37
请看下面的程序:
//* 服务端
import java.io.*;
import java.net.*;
import java.util.*;
public class Chatserver{
public static void main(String Args[]){
int Pport;
Chatserver chat=new Chatserver();
ServerSocket Hhost=null;
int Pperson=0;
int flag=0,flag2=0;
Vector Sockets=new Vector(Pperson+1);
Vector Pthread=new Vector(Pperson);
Socket Cilent=null,Cilent2=null,Cilent3=null;
int j=0;
Ppipe tmp;
Ppipe cc=new Ppipe();
Object o;
try
{
Pport=Integer.parseInt(Args[0]);

Pperson=Integer.parseInt(Args[1]);
}catch(NumberFormatException e)
{
Pport=5000;
Pperson=10;//*创建端口
}
try
{
Hhost=new ServerSocket(Pport);
System.out.println("Thank you ,listening at port:"+Integer.toString(Pport));
}
catch(IOException e)
{
System.out.println("connect error!");
}
while(true)
{try
{if(flag<9){
while(Sockets.size()<2)
{
Cilent=Hhost.accept();//*监听端口
Sockets.addElement((Object)Cilent);
flag++;
cc.fflag=flag;
cc.Sockets.addElement((Object)Cilent);//*保存信息
}
if(flag>0)
{
for(Enumeration E=Sockets.elements();E.hasMoreElements();)
{
Cilent3=(Socket)E.nextElement();
cc.Out=new PrintStream(Cilent3.getOutputStream());
for(j=0;j<flag;j++)
{
cc.sendline("state");
cc.sendline(Integer.toString(j));
}
}
}
}
if(flag>0)
{ while((Socket)Sockets.elementAt(flag2)!=null&&flag2<Sockets.size()-1)
{ Cilent2=(Socket)Sockets.elementAt(flag2);
o=new Ppipe(Cilent2);
Pthread.addElement((Object)o);
tmp=(Ppipe)o;
flag2++;
tmp.start();//*创建线程以处理各个客户

}
}
}catch(IOException e)
{

}
}
}

}
class Ppipe extends Thread
{
Socket a=null;
Socket b=null;
Socket c=null;
Vector Sockets=new Vector();
DataInputStream In=null;
PrintStream Out=null;
String message="";
String buf="";
int i=0;
int fflag=0;
public Ppipe(){}
public Ppipe(Socket a)
{
this.a=a;
}
public synchronized void run()
{

while(true)
{ while(i>fflag){
i=getstate(a);
System.out.println("select error!");
}
c=b;
if(i==-1)
b=c;
else this.b=(Socket)Sockets.elementAt(i);
try
{
In=new DataInputStream(a.getInputStream());//*初始化流
Out=new PrintStream(b.getOutputStream());
while(message.equals("")){message=reiceiveline();
notify();
yield();}
buf=reiceiveline();
if(message.equals("message"))
{
sendline("message");
sendline(buf);
}
}
catch(IOException e)
{
}
}
}

public String reiceiveline()
{
try {return In.readLine();}
catch(IOException e)
{return "";
}
}
public void sendline(String message)
{

message=message;
Out.println(message);
}

public synchronized int getstate(Socket a)
{
String message;
String buf;

Integer i;

try{ In=new DataInputStream(a.getInputStream());}
catch(IOException e)
{
}
message=reiceiveline();
buf=reiceiveline();
if(message.equals("state"))
{
i=new Integer(buf);
return i.intValue();
}
else return -1;
}
}
它在jdk中编译通过了,但运行时却抛出了一个异常,java.lang.ArrayIndexOutOfBoundsException at Chatserver.main<Chatserver.java:21>


希望不吝赐教!谢谢!
...全文
57 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
yonghengdizhen 2002-09-19
  • 打赏
  • 举报
回复
摆明了是数组越界..你就找你操作数组的语句呀..

这个该放到java版的.

87,994

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 JavaScript
社区管理员
  • JavaScript
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧