求各位大佬帮帮我,16号就要完成的orz……
服务端
import sockettcpServerSocket=socket.socket()#创建socket对象
host = socket.gethostname()#获取本地主机名
port=12567#设置端口
tcpServerSocket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)#让端口可以复用
tcpServerSocket.bind((host,port))#将地址与套接字绑定,且套接字要求是从未被绑定过的
tcpServerSocket.listen(5)#代办事件中排队等待connect的最大数目
#tcpServerSocket.settimeout(10000)
while True:
    print("等待连接")
    #建立客户端连接,接受connection,返回两个参数,c是该connection上可以发送和接收数据的新套接字对象
    #addr是与connection另一端的套接字绑定的地址
    clientSocket, addr = tcpServerSocket.accept()    
    #clientSocket.settimeout(10000)
    print ('连接地址:', addr)
    while True:
        data=clientSocket.recv(4096)
        if not data:
            break
        #向客户端表示接收到数据
        str='来自服务端的消息!'
        clientSocket.send(str.encode())#字符串编码为字节
    #套接字在垃圾收集garbage-collected时会自动close
    #close关闭该connection上的资源但不一定马上断开connection
    #想要立即断开connection,要先调用shutdown再close
        clientSocket.close() # 关闭连接
tcpServerSocket.close()
客户端
public class MainActivity extends AppCompatActivity {
    @BindView(R.id.button1)Button button1;
    @BindView(R.id.editText)EditText editText;
    private static final int port=12567;//connection另一端的端口
    private static final String hostip="SJXBDATH5ZQZGTW";//connection另一端的ip    @Override
    protected void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
    }
    @OnClick(R.id.button1)
    public void runtcpClient(){
        Log.i("Client","按钮监听事件有效");
        Thread thread=new Thread(){
            @Override
            public void run(){ tcpClient();}
        };
        thread.start();
    }
    private void tcpClient(){
        try{
            Socket socketClient = new Socket(hostip, port);
            Log.i("Client","新建套接字有效");
            BufferedReader in=new BufferedReader(new InputStreamReader(socketClient.getInputStream()));
            BufferedWriter out=new BufferedWriter(new OutputStreamWriter(socketClient.getOutputStream()));
            String outMsg = "TCP connecting to " + port + System.getProperty("line.separator");//发出的数据
            Log.i("Client","得到发出数据");
            out.write(outMsg);//发送数据
            Log.i("Client","发送数据有效");
            out.flush();
            Log.i("TcpClient", "sent: " + outMsg);
            String inMsg = in.readLine() + System.getProperty("line.separator");//服务器返回的数据
            Log.i("TcpClient", "received: " + inMsg);
            socketClient.close();
        }catch (UnknownHostException e){e.printStackTrace();}
        catch (IOException e){e.printStackTrace();}
    }服务端已启动报错日志报错说的36行是这个  public void run(){ tcpClient();}
 at com.example.administrator.client.MainActivity$1.run(MainActivity.java:36)
    Caused by: android.system.ErrnoException: connect failed: ETIMEDOUT (Connection timed out)

解决方案 »

  1.   

    我权限也有加了<uses-permission android:name="android.permission.INTERNET"/>
    还有一个问题是:我的电脑有安装虚拟机,python服务端打印出来的套接字地址是用VMware Network Adapter VMnet1的IPv4地址
    但是当我在android代码中如果把hostip改成无线网络连接的IPv4地址,日志就报错android.system.ErrnoException: connect failed: ECONNREFUSED,同样在36行,我该用哪个IP地址?我该怎么修改呢?