java socket服务器接收不到客户端发来的消息

package socket;

public class MySeverSocket {

public static void main(String[] args) {
    new ServerListener().start();
}

}


package socket;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;

import javax.swing.JOptionPane;

public class ServerListener extends Thread {
public void run (){
try {

ServerSocket serverSocket = new ServerSocket(55555);
while (true){
Socket socket = serverSocket.accept();
JOptionPane.showMessageDialog(null, "有客户端连接到了55555端口");
ChatSocket cs = new ChatSocket(socket);
cs.start();
ChatManager.getChatManager().add(cs);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}


package socket;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.Socket;

public class ChatSocket extends Thread {

Socket socket;
public ChatSocket(Socket s) {
    this.socket = s;
}

public void  out(String out) {
    try {
        socket.getOutputStream().write(out.getBytes("UTF-8"));
    } catch (UnsupportedEncodingException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

@Override
public void run() {
    try {
        BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream(), "UTF-8"));
        String line = null;
        while ((line = br.readLine()) != null) {
            ChatManager.getChatManager().publich(this, line);
        }
        br.close();
    } catch (UnsupportedEncodingException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

}


package socket;

import java.util.Vector;

public class ChatManager {
private ChatManager() {}
private static final ChatManager cm = new ChatManager();
public static ChatManager getChatManager() {
return cm;
}

Vector<ChatSocket> vector = new Vector<ChatSocket>();

public void  add(ChatSocket cs) {
    vector.add(cs);
}

public void publich(ChatSocket cs,String out) {
    for(int i = 0;i<vector.size();i++){
        ChatSocket csChatSocket = vector.get(i);
        if (!cs.equals(csChatSocket)) {
            csChatSocket.out(out);
        }
    }
}

}

安卓客户端连接与发送消息方法

public void connect(){
final AsyncTask read = new AsyncTask() {
String iptext = ip.getText().toString();
@Override
protected Void doInBackground(Void... params) {
try {
socket = new Socket(iptext,55555);
writer = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
publishProgress("@success");
} catch (IOException e) {
Toast.makeText(MainActivity.this,"连接失败",Toast.LENGTH_SHORT).show();
e.printStackTrace();
}

            try {
                String line;
                while ((line = reader.readLine()) != null){
                    publishProgress(line);
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
            return null;
        }

        @Override
        protected void onProgressUpdate(String... values) {
            if (values[0].equals("@success")){
                Toast.makeText(MainActivity.this,"连接成功",Toast.LENGTH_SHORT).show();
            }
            text.append("别人说"+values[0]+"\n");
            super.onProgressUpdate(values);
        }
    };
    read.execute();
}

public void send(){
    try {
       // text.append("我说:"+editText.getText().toString()+"\n");
        writer.write(editText.getText().toString()+"\n");
        writer.flush();
        editText.setText("");
    } catch (IOException e) {
        e.printStackTrace();
    }
}

4个回答

有没有报错日志,可能网络都不通

收不到客户端发来的消息,总有个问题情况描述吧,先描述下情况

就是服务器用终端相互发消息是可以的,用app时就不显示对方发来的消息

按理来说 两个客户端发消息的话 会显示出来的

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问