Javasocket 与ActionScript3的简单通信问题(新手没有多少分啊 谢谢了)
java server端package com.testSocket.lyx;
import java.net.*;
import java.io.*;
public class TCPServer {
public TCPServer() {
try {
ServerSocket server = new ServerSocket(8888);
//server.bind(new InetSocketAddress("127.0.0.1",8888));
System.out.println("servicer 已启动");
while (true) {
TCPServerThread tct = new TCPServerThread(server.accept());
tct.start();
}
} catch (IOException ex) {
ex.printStackTrace();
}
}
public static void main(String[] args) throws IOException {
new TCPServer();
}
}
package com.testSocket.lyx;
import java.net.*;
import java.io.*;
public class TCPServerThread extends Thread {
Socket client;
volatile static int num = 0;
public TCPServerThread(Socket c) {
this.client = c;
System.out.println(client.getInetAddress().getHostAddress() + ":"
+ client.getPort());
}
@Override
public void run() {
int i = ++num;
System.out.println("client " + i + " is starting");
System.out.println("请输入:");
try {
DataInputStream in = new DataInputStream(client.getInputStream());
DataOutputStream out = new DataOutputStream(client
.getOutputStream());
BufferedReader wt = new BufferedReader(new InputStreamReader(
System.in));
while (true) {
if (in.available() > 0) {
String str = in.readUTF();
System.out.println("clientSay:"+str);
if (str.equals("end") || (null == str)) {
break;
}
} else {
if (wt.ready()) {
System.out.println("请输入:");
String str = wt.readLine();
out.writeUTF(str);
out.flush();
} else {
try {
Thread.sleep(100);
} catch (Exception ex) {
ex.printStackTrace();
break;
}
}
}
}
client.close();
} catch (Exception ex) {
ex.printStackTrace();
} finally {
System.out.println("client " + i++ + " is ending");
}
}
}
AS客户端
package {
import flash.events.*;
import flash.net.Socket;
import flash.display.Sprite
public class JavaAndAs3Socket extends Sprite{
private var socket:Socket;
public function JavaAndAs3Socket() {
socket=new Socket ;
socket.addEventListener(Event.CONNECT,onConnect);//注册侦听函数
socket.connect("127.0.0.1",6666);//连接本地服务器8888端口
}
private function onConnect(e:Event):void {
trace("正在连接服务器...");
}
}
}
异常
Error #2044: 未处理的 IOErrorEvent:。 text=Error #2031: Socket 错误。
at JavaAndAs3Socket$iinit()
at SocketTest_fla::MainTimeline/SocketTest_fla::frame1()
谢谢各位~ ~. --------------------编程问答-------------------- 端口都是8888哈 这发的时候忘改回来了...
补充:Java , Java相关