| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 479 人关注过本帖
标题:线程中的问题
只看楼主 加入收藏
狂飙的蜗牛
Rank: 1
等 级:新手上路
帖 子:174
专家分:0
注 册:2007-1-23
收藏
 问题点数:0 回复次数:4 
线程中的问题

import java.util.Vector;
import java.net.Socket;
import java.net.UnknownHostException;
import java.io.IOException;
import sun.net.ftp.FtpClient;
import java.io.InputStream;
import java.awt.Color;
import java.io.BufferedReader;
import java.util.HashMap;


public class ConnectTest {

Vector threadVector = new Vector();
Thread t1;

StringBuffer strAllInfo;//得到所有的错误信息

ConnectTest(Vector vector, boolean bUseMail, boolean bUseSound,
boolean bUseMsg, String mailADD, String mailPWD) {

for (int i = 0; i < vector.size(); i++) {
t1 = new Thread(new ThreadFtpConn((String) vector.get(i), bUseMail,
bUseSound, bUseMsg, mailADD,
mailPWD));
t1.start();
threadVector.addElement(t1);

}
}

public void stop() {
Thread t1;
for (int j = 0; j < threadVector.size(); j++) {
t1 = (Thread) threadVector.get(j);
t1.stop();
}
System.out.println("Ftp线程已停止");
}

public static void main(String[] args) {
// ConnectTest connecttest = new ConnectTest();ghfghfg
Vector vector = new Vector();
vector.addElement("10.2.0.4:21:5");
vector.addElement("10.1.9.6:21:4");
vector.addElement("10.1.9.100:21:5");
vector.addElement("10.1.9.4:21:7");
ConnectTest CT = new ConnectTest(vector, true, true, true, "aa@", "PWD");
}
}


class ThreadFtpConn implements Runnable {
PopFrameDemo ftPopFrame = new PopFrameDemo(); //提示框
//static StringBuffer strInfo;

private String strFtpIpPort = "";
private String strFtpIp = "";
private int intFtpPort = 0;
private int intFtpSleep = 0;

private boolean msgAlarm = false;
private boolean mailAlarm = false;
private boolean soundAlarm = false;
private String mailADD = "";
private String mailPWD = "";

ThreadFtpConn(String strFtpIpPort, boolean bUseMail, boolean bUseSound,
boolean bUseMsg, String mailADD, String mailPWD) {
this.strFtpIpPort = strFtpIpPort;
this.setFtpIp(strFtpIpPort);
this.setFtpPort(strFtpIpPort);
this.setFtpSleep(strFtpIpPort);
this.mailAlarm = bUseMail;
this.msgAlarm = bUseMsg;
this.soundAlarm = bUseSound;
this.mailADD = mailADD;
this.mailPWD = mailPWD;
System.out.println(mailAlarm);
System.out.println(msgAlarm);
System.out.println(soundAlarm);
System.out.println(mailADD);
System.out.println(mailPWD);
}

/*
*
*
*/
private void setFtpIp(String strFtpIpPort) {

int intFirstPosition = strFtpIpPort.indexOf(":");
String strFtpIp = strFtpIpPort.substring(0, intFirstPosition);
this.strFtpIp = strFtpIp;
}

/*
*
*
*/
private void setFtpPort(String strFtpIpPort) {
int intFirstPosition = strFtpIpPort.indexOf(":");
int intLastPosition = strFtpIpPort.lastIndexOf(":");
int intFtpPort = Integer.parseInt(strFtpIpPort.substring(
intFirstPosition + 1,
intLastPosition));
this.intFtpPort = intFtpPort;
}

/*
*
*
*/
private void setFtpSleep(String strFtpIpPort) {
int intLastPosition = strFtpIpPort.lastIndexOf(":");
String strFtpSleep = strFtpIpPort.substring(intLastPosition + 1);
int intFtpSleep = Integer.parseInt(strFtpSleep);
this.intFtpSleep = intFtpSleep;

}

/*
*
*
*/
private boolean IsConnect(String ip, int port) {
String strInfo="";
try {
Socket socket = new Socket(ip, port);
return true;
} catch (UnknownHostException e) {
System.out.println("错误的IP地址");
} catch (IOException e) {
strInfo="不能通过端口 " + port + " 与主机 " + ip + " 进行FTP连接";
System.out.println(strInfo);


}
return false;
}

private boolean IsFtpConnect(String ip) {
FtpClient ftpclient = new FtpClient();

try {
ftpclient.openServer(ip);
ftpclient.login("anonymous", "anonymous");
InputStream is = ftpclient.list();
StringBuffer info = new StringBuffer();
int ch;
while ((ch = is.read()) >= 0) {
info.append((char) ch);
}
//System.out.println(new String(info));
is.close();
return true;
} catch (IOException e) {
System.out.println();
System.out.println("主机 " + ip + " FTP服务没有开启或者出现异常");
}

return false;
}


public void run() {
while (true) {
if (this.IsConnect(strFtpIp, intFtpPort)) {
if (this.IsFtpConnect(strFtpIp)) {
System.out.println("ftp " + strFtpIp + " 正常运行");
} else {
System.out.println("ftp " + strFtpIp + " no connect");
}
} else {

// System.out.println("connect error!");

}
try {
Thread.sleep(intFtpSleep * 1000);
} catch (InterruptedException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
}

}

}
}



问题:我想用strAllInfo保存所有线程的strInfo的值,该怎么做..

求求各位了.帮忙看看该怎么做.实在想不出办法了..谢谢啦.

搜索更多相关主题的帖子: 线程 
2007-05-18 22:44
a276202460
Rank: 2
等 级:新手上路
威 望:4
帖 子:392
专家分:1
注 册:2007-4-10
收藏
得分:0 
啊刚回复了没保存 没给发出去
如果想存储的是strInfo="不能通过端口 " + port + " 与主机 " + ip + " 进行FTP连接";

可以用Vector 也可以用LinkList 定义LinkList<Thread> listthread存储t1;定义LinkList<String> liststring存储strInfo ;
在这段代码
t1 = new Thread(new ThreadFtpConn((String) vector.get(i), bUseMail,
bUseSound, bUseMsg, mailADD,
mailPWD));
t1.start();
加上listthread.add(t1);
liststring.add(strInfo);
一一对应;
如果正常会把上一次线程错误信息存入 要在run的try{
语句中加如strInfof="true";
在要输出str
Info的时候区别 不输出来
先get线程当前的index然后输出liststring.(i);
我不能肯定好使不好使 楼主试试吧先

2007-05-18 23:16
狂飙的蜗牛
Rank: 1
等 级:新手上路
帖 子:174
专家分:0
注 册:2007-1-23
收藏
得分:0 

十分感谢楼上的回复.我去试试.

2007-05-19 06:44
狂飙的蜗牛
Rank: 1
等 级:新手上路
帖 子:174
专家分:0
注 册:2007-1-23
收藏
得分:0 
再次感谢2楼的.我用其他方法解决了.谢谢你的帮助..
2007-05-19 17:48
a276202460
Rank: 2
等 级:新手上路
威 望:4
帖 子:392
专家分:1
注 册:2007-4-10
收藏
得分:0 

呵呵 不会是用的BufferString吧 那个是按字节存储的


2007-05-19 22:07
快速回复:线程中的问题
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.053287 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved