当前位置:编程学习 > JAVA >>

关于serversocket.accept()阻塞求助

发送线程部分代码:
public void run()
{
try
{
Socket sendToMyself = null;
if (flag == 0) { // 发送文字消息
ObjectInputStream oin = new ObjectInputStream(client.getInputStream());
this.message = new chatMessage((chatMessage) oin.readObject());
if (message.anotherQQ.equals(mInfo.myself.qq))
{
...
}
else
{
if (MainInte易做图ce.map_ipPort.get(message.anotherQQ) != null)
{
sendToMyself = new Socket(map_ipPort.get(message.anotherQQ).ip, map_ipPort.get(message.anotherQQ).chatPort);
ObjectOutputStream oout = new ObjectOutputStream(sendToMyself.getOutputStream());
oout.writeObject(message);
}
else
{
...
}
}
}
else {// flag == 1,发送文件
ObjectInputStream oin = new ObjectInputStream(client.getInputStream());
this.fmessage = new fileMessage((fileMessage) oin.readObject());
if (fmessage.anotherQQ.equals(mInfo.myself.qq))
{}
else
{
if (MainInte易做图ce.map_ipPort.get(fmessage.anotherQQ) != null)
{
if (sendToMyself == null) {
sendToMyself = new Socket(map_ipPort.get(fmessage.anotherQQ).ip, map_ipPort.get(fmessage.anotherQQ).chatPort);
}
DataOutputStream oout = new DataOutputStream(sendToMyself.getOutputStream());
FileInputStream fin = new FileInputStream(fmessage.file);
if (fmessage.file != null) {
byte[] bytes = new byte[(int) fmessage.file.length()];
int len = -1;
while ((len = fin.read(bytes)) != -1) {
oout.write(bytes);
}
}
}
else
{
...
}
}
}
client.close();
}
catch (IOException e)
{}
catch (ClassNotFoundException e)
{}
}
}


接收线程部分代码:
	class receiver_messageThread extends Thread
{
ServerSocket server = null;
Socket client = null;

public receiver_messageThread() throws IOException
{
server = getAvailableServerSocketFunction.getServerSocket(MainInte易做图ce.begin_window_port);
MainInte易做图ce.map_portLocal.put(anotherQQ, server.getLocalPort());
}

public void run()
{
try
{
while (true)
{
client = server.accept();
receiverMessage_dealingThread receiveMsg = new receiverMessage_dealingThread(client);
}
}
catch (IOException e)
{}
}
}

// ////////////////////////////下面是处理线程////////////////////////////////////////////////
class receiverMessage_dealingThread extends Thread
{
Socket client = null;
chatMessage message = null;
fileMessage         fmessage = null;
ObjectInputStream oin = null;
FileOutputStream    fout    = null;

public receiverMessage_dealingThread(Socket client)
{
this.client = client;
start();
}

public void run()
{
try
{
if (MainInte易做图ce.flag == 0) {//接受消息
oin = new ObjectInputStream(client.getInputStream());
message = (chatMessage) oin.readObject();
// 获取对方QQ号码,备注(昵称),个性签名,性别
jt_recive.insert_text2(anotherQQnickname + "  " + message.time);
int c1 = color1, c2 = color2, c3 = color3, sizeTemp = font_size;
transformToReceive(message.text);
color1 = c1;
color2 = c2;
color3 = c3;
font_size = sizeTemp;
jt_recive.setFontSize(font_size);
jt_recive.setColor(new Color(c1, color2, color3));
jt_recive.selectAll();
jt_recive.setCaretPosition(jt_recive.getSelectedText().length());
jt_recive.requestFocus();
jt_send.requestFocus();
}
else {//接受文件
jt_recive.insert_text2(anotherQQnickname + " " + time.gettime() + "\n");
jt_recive.insert_text2("正在传送文件。。。");
fout = new FileOutputStream(new File("E:/"+ "接受文件"));
DataInputStream in = new DataInputStream(client.getInputStream());
byte[] bytes = new byte[1024];
int len = in.read(bytes);
while (len != -1) {
fout.write(bytes , 0 , len );
len = in.read(bytes);
}
MainInte易做图ce.flag = 0;
System.out.println("接受完毕!");
}
client.close();
}
catch (Exception e)
{}
}
}

代码在接受消息时候是正常的,可是我在接受文件的时候阻塞了!这是怎么回事啊?
补充:Java ,  Java SE
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,