发垃圾数据,能发,说明通的,异常说明~o~.................zzz
解决方案 »
- 返回自 1970年1月1日00:00:00 GMT以来对象表示的毫秒数??
- 如何实现对从键盘输入的一组数据进行排序?
- 请教关于使用InputStream下载文件的问题
- 谁做过用javamail查看收件箱的程序?
- 请教有关java web start问题
- ◆◆JBuilder环境下,怎样单步执行代码?◆◆
- JFrame问题:如何在窗体一打开就处于最大化状态
- 我下载的oracle的jdbc驱动,解压缩后包含两个子目录javax和oracle,并没有包含任何*.jar,我下载的对不对?如果对的话,
- web.xml中servlet匹配的先后问题
- 有没有大神告诉我一下jsonlib的优缺点
- 一个基本的问题。(很容易给分的哟)
- 一个网页的小问题
{
s.getInputStream();
}catch(Exception e)
{
//socket is closed.
}
当你不断的read的时候,server断,exception就catch到了,没有问题的.
怎么和老兄说的都不一样啊?
public void run(){
while (Action !=0){
try{
is = socket.getInputStream();
ir = new InputStreamReader(is);
AppServer.sysinfo.HNMState = "正常!!";
char str[] = new char[1024];
ir.read(str); //平时在这里等待,但是SERVER一断就立即会向下运行。
String Packet = new String(str);
if ( Packet.trim().length()>0) {
// AppServer.log.showmessage(Packet,0);
UnPack(Packet);
}
else{
Integer ee = new Integer(Packet.trim().length());
AppServer.sysinfo.HNMState = "断开!!";
break;
}
}catch (Exception err){
AppServer.log.showmessage("解包错误",0); //没有出现过EXCEPTION
}
} }
程序其他没有什么问题.
把getInputStream放到循环外面效率高点.
import java.io.*;
public class SimpleClient {
public static void main(String args[])
throws IOException {
int c;
Socket s1;
InputStream s1In;
DataInputStream dis;
// Open your connection to a server, at port 5432
// localhost used here
s1 = new Socket("192.111.111.2",5432);
//set up the time out of read operation = 100s, if occured assume server dead.
s1.setSoTimeout(10000);
// Get an input file handle from the socket and
// read the input
s1In = s1.getInputStream();
dis = new DataInputStream(s1In); while (true){
try {
String st = new String (dis.readUTF());
System.out.println(st);
}catch (InterruptedIOException ee){
System.out.println("Time out , Maybe Server dead or power down casually");
break;
}catch (EOFException ee){
System.out.println("End of Stream");
break;
}catch (IOException e){
System.out.println("IOException, May be server closed");
break;
}
}
// When done, just close the connection and exit
dis.close();
s1In.close();
s1.close();
}
}
import java.io.*;public class SimpleServer {
public static void main(String args[]) {
ServerSocket s = null;
Socket s1;
String sendString = "Hello Net World!";
int slength = sendString.length();
OutputStream s1out;
DataOutputStream dos; // Register your service on port 5432
try {
s = new ServerSocket(5432);
} catch (IOException e) {
// ignore
}
// Run the listen/accept loop forever
int count=0;
while (true) {
try {
// Wait here and listen for a connection
s1=s.accept(); // Get a communication stream associated with
// the socket
s1out = s1.getOutputStream();
dos = new DataOutputStream (s1out); // Send your string!
// (UTF provides machine independence)
while (System.in.available()==0){
dos.writeUTF(sendString);
}
// clear key board input
while (System.in.available()!=0){
System.in.read();
} System.out.println("SocketClosed "+ count++);
// Close the connection, but not the server socket
dos.close();
s1out.close();
s1.close();
} catch (IOException e) {
// ignore
}
}
}
}
当再server端按 enter键正常close掉socket,
按ctrl+c相当于结束server,
直接关掉运行server的电源会引起client的等待,。
则在client端分别引起EOFExceptin
IOException 以区别这两种情况,
等待10秒钟后就会因超时退出。
这样应该能解决你的问题了吧,别忘了给加份哦