Test du socket sur PDA sous WM6.1 !
Nk36's Home :: Informatique :: Programmation :: Java
Page 1 sur 1
Que pensez vous de ce bout de code ?
Test du socket sur PDA sous WM6.1 !
Et voila un petit bout de code que j'ai fait pour envoyé des requêtes SMTP sur une adresse que l'on spécifie.
Le but était d'envoyé cela à mon PC qui les redirigeait sur le SMTP de Free.
Le but était d'envoyé cela à mon PC qui les redirigeait sur le SMTP de Free.
- Code:
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import java.io.InputStream;
import java.io.IOException;
import java.io.OutputStream;
import javax.microedition.io.Connector;
import javax.microedition.io.StreamConnection;
import javax.microedition.lcdui.Alert;
import javax.microedition.lcdui.AlertType;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.StringItem;
import javax.microedition.lcdui.TextField;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;
/**
* @author Nk
*/
public class SocketMIDlet extends MIDlet implements CommandListener, Runnable {
private Display display;
private Form addressForm;
private Form connectForm;
private Form displayForm;
private TextField serverName;
private TextField serverPort;
private StringItem messageLabel;
private StringItem errorLabel;
private Command okCommand;
private Command exitCommand;
private Command backCommand;
StreamConnection socket = null;
OutputStream os = null;
InputStream is = null;
public SocketMIDlet() {
}
public void connect(){
System.out.println("o-> connect()");
try {
String server = serverName.getString();
String port = serverPort.getString();
String name = "socket://" + server + ":" + port;
socket = (StreamConnection)Connector.open(name, Connector.READ_WRITE);
} catch (Exception ex) {
Alert alert = new Alert("Invalid Address",
"The supplied address is invalid\n" +
"Please correct it and try again.", null,
AlertType.ERROR);
alert.setTimeout(Alert.FOREVER);
display.setCurrent(alert, addressForm);
System.out.println("");
return;
}
inputOpen();
outputOpen();
System.out.println("<- connect()");
}
public void inputOpen(){
try {
is = socket.openInputStream();
System.out.println("socket.openInputStream() succeed");
} catch (IOException ex) {
try {
is.close();
System.out.println("socket.openInputStream() maybe already open !");
} catch (IOException ex1) {
System.out.println("socket.openInputStream() cannot be close !");
}
}
}
public void outputOpen(){
try {
os = socket.openOutputStream();
System.out.println("socket.openOutputStream() succeed");
} catch (IOException ex) {
try {
os.close();
System.out.println("socket.openOutputStream() maybe already open !");
} catch (IOException ex1) {
System.out.println("socket.openOutputStream() cannot be close !");
}
}
}
public void sendData(String request){
System.out.println("o-> sendData(String request)");
try {
// Send a message to the server
os.write(request.getBytes());
display.setCurrent(displayForm);
} catch (IOException ex) {
Alert alert = new Alert("Output Error",
"An error occurred while communicating with the server.",
null, AlertType.ERROR);
alert.setTimeout(Alert.FOREVER);
display.setCurrent(alert, addressForm);
return;
}
String tmpStr = messageLabel.getText();
messageLabel.setText(tmpStr + "Envoyé : " + request);
System.out.println("<- sendData(String request)");
}
public String receiveData(){
System.out.println("o-> receiveData()");
String reply = null;
try {
// Read the server's reply, up to a maximum
// of 128 bytes.
final int MAX_LENGTH = 128;
byte[] buf = new byte[MAX_LENGTH];
int total = 0;
while (total == 0) {
int count = is.read(buf, total, MAX_LENGTH - total);
if (count < 0) {
break;
}
total += count;
System.out.println("=> total = " + total);
}
reply = new String(buf, 0, total);
System.out.print("reply = " + reply + "\n");
display.setCurrent(displayForm);
} catch (IOException ex) {
Alert alert = new Alert("Input Error",
"An error occurred while communicating with the server.",
null, AlertType.ERROR);
alert.setTimeout(Alert.FOREVER);
display.setCurrent(alert, addressForm);
}
System.out.println("<- receiveData()");
String tmpStr = messageLabel.getText();
if (tmpStr == null)
messageLabel.setText("Recu : " + reply);
else
messageLabel.setText(tmpStr + "Recu : " + reply);
return reply;
}
public void close(){
System.out.println("o-> close()");
try {
socket.close();
} catch (IOException ex) {
Alert alert = new Alert("Connection Close Error",
"An error occurred while communicating with the server.",
null, AlertType.ERROR);
alert.setTimeout(Alert.FOREVER);
display.setCurrent(alert, addressForm);
System.out.println("<- close()");
return;
} finally {
// Close open streams and the socket
try {
if (socket != null) {
socket.close();
socket = null;
}
} catch (IOException ex1) {}
}
System.out.println("<- close()");
}
public void run() {
System.out.println("o-> run()");
String reponseSocket = "";
String reponse = "";
connect();
reponse = receiveData();
sendData("HELO free\n");
reponse = receiveData();
sendData("MAIL FROM: <nk36@free.fr>\n");
reponse = receiveData();
sendData("RCPT TO: <nk36@free.fr>\n");
reponse = receiveData();
sendData("DATA\n");
reponse = receiveData();
sendData("coucou\n");
sendData("coucou\n");
sendData("coucou\n");
sendData("coucou\n");
sendData(".\n");
reponse = receiveData();
sendData("quit\n");
close();
try{
is.close();
os.close();
}catch(IOException e){}
System.out.println("<- run()");
}
public void commandAction(Command cmd, Displayable d) {
if (cmd == okCommand) {
Thread t = new Thread(this);
t.start();
display.setCurrent(connectForm);
} else if (cmd == backCommand) {
display.setCurrent(addressForm);
} else if (cmd == exitCommand) {
try {
destroyApp(true);
} catch (MIDletStateChangeException ex) {
}
notifyDestroyed();
}
}
private void initialize() {
display = Display.getDisplay(this);
// Commands
exitCommand = new Command("Exit", Command.EXIT, 0);
okCommand = new Command("OK", Command.OK, 0);
backCommand = new Command("Back", Command.BACK, 0);
// The address form
addressForm = new Form("Socket Client");
serverName = new TextField("Server name:", "82.233.83.32", 256, TextField.ANY);
serverPort = new TextField("Server port:", "5443", 8, TextField.NUMERIC);
addressForm.append(serverName);
addressForm.append(serverPort);
addressForm.addCommand(okCommand);
addressForm.addCommand(exitCommand);
addressForm.setCommandListener(this);
// The connect form
connectForm = new Form("Connecting");
messageLabel = new StringItem(null, "Connecting...\nPlease wait.");
connectForm.append(messageLabel);
connectForm.addCommand(backCommand);
connectForm.setCommandListener(this);
// The display form
displayForm = new Form("Server Reply");
messageLabel = new StringItem(null, null);
displayForm.append(messageLabel);
displayForm.addCommand(backCommand);
displayForm.setCommandListener(this);
}
public void startMIDlet() {
}
public void resumeMIDlet() {
}
public void switchDisplayable(Alert alert, Displayable nextDisplayable) {
Display display = getDisplay();
if (alert == null) {
display.setCurrent(nextDisplayable);
} else {
display.setCurrent(alert, nextDisplayable);
}
}
public Display getDisplay () {
return Display.getDisplay(this);
}
public void startApp() {
if (display == null) {
initialize();
display.setCurrent(addressForm);
}
}
public void pauseApp() {
}
public void destroyApp(boolean unconditional) throws MIDletStateChangeException {
}
}
Nk36's Home :: Informatique :: Programmation :: Java
Page 1 sur 1
Permission de ce forum:
Vous ne pouvez pas répondre aux sujets dans ce forum
|
|