java 端做了个简单的图形界面,实现了一对一的通话
flash 的代码如下:
lists 是显示用的textarea 实例名
input_txt 是一个输入文本的实例名
System.useCodepage=true;
lists.text = "connecting...";
var mySocket = new XMLSocket();
//
mySocket.connect("127.0.0.1", 3000);
mySocket.onConnect = function(success) {
if (success) {
lists.text = "Connect Ok!";
//mySocket.send("guest");
Selection.setFocus(input_txt);
} else {
lists.text = "connect failed";
}
};
mySocket.onData = function(s) {
var pos=s.indexOf("&");
if(pos!=-1){
s=s.split("&");
lists.text+=s[1];
}
};
mySocket.onClose=function(){
lists.text+="Server has been closed.";
}
//
bn_send.onRelease = function() {
sendMsg(input_txt.text);
};
var key = new Object();
key.onKeyDown = function() {
if (Key.isDown(Key.ENTER)) {
sendMsg(input_txt.text);
}
};
Key.addListener(key);
//
function sendMsg(str) {
mySocket.send("msg&"+str+"&msg\n");
input_txt.text = "";
Selection.setFocus(input_txt);
}
server 端:
import java.io.*;
import java.net.*;
import java.awt.*;
import javax.swing.*;
public class FlashServer {
protected int listenPort = 3000;
JPanel list;
JTextArea list_txt;
JFrame w;
JScrollPane bar;
public FlashServer(){
list=new JPanel();
//用来显示信息的文本框
list_txt=new JTextArea(8,20);
list_txt.setLineWrap(true);
list.add(list_txt);
//滚动条
int V_bar=ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED;
int h_bar=ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER;
bar=new JScrollPane(list_txt,V_bar,h_bar);
//创建窗口
w=new JFrame("walktree的Flash Server");
w.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
w.getContentPane().add(list,BorderLayout.CENTER);
w.getContentPane().add(bar);
w.setBounds(5,5,320,160);
w.setVisible(true);
}
public void acceptConnections() {
try {
ServerSocket server = new ServerSocket(this.listenPort);
Socket clientObj = null;
list_txt.append("Server 空闲中...\n");
clientObj = server.accept();
list_txt.append("一个客户端正连接过来...\n");
handleConnection(clientObj);
} catch (BindException e) {
list_txt.append("Unable to bind to port " + listenPort+"\n");
}
catch (IOException e) {
list_txt.append("Unable to instantiate a ServerSocket on port: " + listenPort+"\n");
}
}
public void handleConnection(Socket clientObj) {
list_txt.append("正在对此连接进行处理......\n");
try {
PrintWriter outObj=new PrintWriter(clientObj.getOutputStream());
InputStream inputFromSocket = clientObj.getInputStream();
BufferedReader streamReader = new BufferedReader(new InputStreamReader(inputFromSocket));
//首先要将InputStream转换到BufferedReader中
String line = streamReader.readLine();
String s=getMsg(line);
while (!s.equals("quit")) {
list_txt.append("Client : "+s+"\n");
//输出给flash
outObj.println(line+"\0");
outObj.flush();
//从streamReader中读出信息
line = streamReader.readLine();
s=getMsg(line);
}
list_txt.append("接受信息完毕!\n");
//完成之后关闭流
streamReader.close();
inputFromSocket.close();
outObj.close();
} catch (Exception e) {
list_txt.append("client is closed.\n");
}
}
private String getMsg(String s){
int start=s.indexOf("msg&");
int endpos=s.indexOf("&msg");
return s.substring(start+4,endpos);
}
//主程序,建立sever实例对象,然后运行对象的acceptConnections()方法
public static void main(String[] args) {
FlashServer server = new FlashServer();
server.acceptConnections();
}
}
flash 的代码如下:
lists 是显示用的textarea 实例名
input_txt 是一个输入文本的实例名
System.useCodepage=true;
lists.text = "connecting...";
var mySocket = new XMLSocket();
//
mySocket.connect("127.0.0.1", 3000);
mySocket.onConnect = function(success) {
if (success) {
lists.text = "Connect Ok!";
//mySocket.send("guest");
Selection.setFocus(input_txt);
} else {
lists.text = "connect failed";
}
};
mySocket.onData = function(s) {
var pos=s.indexOf("&");
if(pos!=-1){
s=s.split("&");
lists.text+=s[1];
}
};
mySocket.onClose=function(){
lists.text+="Server has been closed.";
}
//
bn_send.onRelease = function() {
sendMsg(input_txt.text);
};
var key = new Object();
key.onKeyDown = function() {
if (Key.isDown(Key.ENTER)) {
sendMsg(input_txt.text);
}
};
Key.addListener(key);
//
function sendMsg(str) {
mySocket.send("msg&"+str+"&msg\n");
input_txt.text = "";
Selection.setFocus(input_txt);
}
server 端:
import java.io.*;
import java.net.*;
import java.awt.*;
import javax.swing.*;
public class FlashServer {
protected int listenPort = 3000;
JPanel list;
JTextArea list_txt;
JFrame w;
JScrollPane bar;
public FlashServer(){
list=new JPanel();
//用来显示信息的文本框
list_txt=new JTextArea(8,20);
list_txt.setLineWrap(true);
list.add(list_txt);
//滚动条
int V_bar=ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED;
int h_bar=ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER;
bar=new JScrollPane(list_txt,V_bar,h_bar);
//创建窗口
w=new JFrame("walktree的Flash Server");
w.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
w.getContentPane().add(list,BorderLayout.CENTER);
w.getContentPane().add(bar);
w.setBounds(5,5,320,160);
w.setVisible(true);
}
public void acceptConnections() {
try {
ServerSocket server = new ServerSocket(this.listenPort);
Socket clientObj = null;
list_txt.append("Server 空闲中...\n");
clientObj = server.accept();
list_txt.append("一个客户端正连接过来...\n");
handleConnection(clientObj);
} catch (BindException e) {
list_txt.append("Unable to bind to port " + listenPort+"\n");
}
catch (IOException e) {
list_txt.append("Unable to instantiate a ServerSocket on port: " + listenPort+"\n");
}
}
public void handleConnection(Socket clientObj) {
list_txt.append("正在对此连接进行处理......\n");
try {
PrintWriter outObj=new PrintWriter(clientObj.getOutputStream());
InputStream inputFromSocket = clientObj.getInputStream();
BufferedReader streamReader = new BufferedReader(new InputStreamReader(inputFromSocket));
//首先要将InputStream转换到BufferedReader中
String line = streamReader.readLine();
String s=getMsg(line);
while (!s.equals("quit")) {
list_txt.append("Client : "+s+"\n");
//输出给flash
outObj.println(line+"\0");
outObj.flush();
//从streamReader中读出信息
line = streamReader.readLine();
s=getMsg(line);
}
list_txt.append("接受信息完毕!\n");
//完成之后关闭流
streamReader.close();
inputFromSocket.close();
outObj.close();
} catch (Exception e) {
list_txt.append("client is closed.\n");
}
}
private String getMsg(String s){
int start=s.indexOf("msg&");
int endpos=s.indexOf("&msg");
return s.substring(start+4,endpos);
}
//主程序,建立sever实例对象,然后运行对象的acceptConnections()方法
public static void main(String[] args) {
FlashServer server = new FlashServer();
server.acceptConnections();
}
}
回复Comments
{commenttime}{commentauthor}
{CommentUrl}
{commentcontent}