高分挑错!高手快来出招啊。。。。。。

popco2 2002-05-20 05:17:44
我现在正在做一个在线交流Applitcation的client端.要求实现身份验证,以及ftp文件传输功能。交谈信息要求能够保存,且也能读出来。交谈的信息以及各动作在textArea1里显示。下面给出我现在做出来的,运行没有错,就是功能上不能实现。

1。身份验证
2。聊天记录能保存及打开
3。想添加ftp文件传输功能,望高手能在里面给添加一下


高手快给虾米指点迷津啊!!!定当高分回报!!!


//////以下程序为在JB6内调试的

//////////////////////////////////////////////////////////////////
/////////////////Application1.java

package aq;

import javax.swing.UIManager;
import java.awt.*;

/**
* <p>Title: </p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2002</p>
* <p>Company: </p>
* @author popco2
* @version 1.0
*/

public class Application1 {
boolean packFrame = false;

//Construct the application
public Application1() {
Frame1 frame = new Frame1();
//Validate frames that have preset sizes
//Pack frames that have useful preferred size info, e.g. from their layout
if (packFrame) {
frame.pack();
}
else {
frame.validate();
}
//Center the window
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
Dimension frameSize = frame.getSize();
if (frameSize.height > screenSize.height) {
frameSize.height = screenSize.height;
}
if (frameSize.width > screenSize.width) {
frameSize.width = screenSize.width;
}
frame.setLocation((screenSize.width - frameSize.width) / 2, (screenSize.height - frameSize.height) / 2);
frame.setVisible(true);
}
//Main method
public static void main(String[] args) {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
}
catch(Exception e) {
e.printStackTrace();
}
new Application1();
}
}
...全文
32 19 打赏 收藏 转发到动态 举报
写回复
用AI写文章
19 条回复
切换为时间正序
请发表友善的回复…
发表回复
popco2 2002-05-28
  • 打赏
  • 举报
回复
可我手头上没多少啊,我想拿出来大家讨论讨论
高手给些经典做法,免得小虾米们到处找

希望高手们继续前进!
mygarfield 2002-05-26
  • 打赏
  • 举报
回复
wa................
hccpro 2002-05-22
  • 打赏
  • 举报
回复
server端开FTP了么??

干嘛非要FTP功能??
WQmeng 2002-05-22
  • 打赏
  • 举报
回复
用Jb跟中一下,哪块功能不对,就在那块开刀嘛。
hh_fwhy 2002-05-22
  • 打赏
  • 举报
回复
老弟,你就做了个界面呀???
你里面好多东东都还有问题呢!(用什么awt组件呀!),什么方法就大概写了个框架,你还不如直接要别人现成的程序算了!
hccpro 2002-05-22
  • 打赏
  • 举报
回复
上面:你还没写东西呢,干嘛flush()?

buff.available() == 0 就说明为空呗

一般都是

if (buff.available() > 0) do....
popco2 2002-05-22
  • 打赏
  • 举报
回复
哦,明白了,谢谢

我这里还有个帖子是关于这个application的:
就是如何向Server端申请身份验证
想要个Demo,望得到帮助,详见:
http://www.csdn.net/expert/topic/731/731909.xml?temp=.6492426

我给出的代码中有:
void save_actionPerformed(ActionEvent e) {
try{
FileOutputStream file = new FileOutputStream("note.txt");
BufferedOutputStream buff = new BufferedOutputStream(file);
buff.flush();//////flush 有问题???????????
buff.close();
textArea1.append("AQ保存成功!\n");
}catch(IOException ex){
textArea1.append(ex.toString()+"\n");
}
}

void open_actionPerformed(ActionEvent e) {
try{
FileInputStream file = new FileInputStream("note.txt");
BufferedInputStream buff = new BufferedInputStream(file);
if(buff.available()!=-1){////当 Note.txt 为空时 应该怎么判断?
textArea1.append(buff.read()+"\nAQ读入完成!\n");
buff.close();
}
else{
textArea1.append("AQ暂时无记录\n");
}
}catch(IOException ex){
textArea1.append(ex.toString()+"\n");
}
}

/////////////////////////////////
他们是用来实现对textArea1中的聊天记录进行保存,打开的,麻烦看看有没有错
能不能实现保存,打开?望给予修正!

Thanx!
hccpro 2002-05-22
  • 打赏
  • 举报
回复
格式无需知道,

按二进制的InputStream/OutputStream操作就没问题。
popco2 2002-05-22
  • 打赏
  • 举报
回复
在我的application中A端怎么打开收到的文件?
1。文件格式已知
2。文件格式未知
hccpro 2002-05-22
  • 打赏
  • 举报
回复
你要先有一个自定义的通讯协议,

比如A要发文件给B,则在现有的连接中发一个指令,比如:

SendFile from B

A接到后,就准备好接收:

ServerSocket ss = new ServerSocket(自定义一个专用文件接收的端口);
Socket socket = ss.accept();
BufferedInputStream remoteFile = new BufferedInputStream(socket.getInputStream());
BufferedOutputStream saveFile = new BufferedOutputStream(new FileOutputStream(指定存放文件的路径和文件名));

byte[] buffer = new byte[1024*128];
int b;
while ((b=remoteFile.read(buffer)) != -1)
{
saveFile.write(buffer, 0, b);
}

remoteFile.close();
saveFile.close();
socket.close();
ss.close();


在B端发文件,则要:

Socket socket = new Socket(对方IP地址,文件传输端口);
BufferedInputStream localFile = new BufferedInputStream(new FileInputStream(文件路径和文件名));
BufferedOutputStream sendFile = new BufferedOutputStream(socket.getOutputStream());

byte[] buffer = new byte[1024*128];
int b;
while ((b=localFile.read(buffer)) != -1)
{
sendFile.write(buffer, 0, b);
}

localFile.close();
sendFile.close();
socket.close();
popco2 2002-05-22
  • 打赏
  • 举报
回复
是有点吃撑了,胃口好大:)

我想要个像QQ里的传送文件那种方式,有文件选择。

要是socket的InputStream/OutputStream具体怎么实现,给个Demo
如能实现,高分回报
hccpro 2002-05-22
  • 打赏
  • 举报
回复
你真是吃饱了撑的,呵呵。

要交换文件直接用socket的InputStream/OutputStream就行了。

用FTP干嘛?还要研究FTP协议!!!!

传输文件不是都用FTP的!!!!
popco2 2002-05-22
  • 打赏
  • 举报
回复
其实我的client只是一个Demo,server端不存在
想在线交流时免不了要交换文件,就需要Ftp了,我知道加个Ftp功能是比较麻烦
但努力做吧,争取能实现
wyalchemy 2002-05-21
  • 打赏
  • 举报
回复
你最好先自己仔细分析一下你的代码,着重指出一些地方供大家帮助解决,否则,大家看到上面那么多就没什么兴趣了(不排除有以外者!),而且,回你帖子的人真的没有必要再花时间去弄明白你那些与问题无关的部分,你肯定要自己熟悉些,所以还是先自己先仔细看看再问!
shining_ljr 2002-05-21
  • 打赏
  • 举报
回复
老兄,要人帮你写代码也不是这样子的吧
popco2 2002-05-21
  • 打赏
  • 举报
回复
大哥,要不是着急,咱也不会这么长篇累牍了 :(
alphazhao 2002-05-20
  • 打赏
  • 举报
回复
太长了……
popco2 2002-05-20
  • 打赏
  • 举报
回复
//接上

menuBar1.add(menu1);
menuBar1.add(menu2);
menuBar1.add(menu3);
menu1.add(menuItem1);
menu1.add(menuItem2);
menu1.add(menuItem3);
menu1.addSeparator();
menu1.add(menuItem4);
menu2.add(menuItem5);
menu2.add(menuItem6);
menu3.add(menuItem7);
menu3.add(menuItem8);
setMenuBar(menuBar1);
}
//Overridden so we can exit when window is closed
protected void processWindowEvent(WindowEvent e) {
super.processWindowEvent(e);
if (e.getID() == WindowEvent.WINDOW_CLOSING) {
quit();
}
}

void link_actionPerformed(ActionEvent e) {
Thread thread = new Thread(this);
thread.start();
}

void send_actionPerformed(ActionEvent e) {
sout.println(textField1.getText());
sout.flush();
textArea1.append("客户端消息:"+textField1.getText()+"\n");
textField1.setText("");
}

void quit_actionPerformed(ActionEvent e) {
quit();
}
public void run() {
/**@todo: Implement this java.lang.Runnable method*/
try{
socket = new Socket("LocalHost",1124);//这两个参数可以从用户定义的地方获取
sin = new BufferedReader(new InputStreamReader(socket.getInputStream()));
sout = new PrintWriter(socket.getOutputStream());
send.setEnabled(true);
Receiver r = new Receiver();
Thread t = new Thread(r);
t.start();
textArea1.append("恭喜!成功连接到老师的服务器!\n");
link.setEnabled(false);
}catch(Exception e){
textArea1.append(e.toString()+"\n");
}
}

private class Receiver implements Runnable{
public void run(){
String msg = null;
try{
msg = sin.readLine();
while(msg!="ServerExits!"){
textArea1.append("服务器消息:"+msg+"\n");
msg = sin.readLine();
}
sin.close();
sout.close();
socket.close();
}catch(Exception e){}
send.setEnabled(false); //服务器不在服务中,禁止消息发送
}
}

void quit(){
try{
sout.println("客户端退出!");
sout.flush();
}catch(Exception e){}
finally{
System.exit(0);
}
}

void save_actionPerformed(ActionEvent e) {
try{
FileOutputStream file = new FileOutputStream("note.txt");
BufferedOutputStream buff = new BufferedOutputStream(file);
buff.flush();//////flush 有问题???????????
buff.close();
textArea1.append("AQ保存成功!\n");
}catch(IOException ex){
textArea1.append(ex.toString()+"\n");
}
}

void open_actionPerformed(ActionEvent e) {
try{
FileInputStream file = new FileInputStream("note.txt");
BufferedInputStream buff = new BufferedInputStream(file);
if(buff.available()!=-1){////当 Note.txt 为空时 available?
textArea1.append(buff.read()+"\nAQ读入完成!\n");
buff.close();
}
else{
textArea1.append("AQ暂时无记录\n");
}
}catch(IOException ex){
textArea1.append(ex.toString()+"\n");
}
}

}
popco2 2002-05-20
  • 打赏
  • 举报
回复
////////////////Frame1.java
package aq;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import com.borland.jbcl.layout.*;
import java.net.*;
import java.io.*;
import java.lang.String;

/**
* <p>Title: </p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2002</p>
* <p>Company: </p>
* @author popco2
* @version 1.0
*/

public class Frame1 extends JFrame implements Runnable {
JPanel contentPane;
Panel panel1 = new Panel();
TextField textField1 = new TextField();
Button send = new Button();
Button quit = new Button();
Panel panel2 = new Panel();
Label label1 = new Label();
TextField name = new TextField();
Label label2 = new Label();
Button link = new Button();
Button relink = new Button();
Button save = new Button();
Socket socket = null;
BufferedReader sin = null;
PrintWriter sout = null;
TextArea textArea1 = new TextArea();
JPasswordField password = new JPasswordField();
MenuBar menuBar1 = new MenuBar();
Menu menu1 = new Menu();
MenuItem menuItem1 = new MenuItem();
MenuItem menuItem2 = new MenuItem();
MenuItem menuItem3 = new MenuItem();
MenuItem menuItem4 = new MenuItem();
Menu menu2 = new Menu();
MenuItem menuItem5 = new MenuItem();
MenuItem menuItem6 = new MenuItem();
Menu menu3 = new Menu();
MenuItem menuItem7 = new MenuItem();
MenuItem menuItem8 = new MenuItem();
Button open = new Button();
XYLayout xYLayout1 = new XYLayout();
Label label3 = new Label();
Label label4 = new Label();
TextField hostAddr = new TextField();
TextField port = new TextField();
GridBagLayout gridBagLayout1 = new GridBagLayout();
GridBagLayout gridBagLayout2 = new GridBagLayout();

//Construct the frame
public Frame1() {
enableEvents(AWTEvent.WINDOW_EVENT_MASK);
try {
jbInit();
}
catch(Exception e) {
e.printStackTrace();
}
}
//Component initialization
private void jbInit() throws Exception {
//setIconImage(Toolkit.getDefaultToolkit().createImage(Frame1.class.getResource("[Your Icon]")));
contentPane = (JPanel) this.getContentPane();
contentPane.setLayout(xYLayout1);
this.setSize(new Dimension(469, 251));
this.setTitle("答疑程式——客户端");
textField1.setColumns(26);
send.setLabel("发送");
send.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
send_actionPerformed(e);
}
});
quit.setLabel("退出");
quit.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
quit_actionPerformed(e);
}
});
label1.setText("学生");
name.setColumns(8);
label2.setText("口令");
link.setActionCommand("登录");
link.setLabel("登录");
link.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
link_actionPerformed(e);
}
});
relink.setLabel("注销");
save.setLabel("保存");
save.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
save_actionPerformed(e);
}
});
password.setColumns(8);
menu1.setLabel("动作");
menuItem1.setLabel("发送");
menuItem2.setLabel("保存");
menuItem3.setLabel("打开");
menuItem4.setLabel("退出");
menu2.setLabel("状态");
menuItem5.setLabel("登录");
menuItem6.setLabel("注销");
menu3.setLabel("帮助");
menuItem7.setLabel("动画帮助");
menuItem8.setLabel("关于AQ");
textArea1.setEditable(false);
open.setLabel("打开");
open.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
open_actionPerformed(e);
}
});
label3.setText("AQ服务器地址");
label4.setText("端口");
hostAddr.setColumns(4);
port.setColumns(15);
panel1.setLayout(gridBagLayout2);
panel2.setLayout(gridBagLayout1);
contentPane.setBackground(new Color(212, 208, 200));
panel1.add(textField1, new GridBagConstraints(0, 0, 4, 1, 1.0, 0.0
,GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(3, 5, 0, 8), 362, 0));
panel1.add(quit, new GridBagConstraints(4, 1, 1, 1, 0.0, 0.0
,GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0, 0, 3, 12), 0, 0));
panel1.add(label3, new GridBagConstraints(0, 1, 1, 1, 0.0, 0.0
,GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(9, 17, 9, 0), 0, 0));
panel1.add(port, new GridBagConstraints(1, 1, 1, 1, 1.0, 0.0
,GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(8, 0, 3, 0), 165, 0));
panel1.add(label4, new GridBagConstraints(2, 1, 1, 1, 0.0, 0.0
,GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(11, 8, 3, 0), 0, 0));
panel1.add(hostAddr, new GridBagConstraints(3, 1, 1, 1, 1.0, 0.0
,GridBagConstraints.SOUTH, GridBagConstraints.NONE, new Insets(6, 0, 6, 10), 47, 0));
panel1.add(send, new GridBagConstraints(4, 0, 1, 1, 0.0, 0.0
,GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(3, 1, 0, 17), 0, 0));
panel2.add(label1, new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0
,GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(5, 9, 0, 0), 0, 0));
panel2.add(name, new GridBagConstraints(1, 0, 2, 1, 1.0, 0.0
,GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(5, 0, 0, 9), 0, 0));
panel2.add(label2, new GridBagConstraints(0, 1, 1, 1, 0.0, 0.0
,GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(9, 9, 0, 0), 0, 0));
panel2.add(password, new GridBagConstraints(1, 1, 2, 1, 1.0, 0.0
,GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(7, 0, 0, 9), 0, 0));
panel2.add(link, new GridBagConstraints(0, 2, 2, 1, 0.0, 0.0
,GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(7, 9, 0, 0), 0, 0));
panel2.add(relink, new GridBagConstraints(2, 2, 1, 1, 0.0, 0.0
,GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(7, 0, 0, 9), 0, 0));
panel2.add(save, new GridBagConstraints(0, 3, 2, 1, 0.0, 0.0
,GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0, 9, 12, 0), 0, 0));
panel2.add(open, new GridBagConstraints(2, 3, 1, 1, 0.0, 0.0
,GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0, 0, 12, 9), 0, 0));
contentPane.add(panel1, new XYConstraints(3, 135, 470, 66));
contentPane.add(textArea1, new XYConstraints(0, 0, 332, 133));
contentPane.add(panel2, new XYConstraints(330, 4, 141, 134));

62,616

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧