求J2ME高手帮助,socket服务端客户端问题.

xj13738946595 2010-10-18 04:07:21
我的客户端:

package login;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;

import javax.microedition.io.Connector;
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.TextField;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;

import canvas.WelcomeCanvas;

public class LoginMIDlet extends MIDlet implements CommandListener {
private Display display;
private Form form;
private Form form2;
private Command command;
private Command command2;
TextField user;
TextField password;
TextField info;
TextField acceptedInfo;
DataOutputStream dos;
DataInputStream dis;
SocketConnection sc;
String str;
public LoginMIDlet() {

display = Display.getDisplay(this);
user = new TextField("用户帐号", "", 10, user.ANY);
password = new TextField("用户密码", "", 10, user.PASSWORD);
command = new Command("登录", command.OK, 1);
form = new Form("Login");
form.addCommand(command);
form.append(user);
form.append(password);
form.setCommandListener(this);
form2 = new Form("聊天室");
command2 = new Command("发送", command2.OK, 1);
form2.addCommand(command2);
info = new TextField("您要输入的信息", "", 100, info.ANY);
acceptedInfo = new TextField("您接收到的信息", "", 100, acceptedInfo.ANY);
form2.append(acceptedInfo);
form2.append(info);
form2.setCommandListener(this);

}

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

}

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

}

protected void startApp() throws MIDletStateChangeException {

WelcomeCanvas welcome = new WelcomeCanvas(display, form);
welcome.setDisplayTime(6000);
display.setCurrent(welcome);

}

public void commandAction(Command c, Displayable arg1) {
if (c==command) {
new Thread(new chatThread()).start();
display.setCurrent(form2);

}

if (c==command2) {
try {

dos.writeUTF(info.getString());
info.setString(null);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
class chatThread implements Runnable{

public void run() {
// TODO Auto-generated method stub
try {
sc=(SocketConnection)Connector.open("socket://127.0.0.1:8888");//打开连接
dos=sc.openDataOutputStream();
dis=sc.openDataInputStream();
while(true){

str=dis.readUTF();
acceptedInfo.setString(str);
form2.append(acceptedInfo);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}



}
}

服务端:
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.Scanner;

/**
*
* @author Administrator
* 1. 建立serversocket
* 2. 启动线程
* 3. 等待客户端连接
* 4. 保存客户端连接到List
* 5.
* 如果收到消息,则遍历list, 发送消息到各个客户端 收到消息,直接echo回客户端
*/
public class ChatServer implements Runnable {

ServerSocket ss = null;
int port = 8080;

public ChatServer() {
try {
ss = new ServerSocket(port);
new Thread(this).start();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

Socket socket = null;
ArrayList list = new ArrayList();

public void run() {
// TODO Auto-generated method stub
while (true) {
try {
socket = ss.accept();
list.add(socket);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

/**
* @param args
*/
public static void main(String[] args) {

}

class Worker extends Thread {
Socket socket = null;
DataInputStream dis;
DataOutputStream dos;

public Worker(Socket socket) {
try {
dis = new DataInputStream(socket.getInputStream());
dos = new DataOutputStream(socket.getOutputStream());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}

public void run() {
boolean yesno=true;
while (yesno) {
for (int i = 0; i < list.size(); i++) {
if (list.size()>0) {
try {
dos.writeUTF(list.get(i).toString());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}else {
yesno=false;
}
}

}
}
}
}


整了一个下午都是javax.microedition.io.ConnectionNotFoundException:openTCP 这个异常,
我是新手..求解答求帮助.
...全文
38 点赞 收藏 3
写回复
3 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
renjianguokeivor 2010-10-20
[Quote=引用 2 楼 zyc1314199 的回复:]
服务端的是8080端口,客户端的是8888端口。。。。。。
[/Quote]
好眼力
回复
zyc1314199 2010-10-20
服务端的是8080端口,客户端的是8888端口。。。。。。
回复
我要戒烟了 2010-10-19
你的电脑上有8888端口的程序开着么?
回复
相关推荐
发帖
J2ME
创建于2007-09-28

1.3w+

社区成员

Java J2ME
申请成为版主
帖子事件
创建了帖子
2010-10-18 04:07
社区公告
暂无公告