类型:转载 责任编辑:asp.net 日期:2007/05/23
热门软件下载:










页面导航:
正文内容:public void netcomm_init() {
net_state = 1;
try {
sock = new socket(serveraddr, serverport);
} catch (ioexception e) {
net_state = 0;
}
timer.start();
}
public void netcomm_data()
{
try {
outputstream outputstream = sock.getoutputstream();
bufferedwriter out = new bufferedwriter
(new outputstreamwriter(outputstream));
out.write("java by piky2004@126.com");
out.flush();
bufferedreader in = new bufferedreader
(new inputstreamreader(sock.getinputstream()));
boolean more = true;
while(more) {
string str = in.readline();
if(str == null) more = false;
else
// 处理数据
system.out.println(str);
}
in.close();
} catch (ioexception e) {
netcomm_close();
net_state = 0;
}
timer.start();
}
public void netcomm_close()
{
if(sock != null)
try{
sock.close();
} catch ( ioexception e) {
}
}
public void actionperformed(actionevent e)
{
if(net_state == 0)
netcomm_init();
else
netcomm_data();
}
}
在以上程序中,也可以为外部应用提供一个回调函数,以便在网络异常或恢复正常时通知应用。服务应用的网络通信类类似,可以放在同一类中。