在同一个局域网内(WIFI),利用socket实现手机与PC通信,手机client,PC为server,问题一直处在第一步的TCP连接(即客户端卡在new socket(IP,port),服务端卡在socket = serversocket.accpet())这里,检查了client的访问wifi权限和网络权限都打开了,代码也正常,可就是抛出连接超时异常结束;
错误信息:
以下是我的socket代码;
Client:
主线程:
Server端:
客户端的权限:
代码很简单,可就是一直通不了,求见解,解决方案;IP这些都是对的
错误信息:
![](http://img.bbs.csdn.net/upload/201508/05/1438749494_465789.png)
以下是我的socket代码;
Client:
主线程:
public class MainActivity extends Activity implements OnClickListener{
private static final String TAG = "AC";
private EditText msg;
private TextView content;
private Button send,closed;
InetAddress inet;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
Log.i(TAG, NetType(this));
new ThreadNet().start();
}
//子线程模拟客户端
package com.example.connection;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;
import android.util.Log;
public class ThreadNet extends Thread{
private static final String TAG = "ThreadNet";
private static final String path = "192.168.1.102";
private static final int PORT = 54321;
private Socket socket;
public ThreadNet(){
}
@Override
public void run() {
// TODO Auto-generated method stub
String str = "i com from android device!";
OutputStream out = null;
InputStream in = null;
try {
Log.i(TAG, "11111111111 ");
socket = new Socket(path,PORT); // 程序卡在这个地方,一直在尝试连接
Log.i(TAG, "2222222222222 ");
out = socket.getOutputStream();
byte[] buffer = str.getBytes();
out.write(buffer);
Log.i(TAG, "33333333333 ");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
Log.i(TAG, "MSG: "+e.toString());
}finally{
if(out != null){
try {
out.close();
out = null;
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(socket != null){
try {
socket.close();
socket = null;
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
Server端:
public class HttpClient {
private static final int PORT = 54321;
//main方法
public static void main(String[] args){
mServer();
}
public static void mServer(){
ServerSocket sSocket = null;
InputStream in = null;
OutputStream out = null;
Socket socket = null;
byte[] buffer = new byte[1024];
String str = null;
try {
sSocket = new ServerSocket(PORT);
System.out.println("11111111======");
socket = sSocket.accept(); /服务端一直停在这里了
System.out.println("-222222======");
in = socket.getInputStream();
int len = 0;
System.out.println("----======");
while((len = in.read(buffer)) != -1){
str = new String(buffer,0,len);
System.out.print(str);
}
} catch (Exception e) {
// TODO Auto-generated catch block
System.out.println("234: "+ e.toString());
}finally{
if(in != null){
try {
in.close();
in = null;
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(socket != null){
try {
socket.close();
socket = null;
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
客户端的权限:
<uses-permission android:name="android.permission.ACCESS_WIFI_STAT"/>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE"/>
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE"/>
代码很简单,可就是一直通不了,求见解,解决方案;IP这些都是对的