[英]Server/Client Android to PC communication issue
我试图使用套接字将Android手机作为客户端,我的PC作为服务器来编写程序,并且双方都稍作修改,我的服务器代码可以正常工作,但是我无法获得客户端活动并且我不这样做我从根本上知道如何使程序与我交谈,我希望基本上能够有任何一方输入文本并将其发送并设置为对其他用户可见,我知道你们有很多问题可以解决,所以...我让这些人交流吗?
客户端代码(模拟器崩溃,我什至无法开始看到问题)
package com.example.Socket;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.view.View.*;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import java.io.*;
import java.net.*;
public class ClientActivity extends Activity {
private EditText serverIp;
private Button connectPhones;
private String serverIpAddress = "";
private boolean connected = false;
private Handler handler = new Handler();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.client);
serverIp = (EditText) findViewById(R.id.server_ip);
connectPhones = (Button) findViewById(R.id.connect_phones);
connectPhones.setOnClickListener((android.view.View.OnClickListener) connectListener);
}
private OnClickListener connectListener = new OnClickListener() {
@Override
public void onClick(View v) {
if (!connected) {
serverIpAddress = serverIp.getText().toString();
if (!serverIpAddress.equals("")) {
Thread cThread = new Thread(new ClientThread());
cThread.start();
}
}
}
};
public class ClientThread implements Runnable {
public void run() {
try {
InetAddress serverAddr = InetAddress.getByName(serverIpAddress);
Log.d("ClientActivity", "C: Connecting...");
Socket socket = new Socket(serverAddr, 9999);
connected = true;
while (connected) {
try {
Log.d("ClientActivity", "C: Sending command.");
PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket
.getOutputStream())), true);
// where you issue the commands
out.println("Hey Server!");
Log.d("ClientActivity", "C: Sent.");
} catch (Exception e) {
Log.e("ClientActivity", "S: Error", e);
}
}
socket.close();
Log.d("ClientActivity", "C: Closed.");
} catch (Exception e) {
Log.e("ClientActivity", "C: Error", e);
connected = false;
}
}
}
}
服务器代码
import java.io.*;
import java.net.*;
/**
* Simple server using Java Sockets.
* @author Jonathan Engelsma (http://www.cis.gvsu.edu/~engelsma)
*
*/
public class Server {
/**
* @param args
*/
public static void main(String[] args) {
try {
// First we create a server socket and bind it to port 9999.
ServerSocket myServerSocket = new ServerSocket(9999);
// wait for an incoming connection...
System.out.println("Server is waiting for an incoming connection on host="
+ InetAddress.getLocalHost().getCanonicalHostName()
+ " port=" + myServerSocket.getLocalPort());
Socket skt = myServerSocket.accept();
// ok, got a connection. Let's use java.io.* niceties to read and write from the connection.
BufferedReader myInput = new BufferedReader(new InputStreamReader(skt.getInputStream()));
PrintStream myOutput = new PrintStream(skt.getOutputStream());
// attempt to read input from the stream.
String buf = myInput.readLine();
// if we got input, print it out and write a message back to the remote client..
if (buf != null) {
System.out.println("Server read: [" + buf + "]");
myOutput.print("got it");
}
// close the connection.
skt.close();
System.out.println("Server is exiting");
} catch (IOException ex) {
ex.printStackTrace();
System.out.println("Whoops, something bad happened! I'm outta here.");
}
}
}
如果您使用的是ICS或JB,则可能会被限制在主要活动中打开网络连接。 您将收到有关网络权限的不透明错误消息。
我不能在这里将我的答案重新发布到另一个非常类似的StackOverflow问题(主持人认为这是垃圾邮件),但是您可以在此处查看 。
我在那里发布了一个使用asynctask的功能发送和接收套接字连接器客户端。
你正在用吗
<uses-permission android:name="android.permission.INTERNET" />
在Android清单中? 那可能会导致您的问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.