所以我只是看到我是否可以建立一个死的简单聊天应用程序,我面临一个问题...我的电脑上运行的服务器和手机上的客户端,一旦客户端连接到我的电脑我然后可以去我的电脑,写我想要的消息,他们将出现在我的手机(客户端),但当我尝试从我的手机发送消息时,它只会发送一条消息,然后不再有任何想法?
服务器
static OutputStream out;
static InputStream in;
String name = "Server";
public static void main(String args[]){
System.out.println("Server On");
//Server
new Thread(new Runnable(){
@Override
public void run(){
new Main().start();
}}).start();
//Scanner for text input
new Main().scrnn();
}
public void scrnn(){
Scanner lol = new Scanner(System.in);
if(lol.hasNext()){
try {
out.write((name + ": " +lol.nextLine().toString()).getBytes());
out.flush();
} catch (IOException e) {
e.printStackTrace();
}
}
scrnn();
}
public void start(){
try{
ServerSocket sok = new ServerSocket(42069);
while(true){
Socket sk = sok.accept();
in = sk.getInputStream();
out = sk.getOutputStream();
byte[] buffer = new byte[1024];
in.read(buffer);
String recievedData = new String(buffer);
System.out.println(recievedData);
out.write(recievedData.getBytes());
}
}catch(Exception e){
e.printStackTrace();
}
}
和客户端:(安卓手机)
OutputStream out;
InputStream in;
Socket sk;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
list = new Object[]{"Thanks for using Monsterchat"};
Button button = (Button)findViewById(R.id.buttonSend);
listView = (ListView) findViewById(R.id.listView);
new Thread(new Runnable() {
@Override
public void run() {
try {
sk = new Socket("10.0.0.9", 42069);
sk.setKeepAlive(true);
out = sk.getOutputStream();
in = sk.getInputStream();
//This will send fine but if you look at the button onclick it wont send that :(
out.write((name + " Joined the group!").getBytes());
//This is how i can send constant messages from the server though
listenForData();
}catch(Exception e){
e.printStackTrace();
}
}
}).start();
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new Thread(new Runnable() {
@Override
public void run() {
try {
out.write("succ".getBytes());
} catch (Exception e) {
e.printStackTrace();
}
}
}).start();
}
});
}
public void listenForData() throws Exception{
byte[] buffer = new byte[1024];
in.read(buffer);
final String recievedData = new String(buffer);
runOnUiThread(new Runnable() {
@Override
public void run() {
//Update listView with new message
buildList(recievedData);
}
});
//Repeat
listenForData();
}
很抱歉这里有很多代码,而且看起来不对的logcat也没有输出
答案 0 :(得分:0)
while(true){
Socket sk = sok.accept();
in = sk.getInputStream();
out = sk.getOutputStream();
Dr byte[] buffer = new byte[1024];
in.read(buffer);
String recievedData = new String(buffer);
System.out.println(recievedData);
out.write(recievedData.getBytes());
那个循环是错误的。收到一条消息后,服务器将丢弃sk
并等待新客户端。
你最好试试;
Socket sk = sok.accept();
in = sk.getInputStream();
out = sk.getOutputStream();
byte[] buffer = new byte[1024];
while(true){
int nread = in.read(buffer);
String recievedData = new String(buffer, 0, nread);
System.out.println(recievedData);
out.write(recievedData.getBytes());