⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 socketclient.java

📁 一个及时通信的客户端 是用java写的 主要利用了ftpClient类 解压后可以直接运行
💻 JAVA
字号:
import java.io.InputStream;
import java.io.OutputStream;

import javax.microedition.io.Connector;
import javax.microedition.io.ServerSocketConnection;
import javax.microedition.io.SocketConnection;
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;

public class SocketClient extends MIDlet implements Runnable, CommandListener {

	private Display display;

	private Form form;

	private TextField tf;

	private StringItem si;

	private boolean isstop;

	private Command sendCommand = new Command("Send", Command.ITEM, 1);

	private Command exitCommand = new Command("Exit", Command.EXIT, 1);

	InputStream is;

	OutputStream os;

	SocketConnection sc;

	public SocketClient() {
		display = Display.getDisplay(this);
		form = new Form("Socket Client");
		si = new StringItem("status:", " ");
		tf = new TextField("send:", "", 30, TextField.ANY);
		form.append(si);
		form.append(tf);
		form.addCommand(exitCommand);
		form.addCommand(sendCommand);
		form.setCommandListener(this);
		display.setCurrent(form);
		Thread t = new Thread(this);
		t.start();

	}

	protected void startApp() throws MIDletStateChangeException {
		// TODO Auto-generated method stub

	}

	protected void pauseApp() {
		// TODO Auto-generated method stub

	}

	protected void destroyApp(boolean arg0) {
		// TODO Auto-generated method stub

	}

	public void run() {
		try {
			si.setText("Connected to Server");

			sc = (SocketConnection) Connector.open("socket://localhost:8000");
			si.setText("Connection accepted");
			is = sc.openInputStream();
			os = sc.openOutputStream();
			while (true) {
				StringBuffer sb = new StringBuffer();
				int c = 0;
				while (((c = is.read()) != '\n') && (c != -1)) {
					sb.append((char) c);
				}
				if (c == -1) {
					break;
				}
				si.setText("Message received - " + sb.toString());
			}
			stop();
			si.setText("Connection is closed");
			form.removeCommand(sendCommand);

		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	public void commandAction(Command c, Displayable d) {
		if (c == sendCommand) {
			try {
				os.write(tf.getString().getBytes());
				os.write("\r\n".getBytes());
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		if (c == exitCommand) {
			destroyApp(false);
			this.notifyDestroyed();
		}

	}

	public void stop() {
		try {
			if (is != null) {
				is.close();
			}
			if (os != null) {
				os.close();
			}
			if (sc != null) {
				sc.close();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -