android.os.NetworkOnMainThreadException (client application android / serveur application java) (send string)(using emulator localhost)
android.os.NetworkOnMainThreadException (client application android / serveur application java) (send string)(using emulator localhost)
android 应用程序中的 class 客户端(使用模拟器本地主机),向我的 java 应用程序(服务器)发送一个字符串:
public class Client_socket extends MainActivity{
private static Socket socket;
public static void lance()
{
try
{
String host = "localhost";
int port = 25000;
InetAddress address = InetAddress.getByName(host);
socket = new Socket("127.0.0.3", port);
//Send the message to the server
OutputStream os = socket.getOutputStream();
OutputStreamWriter osw = new OutputStreamWriter(os);
BufferedWriter bw = new BufferedWriter(osw);
String nom = "premiere";
String sendMessage = ism + "\n";
bw.write(sendMessage);
bw.flush();
System.out.println("Message sent to the server : "+sendMessage);
//Get the return message from the server
InputStream is = socket.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String message = br.readLine();
System.out.println("Message received from the server : " +message);
}
catch (Exception exception)
{
exception.printStackTrace();
}
finally
{
//Closing the socket
try
{
socket.close();
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
}
java 应用程序中服务器的 class 如何接收我的字符串:
我可以使用什么 ip 和端口?如何获取我的 ip?
public class Server
{
private static Socket socket;
public static void main(String[] args)
{
try
{
int port = 25000;
ServerSocket serverSocket = new ServerSocket(port);
System.out.println("Server Started and listening to the port 25000");
//Server is running always. This is done using this while(true) loop
while(true)
{
//Reading the message from the client
socket = serverSocket.accept();
InputStream is = socket.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String nom = br.readLine();
System.out.println("Message received from client is "+nom);
}
}
catch (Exception e)
{
e.printStackTrace();
}
finally
{
try
{
socket.close();
}
catch(Exception e){}
}
}
}
确保在后台线程中执行网络操作:
new Thread(new Runnable() {
@Override
public void run() {
lance(); // call your network method here
}
}).start();
android 应用程序中的 class 客户端(使用模拟器本地主机),向我的 java 应用程序(服务器)发送一个字符串:
public class Client_socket extends MainActivity{
private static Socket socket;
public static void lance()
{
try
{
String host = "localhost";
int port = 25000;
InetAddress address = InetAddress.getByName(host);
socket = new Socket("127.0.0.3", port);
//Send the message to the server
OutputStream os = socket.getOutputStream();
OutputStreamWriter osw = new OutputStreamWriter(os);
BufferedWriter bw = new BufferedWriter(osw);
String nom = "premiere";
String sendMessage = ism + "\n";
bw.write(sendMessage);
bw.flush();
System.out.println("Message sent to the server : "+sendMessage);
//Get the return message from the server
InputStream is = socket.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String message = br.readLine();
System.out.println("Message received from the server : " +message);
}
catch (Exception exception)
{
exception.printStackTrace();
}
finally
{
//Closing the socket
try
{
socket.close();
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
}
java 应用程序中服务器的 class 如何接收我的字符串: 我可以使用什么 ip 和端口?如何获取我的 ip?
public class Server
{
private static Socket socket;
public static void main(String[] args)
{
try
{
int port = 25000;
ServerSocket serverSocket = new ServerSocket(port);
System.out.println("Server Started and listening to the port 25000");
//Server is running always. This is done using this while(true) loop
while(true)
{
//Reading the message from the client
socket = serverSocket.accept();
InputStream is = socket.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String nom = br.readLine();
System.out.println("Message received from client is "+nom);
}
}
catch (Exception e)
{
e.printStackTrace();
}
finally
{
try
{
socket.close();
}
catch(Exception e){}
}
}
}
确保在后台线程中执行网络操作:
new Thread(new Runnable() {
@Override
public void run() {
lance(); // call your network method here
}
}).start();