我对编码很新,特别是Java,所以如果我错过任何根本明显的东西,我道歉。以外行人的方式回答会有所帮助。
我正在尝试在使用android作为客户端的智能手机和使用Netbeans的笔记本电脑上的服务器之间设置客户端 - 服务器。我设法从电话到服务器进行通信,但是我需要服务器以某种形式的通信进行响应,这是我的问题所在。
我一直在使用一个给我的例子作为我的基础,双向通信适用于netbeans中的客户端和服务器(下面的代码)。我一直在尝试调整android的netbeans客户端代码以相同的方式运行,没有运气。任何帮助,将不胜感激。
这是笔记本电脑的工作服务器代码:
package myserver2;
import java.io.*;
import java.net.*;
public class MyServer2 {
int port = 8989;
public static void main(String[] args) throws Exception
{
MyServer2 SERVER = new MyServer2();
SERVER.run();
}
public void run() throws Exception
{
ServerSocket SRVSOCK = new ServerSocket(port);
SRVSOCK.setReuseAddress(true);
Socket SOCK = SRVSOCK.accept();
InputStreamReader IR = new InputStreamReader(SOCK.getInputStream());
BufferedReader BR = new BufferedReader(IR);
String MESSAGE = BR.readLine();
System.out.println(MESSAGE);
if(MESSAGE != null)
{
PrintStream PS = new PrintStream(SOCK.getOutputStream());
PS.println("MESSAGE recieved!");
}
}
}
这也是笔记本电脑上正在运行的客户端代码:
package myserver2;
import java.io.*;
import java.net.*;
public class MyClient2 {
public static void main(String[] args) throws Exception
{
MyClient2 CLIENT = new MyClient2();
CLIENT.run();
}
public void run() throws Exception
{
Socket SOCK = new Socket("192.168.0.4", 8989);
PrintStream PS = new PrintStream(SOCK.getOutputStream());
PS.println("Hello to SERVER from CLIENT.\n");
InputStreamReader IR = new InputStreamReader(SOCK.getInputStream());
BufferedReader BR = new BufferedReader(IR);
String MESSAGE = BR.readLine();
System.out.println(MESSAGE);
}
}
最后这里是没有工作的android代码:
package com.example.sebastian.myserver3;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.app.Activity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity {
private Socket client;
private PrintWriter printwriter;
private EditText etMsg, etIP, etPort;
private Button button;
private String message;
int port = 0;
private TextView responseText;
private static final String TAG = "ResponseMessages";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.i(TAG, "Just checking logs worked");
// etIP = (EditText) findViewById(R.id.editText1);
// etPort = (EditText) findViewById(R.id.editText2);
etMsg = (EditText) findViewById(R.id.editText3);
button = (Button) findViewById(R.id.button1);
responseText = (TextView) findViewById(R.id.responseText);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
message = etMsg.getText().toString();
etMsg.setText(" ");
port = Integer.parseInt("8989");
Toast.makeText(getApplicationContext(), "Message was sent", Toast.LENGTH_LONG).show();
new Thread(new Runnable() {
@Override
public void run() {
try {
client = new Socket("192.168.0.4", port);
printwriter = new PrintWriter(client.getOutputStream());
Log.i(TAG, "The message sent was " + message);
printwriter.write(message);
printwriter.flush();
printwriter.close();
InputStreamReader IR = new InputStreamReader(client.getInputStream());
BufferedReader BR = new BufferedReader(IR);
StringBuilder total = new StringBuilder();
String line;
try{
while((line = BR.readLine()) != null){
total.append(line);
responseText.setText(total);
}
}catch (IOException e){
e.printStackTrace();
}
// String MESSAGE = BR.toString();
// Log.i(TAG, "The message received was " + MESSAGE);
// Toast.makeText(getApplicationContext(), "This is also a toast!", Toast.LENGTH_LONG).show();
// responseText = (TextView) findViewById(R.id.responseText);
// responseText.setText(MESSAGE);
client.close();
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}).start();
}
});
}
}
我试图在Logcat,totast和textview中显示图像,我似乎无法实现任何目标。我认为该消息未被正确接收,因此无法显示。 因为我尝试了很多不同的东西而没有运气,所以android代码可能已经到处都是。我发布的代码就是我碰壁的地方,需要一些帮助。
感谢任何回复的人。