TestClient.java
package kr.appcreator.client;
import java.io.IOException;
import java.io.OutputStream;
import java.net.Socket;
import java.net.UnknownHostException;
import kr.appcreator.net.PacketMaker;
public class TestClient {
public static void main(String args[]){
PacketMaker packet= new PacketMaker();
packet.setCommand(1000);
packet.putString("재밋는 프로그래밍 시그");
byte[] data= packet.build();
System.out.println("send Data: " + data.length);
int port= 7787;
TestClient t= new TestClient();
t.send( data, port );
}
private void send(byte[] data, int port) {
Socket s;
try {
s = new Socket("210.118.73.202", port);
OutputStream out= s.getOutputStream();
out.write(data);
s.close();
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
PacketMaker.java
package kr.appcreator.net;
public class PacketMaker {
private static final int INIT_POSITION = 4;
private int command_;
private byte[] buffer_= new byte[1024];
private int position_= INIT_POSITION;
public PacketMaker(byte[] readData) {
position_= INIT_POSITION;
buffer_= readData;
}
public PacketMaker() {
position_= INIT_POSITION;
}
public void setCommand(int command) {
// something code..
}
public void putString(String string) {
// something code..
}
public byte[] build() {
byte[] resultData= new byte[position_];
// something code .. or new code?
return resultData;
}
public int getCommand() {
// something code .. or new code ?
return 0;
}
public String getString() {
return null;
}
public void putInt(int value) {
buffer_[position_++]= (byte)((value >> 24) & 0xFF);
buffer_[position_++]= (byte)((value >> 16) & 0xFF);
buffer_[position_++]= (byte)((value >> 8) & 0xFF);
buffer_[position_++]= (byte)(value & 0xFF);
}
public int getInt() {
int value= (get() << 24 & 0xFF000000);
value = value | ((get() << 16) & 0x00FF0000);
value = value | ((get() << 8) & 0x0000FF00);
value = value | (get() & 0x000000FF);
return value;
}
private int get() {
// something code ..
return 0;
}
private void put(byte data) {
// something code..
}
}