J2ME Socket编程

Socket是套接字,它是基于TCP协议的传输。

在J2ME开发Socket的流程:

1. 服务器监听某个端口
2. 客户端连接服务器的某个端口
3.通过Socket通讯

在J2ME开发包javax.microedition.io中包含了 HttpConnection, HttpsConnection, SocketConnection,ServerSocketConnection和UDPDatagramConnection类,其中输入流和输出流包含在java.io包中,
这里主要用到的是SocketConnection和ServerSocketConnection这两个类;

代码如下

服务器端:


 
  try {  
	  //监听1234端口
	ServerSocketConnection ssc = (ServerSocketConnection) Connector.open("socket://1234");
			SocketConnection sc = (SocketConnection) ssc.acceptAndOpen(); //等待客户连接,如果客户没有连接的话,这里程序就会在这阻塞
			
		} catch (IOException e) {
			e.printStackTrace();
		}   
  


客户端

  
 
		try {  
            															//连接服务器1234端口
			SocketConnection sc = (SocketConnection) Connector.open("socket:127.0.0.1//1234");
		} catch (IOException e) {
			e.printStackTrace();
		}
   
  



通过以上代码这样就能客户端与服务器端通信了,其中服务器端的SocketConnection 对象sc与客服端的SocketConnection 对象sc 是同一个对象。SocketConnection几个常用的方法有:
 
  getAddress();得到客户端地址
  setSocketOption:设置一些参数,一般可以用默认
  openDataInputStream / openInputStream; 打开输入流
  openDataOutputStream / openOutputStream; 打开输出流


例当客服器端向服务器端发送一个字符串,同样在服务器端能够打印出来,代码如下:

服务器端

package com.easymorse;

import java.io.DataInputStream;
import java.io.IOException;

import javax.microedition.io.Connector;
import javax.microedition.io.ServerSocketConnection;
import javax.microedition.io.SocketConnection;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;

public class ServerMIdlet extends MIDlet {

	public ServerMIdlet() {
		
	}

	protected void destroyApp(boolean arg0) throws MIDletStateChangeException {

	}

	protected void pauseApp() {

	}

	protected void startApp() throws MIDletStateChangeException {

		try {  
			                                                                    //监听1234端口
			ServerSocketConnection ssc = (ServerSocketConnection) Connector.open("socket://1234");
			SocketConnection sc = (SocketConnection) ssc.acceptAndOpen(); //等待客户连接,如果客户没有连接的话,这里程序就会在这阻塞
			DataInputStream dis = (DataInputStream) sc.openDataInputStream();
			String msg = dis.readUTF();
			System.out.println(msg);
			
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

}

 



客户端:
 package com.easymorse;

import java.io.DataOutputStream;
import java.io.IOException;

import javax.microedition.io.Connector;
import javax.microedition.io.ServerSocketConnection;
import javax.microedition.io.SocketConnection;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;

public class ClientSocketMIDlet extends MIDlet {

	public ClientSocketMIDlet() {
	}

	protected void destroyApp(boolean unconditional)
			throws MIDletStateChangeException {

	}

	protected void pauseApp() {

	}

	protected void startApp() throws MIDletStateChangeException {

		try {  
            															//连接服务器1234端口
			SocketConnection sc = (SocketConnection) Connector.open("socket:127.0.0.1//1234");
			DataOutputStream dos = sc.openDataOutputStream();
			dos.writeUTF("你好服务器");
			
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		
	}

}

 

你可能感兴趣的:(java,编程,socket,dos)