?? client.java
字號:
import com.common.DateFormat;
import java.io.*;
import java.net.Socket;
import java.net.UnknownHostException;
public class Client extends Thread{
private Socket socket = null;
private OutputStream os = null;
private InputStream is = null;
private boolean stop=false;
private Msg msg;
public Client(String ip,int port){
try {
socket=new Socket(ip,port);
os=socket.getOutputStream();
is=socket.getInputStream();
msg=new Msg(os,is);
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void run(){
while(!stop){
try {
/*
String body="";
int a=Math.abs(new Random().nextInt(5));
for(int i=0;i<a;i++){
body+=dd[new Random().nextInt(5)];
}
*/
//System.out.println("send:"+body);
//msg.setBody(body);
msg.setBody(RobotImage.getByteos().toByteArray());
send();
get();
//sleep(2000);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
_stop();
}
}
}
public synchronized void _stop(){
stop=true;
}
public void send() throws Exception{
msg.send();
}
public void get() throws Exception{
msg.get();
}
public static void main(String args[]){
new Client(args[0],Integer.parseInt(args[1])).start();
}
}
class Msg {
private int length;
//private String body;
private byte[] msg;
private byte[] bodybyte;
private OutputStream os = null;
private InputStream is = null;
public Msg(OutputStream os,InputStream is){
this.os = os;
this.is = is;
}
public void send() throws Exception{
setLength(bodybyte.length);
byte[] bytelength=DateFormat.intTobyte(length);
os.write(bytelength);
os.write(bodybyte);
os.flush();
}
public void get() throws Exception{
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[length];
if((flag=is.read(bodybyte))!=-1){
if(flag==length){
String body=new String(bodybyte);
//System.out.println(body);
}
}
}
}
}
public byte[] getMsg(){
setLength(bodybyte.length);
byte[] bytelength=DateFormat.intTobyte(length);
msg=new byte[bodybyte.length+4];
for(int i=0;i<msg.length;i++){
if(i<4){
msg[i]=bytelength[i];
}else{
msg[i]=bodybyte[i-4];
}
}
return msg;
}
public int getLength() {
return length;
}
public void setLength(int length) {
this.length = length;
}
public String getBody() {
return new String(bodybyte);
}
public void setBody(String body) {
this.bodybyte=body.getBytes();
}
public void setBody(byte[] bodybyte) {
this.bodybyte = bodybyte;
}
public void setMsg(byte[] msg) {
this.msg = msg;
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -