下面来看看socket的编程,j2me为客户端,j2se为服务端
服务端:
public class MultiThreadServer {
private int prot=1234;
private ServerSocket serverSocket;
private ExecutorService executorService;
private final int POOL_SIZE=1;
public MultiThreadServer() throws IOException{
serverSocket=new ServerSocket(prot);
executorService=Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors()*POOL_SIZE);
System.out.println("server start");
}
public void service(){
while(true){
Socket socket=null;
try {
socket=serverSocket.accept();
executorService.execute(new Handler(socket));
} catch (Exception e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) {
try {
new MultiThreadServer().service();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public class Handler implements Runnable {
private Socket socket;
public Handler(Socket socket){
this.socket=socket;
}
private PrintWriter getWrite(Socket socket) throws IOException{
OutputStream socketOut=socket.getOutputStream();
return new PrintWriter(socketOut,true);
}
private BufferedReader getReader(Socket socket) throws IOException{
InputStream socketIn=socket.getInputStream();
return new BufferedReader(new InputStreamReader(socketIn));
}
public String echo(String msg){
return "echo:"+msg;
}
public void run() {
try {
System.out.println("New Connection accepted"+socket.getInetAddress()+":"+socket.getPort());
BufferedReader br=getReader(socket);
PrintWriter pw=getWrite(socket);
String msg=null;
while((msg=br.readLine())!=null){
System.out.println(msg);
pw.println(echo(msg)+"asd");
if(msg.equals("byte")){
break;
}
}
} catch (Exception e) {
e.printStackTrace();
}finally{
try {
if(socket!=null){
socket.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
客户端为j2me:
public class ClientMidlet extends MIDlet implements Runnable {
protected void destroyApp(boolean arg0) throws MIDletStateChangeException {
}
protected void pauseApp() {
}
protected void startApp() throws MIDletStateChangeException {
new Thread(this).start();
}
public void run() {
try {
SocketConnection sc=(SocketConnection)Connector.open("socket://192.168.0.192:1234");
sc.setSocketOption(SocketConnection.DELAY, 0);
sc.setSocketOption(SocketConnection.LINGER, 0);
sc.setSocketOption(SocketConnection.KEEPALIVE, 0);
sc.setSocketOption(SocketConnection.RCVBUF, 5);
sc.setSocketOption(SocketConnection.SNDBUF, 128);
InputStream is=sc.openInputStream();
OutputStream os=sc.openOutputStream();
os.write("hello world/r/n".getBytes());
os.flush();
System.out.println("Response stream:");
int le=0;
byte[] buf=new byte[5];
ByteArrayOutputStream baos = new ByteArrayOutputStream();
DataOutputStream dos = new DataOutputStream(baos);
while(is.available()>0&&(le=is.read(buf, 0, buf.length))!=-1){
System.out.println("55555");
dos.write(buf, 0, le);
}
byte[] data = baos.toByteArray();
String strxml = new String(data, "utf-8");
System.out.println(strxml);
System.out.println("aaaaaaaaaaaaaaa");
is.close();
os.close();
sc.close();
} catch (IOException e) {
e.printStackTrace();
}
}
public static byte[] readFully(InputStream is) {
byte[] bytes = null;
if (is != null) {
try {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
int l = 0;
byte[] b = new byte[1024];
while ((l = is.read(b)) != -1) {
baos.write(b, 0, l);
}
bytes = baos.toByteArray();
baos.close();
baos = null;
} catch (IOException e) {
e.printStackTrace();
}
}
return bytes;
}
}
客户端为j2se:
public class MultiThreadClient {
public static void main(String[] args) {
int numTasks=1;
ExecutorService exec=Executors.newCachedThreadPool();
for(int i=0;i<numTasks;i++){
exec.execute(createTask(i));
}
}
private static Runnable createTask(final int taskId){
return new Runnable(){
private Socket socket=null;
private int port=1234;
public void run(){
System.out.println("Task "+taskId+":start");
try {
socket=new Socket("localhost",port);
OutputStream socketOut=socket.getOutputStream();
socketOut.write("shutdowm/r/n".getBytes());
BufferedReader br=new BufferedReader(new InputStreamReader(socket.getInputStream()));
String msg=null;
while((msg=br.readLine())!=null){
System.out.println(msg);
}
} catch (Exception e) {
e.printStackTrace();
}finally{
try {
if(socket!=null){
socket.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
};
}
}