我试图在计算机和手机之间建立连接。 当我运行PC_Client和phone_Server时,它们都可以工作。但是当我运行整个程序时,它没有显示数据。当我在模拟器中单击它们时,它表示服务器已停止。
任何帮助将不胜感激!
在PC_Client中,
package com.example.violet.server;
import java.net.*;
import java.io.*;
class PC_Client
{
public static void main(String[] args)
throws IOException
{
//Socket socket = new Socket("127.0.0.1" , 30000);
Socket socket = new Socket("10.1.1.6" , 2000);
BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
String line = br.readLine();
System.out.println("from server:" + line);
br.close();
socket.close();
}
}
在phone_Server
中package com.example.violet.server;
import java.net.*;
import java.io.*;
class phone_Server
{
public static void main(String[] args)
throws IOException
{
InetAddress address=InetAddress.getLocalHost();
System.out.println("Ip is"+address.getHostAddress());
ServerSocket ss = new ServerSocket(2000);
while (true)
{
Socket s = ss.accept();
PrintStream ps = new PrintStream(s.getOutputStream());
ps.println("Hello!");
ps.close();
s.close();
}
}
}
在清单
中<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.violet.server">
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"></uses-permission>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
</application>
</manifest>
项目结构:
模拟器: