?? server.java
字號:
import com.common.DateFormat;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import javax.swing.JFrame;
public class Server {
private ServerSocket server = null;
private Socket socket = null;
public Server(int port){
try {
server=new ServerSocket(port);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void start(){
while(true){
try {
SocketThread st=new SocketThread(server.accept());
new Thread(st).start();
st.setVisible(true);
} catch (IOException e) {
// TODO Auto-generated catch block
//e.printStackTrace();
}
}
}
public static void main(String[] args) {
if(args==null){
System.out.println("please input the listen port");
}
new Server(Integer.parseInt(args[0])).start();
}
}
class SocketThread extends JFrame implements Runnable{
private Socket socket = null;
private OutputStream os = null;
private InputStream is = null;
private FileOutputStream fos = null;
private FileInputStream fis = null;
private boolean stop=false;
private File file;
public SocketThread(Socket socket){
this.setSize(1024, 768);
this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
System.out.println(this.getName());
this.socket=socket;
try {
os=socket.getOutputStream();
is=socket.getInputStream();
file=new File("c:/1.jpeg");
fos=new FileOutputStream(file);
fis=new FileInputStream(file);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void run(){
int i=0;
while(!stop){
i++;
try {
byte[] lengthbyte=new byte[4];
int flag;
if((flag=is.read(lengthbyte))!=-1){
if(flag==4){
int length=DateFormat.byteToint(lengthbyte);
System.out.println("length:"+length);
byte[] bodybyte=new byte[1024];
ByteArrayOutputStream byteos=new ByteArrayOutputStream();
while((flag=is.read(bodybyte))!=-1){
//System.out.println(flag);
byteos.write(bodybyte, 0, flag);
if(byteos.size()==length){
break;
}
//if(flag==length){
//String body=new String(bodybyte);
//System.out.println(body);
//}
}
System.out.println("byteos length:"+byteos.toByteArray().length);
fos.write(byteos.toByteArray());
fos.flush();
//RobotImage.toFile("d:/"+i+".jpeg", byteos.toByteArray());
String msg="from server ok";
byte[] bytelength=DateFormat.intTobyte(msg.getBytes().length);
os.write(bytelength);
os.write(msg.getBytes());
os.flush();
repaint();
}
}
//_stop();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
_stop();
}
}
}
public void paint(Graphics g){
try {
Graphics2D g2d=(Graphics2D)g;
Image img=javax.imageio.ImageIO.read(fis);
g2d.drawImage(img, 0, 0, this);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public synchronized void _stop(){
stop=true;
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -