安卓利用Socket和PC通信無(wú)法連接
已經(jīng)設(shè)置了 <uses-permission android:name="android.permission.INTERNET"/>
相同代碼PC端和PC端通信沒有問題
會(huì)在new Socket(ip,port)這里一直堵塞;
服務(wù)器代碼
public ServiceThread(Socket s) throws IOException {
socket = s;
out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket
.getOutputStream())), true);
in=socket.getInputStream();
}
public void run() {
try {
out.println("SERVER DATA");
System.out.println("closing...");
byte[] temp = new byte[1024];
int length=-1;
while((length=in.read(temp))>0){
System.out.println(temp);
}
out.close();
socket.close();
} catch (Exception e) {
}
}
}
public class MyServer {
static final int PORT = 8778;
public static void main(String[] args) throws IOException {
ServerSocket s = new ServerSocket(PORT);
System.out.println("Server 啟動(dòng)");
Socket socket = s.accept();
ServiceThread t = new ServiceThread(socket);
t.start();
}
}
客戶端代碼
Socket socket = null;
BufferedWriter out = null;
try {
socket = new Socket(ip, 8778);
out = new BufferedWriter(new OutputStreamWriter(socket
.getOutputStream()));
out.write(sendViewA.getText().toString());
} catch (Exception e) {
e.printStackTrace();
} finally {
Socket socket = null;
BufferedReader in = null;
try {
socket = new Socket(ip, 8778);
in = new BufferedReader(new InputStreamReader(socket
.getInputStream()));
String fromServerStr = in.readLine();
Toast.makeText(OUTActivity.this,fromServerStr,Toast.LENGTH_LONG).show();
} catch (Exception e) {
e.printStackTrace();
} finally {
認(rèn)證0級(jí)講師
This should be an IP problem:
1. If your device is an emulator: Please check the IP address of the corresponding emulator to access the computer host. Generally, it is available on the official website of the corresponding emulator (the specific IP addresses vary depending on the emulator)
Genymotion: 10.0.3.2
Android emulator: 10.0.2.2
2. If your device is a real machine, it is best to ensure that the mobile phone and the real machine are in the same LAN (if it is a laptop, it is recommended to use the mobile phone to connect to the computer WiFi before accessing). You can check the IP address in cmd yourself
I encountered this once before. Try changing the client’s socket connection code to the following one.
Client:
socket = new Socket();
socket.connect(new InetSocketAddress("1.1.9.30",8080), 5000);
If the mobile phone and PC are both on the same LAN, you need to pay attention to whether wireless isolation is enabled on the router. This may also be a pitfall.
Looking at your error report, did you let the Socket run in the UI thread? ——On Android, after Android 4.0, Socket is not allowed to be used in the UI thread, and a new thread needs to be started to use Socket
NetworkOnMainThreadException, does it involve Socket-related operations in the main thread?
Let the code run first. Assuming that your current client code is written in startClient(), you need to put this code in the child thread for execution:
new Thread(new Runnable() {
@Override
public void run() {
startClient();
}
}).start();
In fact, you also have to deal with the interaction between this thread and the main thread (UI thread), which is related to the business code.
It is recommended to first understand the difference/relationship between the main thread (UI thread) and the sub-thread, and then take a look Implementation of thread communication.
The log clearly says "network on main thread exception". Let's use a newly opened thread to perform network operations as mentioned above