📄 threads.java
字号:
import java.io.*;
import java.net.*;
import javax.swing.*;
import javax.swing.text.*;
/**
* 建立服务器的线程
*
* @author 戚荣波
*
* @version 1.0
* @since J2SE 1.6
*/
class ServerThread extends Thread
{
private ObjectOutputStream m_output;
private ObjectInputStream m_input;
private Socket s;
int port;
public ServerThread(int port)
{
this.port = port;
}
public void run()
{
ServerSocket server;
try
{
server = new ServerSocket(port);
SomethingStored.server = server;
s = Connect.mb_server(server);
SomethingStored.s = getSocket();
m_output = new ObjectOutputStream(s.getOutputStream());
m_input = new ObjectInputStream(s.getInputStream());
SomethingStored.m_output = m_output;
SomethingStored.m_input = m_input;
SChattingFrame.writeSMessage(SMessage.MT_NAME,null,SomethingStored.userName,null);
SChattingFrame.showMessage("Connected");
SChattingFrame.audioPlay("connect.wav");
SomethingStored.destIP = s.getInetAddress();
new ReceiveThread().start();
}
catch(BindException e)
{
return;
}
catch(NullPointerException e)
{
return;
}
catch(SocketException e)
{
return;
}
catch(Exception exp)
{
System.err.println("Exception:"+exp);
exp.printStackTrace();
}
}
public ObjectOutputStream getOut()
{
return m_output;
}
public ObjectInputStream getIn()
{
return m_input;
}
public Socket getSocket()
{
return s;
}
}
/**
* 文件传输时的服务器线程
*
* @author 戚荣波
*
* @version 1.0
* @since J2SE 1.6
*/
class FileServerThread extends Thread
{
int m_port;
boolean m_show;
ServerSocket server;
Socket s;
File f;
boolean isdie;
long fileSize = 0,index = 0;
public FileServerThread(File f,int port ,boolean b_show)
{
m_port = port ;
m_show = b_show ;
this.f = f;
fileSize = f.length();
}
public void run()
{
SomethingStored.isSending = true;
try
{
int r = 0;
server = new ServerSocket(m_port);
if(m_show == false)
loop:
while(true)
{
sleep(500);
r = ReceiveThread.getResponse();
if(r == 0)continue loop;
else if(r == 2)
{
ReceiveThread.setResponse(0);
JOptionPane.showMessageDialog(ShowWindow.app, "Rejected!!", "alert", JOptionPane.ERROR_MESSAGE);
server.close();
SomethingStored.isSending = false;
return;
}
else if(r == 1)
{
ReceiveThread.setResponse(0);
SChattingFrame.showMessage("File is sending...");
break loop;
}
}
s = server.accept();
FileInputStream fos=new FileInputStream(f);
OutputStream netOut=s.getOutputStream();
netOut.flush();
OutputStream doc=new DataOutputStream(new BufferedOutputStream(netOut));
doc.flush();
byte[] buf=new byte[2048]; //创建文件读取缓冲区
StyleToolBar.showLabel.setVisible(true);
String str = null;
int num=fos.read(buf);
while(num!=(-1))//是否读完文件
{
index += num;
doc.write(buf,0,num);//把文件数据写出网络缓冲区
doc.flush();//刷新缓冲区把数据写往客户端
str= String.format("%1$4.2f%%",(double)index*100/fileSize);
StyleToolBar.showLabel.setText(str);
num=fos.read(buf);//继续从文件中读取数据
}
fos.close();
doc.close();
s.close();
server.close();
if(m_show == false)
{
SChattingFrame.showMessage("File sended successfully");
SChattingFrame.audioPlay("file.wav");
}
}
catch(SocketException se)
{
SChattingFrame.showMessage("ERROR:File sending failed");
}
catch(Exception e)
{
System.err.println("Exception:"+e);
e.printStackTrace();
}
StyleToolBar.showLabel.setVisible(false);
SomethingStored.isSending = false;
}
}
/**
* 文件传输时的客户端线程
*
* @author 戚荣波
*
* @version 1.0
* @since J2SE 1.6
*/
class FileReceiveThread extends Thread
{
int m_port;
boolean m_show;
Socket s;
String fileName;
long fileSize;
long index = 0L;
/**
*构造方法
*
*@param fileName 文件名
*@param fileSize 文件大小 当传输图片时表示图片的位置
*@port 端口
*@b_show 为真时表示传输的是图片
*/
public FileReceiveThread(String fileName,long fileSize,int port ,boolean b_show)
{
m_port = port;
m_show = b_show;
this.fileName = fileName;
this.fileSize = fileSize;
}
public void run()
{
SomethingStored.isSending = true;
RandomAccessFile raf = null;
InputStream netIn = null;
InputStream in = null;
try
{
File f = new File("Received");
if(!f.exists())f.mkdir();
else if(f.isFile())
{
if(f.isFile())f.delete();
f.mkdir();
}
s = new Socket(SomethingStored.destIP,m_port);
f=new File("Received"+File.separator+fileName);
StyledDocument doc = SomethingStored.tp_show.getStyledDocument();
int l = doc.getLength(),position = 0;
if(f.exists())f.delete();
f.createNewFile();
raf=new RandomAccessFile(f,"rw");
netIn=s.getInputStream();
in=new DataInputStream(new BufferedInputStream(netIn));
byte[] buf=new byte[2048]; //创建缓冲区缓冲网络数据
int num=in.read(buf);
if(m_show == false)StyleToolBar.showLabel.setVisible(true);
StyleToolBar.showLabel.repaint();
String str = null;
while(num!=(-1))//是否读完所有数据
{
raf.write(buf,0,num);//将数据写往文件
index += num;
str= String.format("%1$4.2f%%",(double)index*100/fileSize);
StyleToolBar.showLabel.setText(str);
raf.skipBytes(num);//顺序写文件字节
num=in.read(buf);//继续从网络中读取文件
}
in.close();
raf.close();
s.close();
if(m_show == false)
{
SChattingFrame.showMessage("File received.(in Received folder)");
SChattingFrame.audioPlay("file.wav");
}
else
{
if(fileSize == -1L)
{
doc.insertString(doc.getLength(),SomethingStored.yourName+":\n",SomethingStored.defaultAttributeSet);
SChattingFrame.showPicture(doc.getLength(),f.getPath()); // 插入图片
doc.insertString(doc.getLength(),"\n",SomethingStored.defaultAttributeSet);
}
}
}
catch(SocketException se)
{
SChattingFrame.showMessage("ERROR:File receiving failed");
}
catch(Exception e)
{
System.err.println("Exception:"+e);
e.printStackTrace();
}
SomethingStored.isSending = false;
StyleToolBar.showLabel.setVisible(false);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -