下面是发送消息类:public class SendData
{
Socket socket=null;
protected BufferedReader inStream=null;
protected PrintWriter outStream=null; public SendData()
{
try
{
init();
sendMsg();
}
catch(Exception e )
{
System.out.println(e.toString());
}
} void init() throws Exception
{
socket=new Socket("192.168.0.14",4385);
inStream = new BufferedReader(new InputStreamReader(this.socket.getInputStream()));
outStream = new PrintWriter(this.socket.getOutputStream(), true);
} void sendMsg()
{ String str=="0020213808685517xxxxxxxx*pang#renfenru*";
try
{ System.out.println("Client accepting msg:");
outStream.println(str);
outStream.flush();
}
catch(IOException e1)
{
System.out.println(e1.toString());
System.exit(-1);
}
} public static void main(String [] args)
{
int str=0;
SendData sendData=new SendData();
}}下面是接收消息线程:public class ReceiveMsg extends Thread //的消息,并存入buffer1
{
protected BufferedReader inStream;
protected PrintWriter outStream;
protected Socket socket; public ReceiveMsg(Socket socket) //构造函数
{
try
{
this.socket=socket;
inStream = new BufferedReader(new InputStreamReader(this.socket.getInputStream()));
outStream = new PrintWriter(this.socket.getOutputStream(), true);
}
catch(Exception e)
{
System.out.println(e.toString());
}
} public void run() //重载run方法
{
String msg=null;
try
{
while(true)
{
try
{
msg=inStream.readLine(); //读取消息
System.out.println("Accept msg:...");
}
catch(IOException e)
{
System.out.println(e.toString());
}
System.out.println("RFSMC: "+msg);
InputMsgBuffer1(msg); //讲消息存入buffer1
}
}
catch(Exception e)
{
System.out.println(e.toString());
}
} public void InputMsgBuffer1(String msg) throws Exception
//将数据放入buffer1;
{
synchronized(ServerThread.buffer1)
{
ServerThread.buffer1.addElement(msg);
}
}}当该接收消息线程启动后,发送一条消息,接收到的地一条消息为正确消息;
但是此后,接收线程仍然不停地接收消息,而显示结果接到消息为null.
请各位大侠帮帮忙。
{
Socket socket=null;
protected BufferedReader inStream=null;
protected PrintWriter outStream=null; public SendData()
{
try
{
init();
sendMsg();
}
catch(Exception e )
{
System.out.println(e.toString());
}
} void init() throws Exception
{
socket=new Socket("192.168.0.14",4385);
inStream = new BufferedReader(new InputStreamReader(this.socket.getInputStream()));
outStream = new PrintWriter(this.socket.getOutputStream(), true);
} void sendMsg()
{ String str=="0020213808685517xxxxxxxx*pang#renfenru*";
try
{ System.out.println("Client accepting msg:");
outStream.println(str);
outStream.flush();
}
catch(IOException e1)
{
System.out.println(e1.toString());
System.exit(-1);
}
} public static void main(String [] args)
{
int str=0;
SendData sendData=new SendData();
}}下面是接收消息线程:public class ReceiveMsg extends Thread //的消息,并存入buffer1
{
protected BufferedReader inStream;
protected PrintWriter outStream;
protected Socket socket; public ReceiveMsg(Socket socket) //构造函数
{
try
{
this.socket=socket;
inStream = new BufferedReader(new InputStreamReader(this.socket.getInputStream()));
outStream = new PrintWriter(this.socket.getOutputStream(), true);
}
catch(Exception e)
{
System.out.println(e.toString());
}
} public void run() //重载run方法
{
String msg=null;
try
{
while(true)
{
try
{
msg=inStream.readLine(); //读取消息
System.out.println("Accept msg:...");
}
catch(IOException e)
{
System.out.println(e.toString());
}
System.out.println("RFSMC: "+msg);
InputMsgBuffer1(msg); //讲消息存入buffer1
}
}
catch(Exception e)
{
System.out.println(e.toString());
}
} public void InputMsgBuffer1(String msg) throws Exception
//将数据放入buffer1;
{
synchronized(ServerThread.buffer1)
{
ServerThread.buffer1.addElement(msg);
}
}}当该接收消息线程启动后,发送一条消息,接收到的地一条消息为正确消息;
但是此后,接收线程仍然不停地接收消息,而显示结果接到消息为null.
请各位大侠帮帮忙。
前面问题正如zzw_happy兄所说,是因为客户已经退出,但是我还是不明白,
客户退出为什么不抛出错误,而是不停地接收null消息呢?又应该如何解决这个问题呢?请大家赐教。