Java客户端接收信息,运行了getmessage线程,但是只有第一个getmessage能把线程走完,第二个线程到readutf就停止.(socket在传递)

浅墨ザ 2018-05-11 06:39:58

//服务端
private static ServerSocket s;
static ArrayList<ClientBean> list;
private ClientBean own;
public Socket_Thread() {
try {
s=new ServerSocket(9999);
} catch (IOException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
list=new ArrayList();
own = new ClientBean();
own.setName(null);
own.setSocket(null);
start();
}
public void run() {
while (true) {
try {
Socket socket = s.accept();
/*DataInputStream in = new DataInputStream(socket.getInputStream());
String all=in.readUTF();
this.getClientBean().setName(all);
System.out.println("连接的新用户:"+this.getID());*/
this.getClientBean().setSocket(socket);
list.add(this.getClientBean());
receivemessage(socket);
//receiveFile(socket);
} catch (Throwable e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
}
}


//收信息
public synchronized void receivemessage(Socket socket) throws Throwable {
new Thread(new Runnable() {
@Override
public void run() {
while(true) {
try {
DataInputStream reader = new DataInputStream(socket.getInputStream());
String all=reader.readUTF();
String info[]=all.split(",");

String to=info[0];
String id=info[1];
String type=info[2];
if(info[2].equals("1")) {
// int n;
byte[]array=new byte[1024];
int n=reader.read(array);
// while((n=reader.read(array))!=-1) {
System.out.println("准备发送");
if(!new String(array,0,n).equals("")) {
sendmessage(to,id,type,array,n);
System.out.println("已发送");
}

System.out.println("服务器收到"+info[1]+"给"+info[0]+"的 "+new String(array,0,n));
array=new byte[1024];
}
else if(type.equals("2")){
String suffix=info[3];
byte[]array=new byte[1024];
int n;
while((n=reader.read(array))!=-1) {
sendfile(to,id,type,suffix,array,n);
array=new byte[1024];
}

}
} catch (IOException e) {
}
}
}
}).start();
}

//发信息
public synchronized void sendmessage(String to,String id,String type,byte[] array,int n) {

new Thread(new Runnable() {

@Override
public void run() {
for(int i=0;i<list.size();i++) {
try {
DataOutputStream writer=new DataOutputStream((list.get(i).getSocket()).getOutputStream());
String all=to+","+id+","+type;
writer.writeUTF(all);
writer.write(array,0,n);
writer.flush();
} catch (IOException e) {
e.printStackTrace();
}

}
}
}).start();
}


//客户端收信息 并显示在面板上
public class getMessage extends Thread{
ClientBean client;
String message;
String all;
JTextArea show_area;
public getMessage(JTextArea show_area,ClientBean client) {
this.show_area=show_area;
this.client=client;
receivemessage();
}
public synchronized void receivemessage() {
new Thread(new Runnable(){
// TODO 自动生成的方法存根
public void run() {
while(true) {
try {
System.out.println("1");
DataInputStream reader = null;
System.out.println("2");
try {
System.out.println("3");
reader = new DataInputStream(client.getSocket().getInputStream());
System.out.println("4");
} catch (IOException e1) {
e1.printStackTrace();
}
String all=reader.readUTF();
System.out.println("5");
String []array=all.split(",");
String talkWith = array[1];
boolean flag=array[0].equals(client.getName());
byte[]array1=new byte[1024];
int n = 0;
if(array[2].equals("1")&&flag) {
System.out.println("6");
n=reader.read(array1);
System.out.println("7");
System.out.println("进来");
//while((n=reader.read(array1))!=-1) {
show_area.append(talkWith+"说:"+new String(array1,0,n)+"\n");
//}
}
} catch (IOException e1) {
// TODO 自动生成的 catch 块
e1.printStackTrace();
}
}
}
}).start();
}
}
...全文
1444 回复 打赏 收藏 转发到动态 举报
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复

58,452

社区成员

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

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