用SOCKET通讯,客户端发送的数据传到服务器,但服务器接收的数据没有更新(第一次成功的把数据发送过去并处理了,但以后每次“发送”,服务器显示的都是第一次的数据。)
应该是输入输出流的问题。但不知道应该怎么修改,请教高手指点!
public class ServerTcp extends Thread {
public ServerTcp() {
this.start();
}
public void run(){
new Conn(client,id,sf);
}
}class Conn extends Thread{
public Conn(Socket client_socket,int id,ServerFrame sf){
client = client_socket;
this.id = id;
this.sf = sf;
try{
in = new BufferedReader(new InputStreamReader(client.getInputStream()));
client.getInputStream().read(receive_data);//把客户端传过来的数据读到一个字节数组里面 out = new PrintStream(client.getOutputStream());
out.println("Welcome......");
out.flush(); }catch(IOException ioe){
try{
client.close();
}catch(IOException ioe2){
System.out.print("Close client error");
return;
}
}
this.start();
}
public void run(){
try{
for(;;){
String s = in.readLine();
if(s == null){
sf.ChatArea.append("终端"+id+"退出\n");
return;
}
//sf.ChatArea.append("终端"+id+":"+s+"\n");
handle_data(receive_data);//这个是处理数据的
out.println("服务器成功接收:"+s+"\n");
out.flush();
}
}catch(IOException ioe){
try{
in.close();
out.close();
client.close();
sf.ChatArea.append("终端" + id + "退出\n");
return;
}catch(IOException ioe2){
ioe2.printStackTrace();
}
}
}
}
应该是输入输出流的问题。但不知道应该怎么修改,请教高手指点!
public class ServerTcp extends Thread {
public ServerTcp() {
this.start();
}
public void run(){
new Conn(client,id,sf);
}
}class Conn extends Thread{
public Conn(Socket client_socket,int id,ServerFrame sf){
client = client_socket;
this.id = id;
this.sf = sf;
try{
in = new BufferedReader(new InputStreamReader(client.getInputStream()));
client.getInputStream().read(receive_data);//把客户端传过来的数据读到一个字节数组里面 out = new PrintStream(client.getOutputStream());
out.println("Welcome......");
out.flush(); }catch(IOException ioe){
try{
client.close();
}catch(IOException ioe2){
System.out.print("Close client error");
return;
}
}
this.start();
}
public void run(){
try{
for(;;){
String s = in.readLine();
if(s == null){
sf.ChatArea.append("终端"+id+"退出\n");
return;
}
//sf.ChatArea.append("终端"+id+":"+s+"\n");
handle_data(receive_data);//这个是处理数据的
out.println("服务器成功接收:"+s+"\n");
out.flush();
}
}catch(IOException ioe){
try{
in.close();
out.close();
client.close();
sf.ChatArea.append("终端" + id + "退出\n");
return;
}catch(IOException ioe2){
ioe2.printStackTrace();
}
}
}
}
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货