📄 mainserver.java
字号:
import java.io.*;
import java.net.*;
import java.awt.*;
public class MainServer{
public TextArea area;
public TextField tf1;
public MainServer(TextArea area,TextField tf1){
this.area=area;
this.tf1=tf1;
}
static final int PORT=8080;
public void serverStart()throws IOException{
ServerSocket s=new ServerSocket(PORT);
System.out.println("Server Start");
try{
while(true) {
// Blocks until a connection occurs:
Socket socket = s.accept();
try {
new ServeOneJabber(socket);
} catch(IOException e) {
// If it fails, close the socket,
// otherwise the thread will close it:
socket.close();
}
}
} finally {
s.close();
}
}
class ServeOneJabber extends Thread{
private Socket ket;
private BufferedReader in;
private PrintWriter out;
//public TextArea area;
public ServeOneJabber(Socket s) throws IOException{
ket=s;
in=new BufferedReader(new InputStreamReader(ket.getInputStream()));
out=new PrintWriter(new BufferedWriter(new OutputStreamWriter(ket.getOutputStream())),true);
start();
}
public void run(){
try {
String str=in.readLine();
if(str.charAt(0)=='-'){
GetFile gf=new GetFile(getname(str));
gf.start();
area.append("get it...\n");
System.out.println("get it...");
}
else{
System.out.println(str);
area.append(str+"\n");
}
}catch(IOException e){}
try{
ket.close();
}catch(IOException e){}
}
public String getname(String s){
int i=5;
while(i>0){
s=s.substring(s.indexOf('/')+1);
System.out.println(s);
i--;
}
return s;
}
class GetFile extends Thread{
byte byteBuffer[]= new byte[1024];
Socket tempSocket;
RandomAccessFile inFile;
InputStream inSocket;
public GetFile(String s){
try{
inFile=new RandomAccessFile("./test/"+s,"rw");
tempSocket = new Socket(tf1.getText(),9090);
inSocket= tempSocket.getInputStream();
}catch(Exception e){}
}
public void run(){
int amount;
try{
while((amount =inSocket.read(byteBuffer) )!= -1){
inFile.write(byteBuffer, 0, amount);
}
inSocket.close();
area.append("Get OK\n");
inFile.close();
tempSocket.close();
}catch(IOException e){}
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -