我的笔记本电脑上运行了一个简单的Java服务器,可以打开ServerSocket
。 Android的一个简单客户端尝试连接到此服务器(使用我的笔记本电脑的IP地址和我在ServerSocket
中指定的端口),并发送一个字符串。客户端挂起:
client = new Socket(IP_ADDRESS, DEST_PORT);
我正在使用连接到笔记本电脑的三星Galaxy S3。请注意,我尝试在AsyncTask
中从客户端建立连接。
我一直在捣乱这一段时间,感觉我错过了一些简单的事情。
所以问题是:如何让我的Android手机识别/看到电脑?提前谢谢!
这是我的Java服务器代码:
//导入等
public class Main
{
private static final int PORT = 4444;
private static ServerSocket serverSocket;
private static Socket clientSocket;
private static InputStreamReader inputStreamReader;
private static BufferedReader bufferedReader;
private static String message;
public static void main(String[] args)
{
try
{
serverSocket = new ServerSocket(PORT, 0, InetAddress.getLocalHost());
System.out.println("IP: " + serverSocket.getInetAddress() + " Port: " + serverSocket.getLocalPort());
} catch (IOException e)
{
System.out.println("Could not listen on port: 4444");
}
System.out.println("Server started. Listening to the port 4444");
while (true)
{
try
{
clientSocket = serverSocket.accept(); // accept the client connection
inputStreamReader = new InputStreamReader(clientSocket.getInputStream());
bufferedReader = new BufferedReader(inputStreamReader); // get the client message
message = bufferedReader.readLine();
System.out.println(message);
inputStreamReader.close();
clientSocket.close();
} catch (IOException ex)
{
System.out.println("Problem in message reading");
}
}
}
}
这是我的Android客户端代码:
//导入等
public class SimpleClientActivity extends Activity
{
private EditText textField;
private Button button;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
textField = (EditText) findViewById(R.id.editText1); // reference to the text field
button = (Button) findViewById(R.id.button1); // reference to the send button
// Button press event listener
button.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
new ConnectToServerTask().execute(textField);
}
});
}
}
这是我的AsyncTask代码:
//导入等
public class ConnectToServerTask extends AsyncTask<View, Integer, Socket>
{
private static final String IP_ADDRESS = "192.168.56.1"; // Toshiba laptop
private static final int DEST_PORT = 4444;
private EditText mTextField;
/**
* Store provided views (used later in onPostExecute(...)).
*
* Create socket to communicate with server (blocking call).
*/
protected Socket doInBackground(View... params)
{
// Store provided views.
if (params.length != 1)
throw new IllegalArgumentException();
mTextField = (EditText) params[0];
// Create socket.
Socket client = null;
try
{
client = new Socket(IP_ADDRESS, DEST_PORT); // connect to server
} catch (UnknownHostException e)
{
e.printStackTrace();
} catch (IOException e)
{
e.printStackTrace();
}
return client;
}
/**
* Write to server.
*/
protected void onPostExecute(Socket client)
{
try
{
PrintWriter printwriter;
String messsage;
messsage = mTextField.getText().toString(); // get the text message on the text field
mTextField.setText(""); // Reset the text field to blank
printwriter = new PrintWriter(client.getOutputStream(), true);
printwriter.write(messsage); // write the message to output stream
printwriter.flush();
printwriter.close();
client.close();
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
答案 0 :(得分:0)
感谢@zapl,请检查PC的IP地址。当正确的地址是192.168.1.56时,有192.168.56.1。