gpt4 book ai didi

java - 从 Android 应用程序通过 TCP 发送数据

转载 作者:可可西里 更新时间:2023-11-01 02:34:15 24 4
gpt4 key购买 nike

我正在尝试从 Android 应用程序发送数据客户端类源:

import java.io.*;
import java.net.Socket;
import java.net.UnknownHostException;

public class CClient
implements Runnable
{
private Socket socket;
private String ServerIP = "10.0.0.14";


public void run()
{
try
{
socket = new Socket("10.0.0.14", 16606);

}
catch(Exception e)
{
System.out.print("Whoops! It didn't work!:");
System.out.print(e.getLocalizedMessage());
System.out.print("\n");
}
}

public void Send(String s)
{
try
{
PrintWriter outToServer = new PrintWriter(new OutputStreamWriter(socket.getOutputStream()));
outToServer.print(s + "\n");
outToServer.flush();


}
catch (UnknownHostException e) {
System.out.print(e.toString());
} catch (IOException e) {
System.out.print(e.toString());
}catch (Exception e) {
System.out.print(e.toString());
}

}
}

在 Activity 中使用 CClient 类来创建连接和发送数据。这是 Activity 代码

import android.os.Bundle;
import android.app.Activity;
import android.view.*;
import android.widget.*;


public class Auth extends Activity {

private ProgressBar mProgress;
private TextView mTV;

private CClient mClient;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_auth);


mProgress = (ProgressBar) findViewById(R.id.progressBar1);
mTV = (TextView) findViewById(R.id.textView4);

mClient = new CClient();
Thread myThready = new Thread(mClient);
myThready.start();

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.auth, menu);
return true;
}

public void proc_Login(View v)
{

for (int i=0; i<5; i++)
mClient.Send("asaadsasdasd");
}

}

问题:仅从客户端(C# 服务器,没有任何错误)接收到一条消息,并且不会发送下一条消息。

最佳答案

此代码有效。我必须添加一个简单的按钮来触发 proc_Login 调用,我使用

netcat -l -p 8546
on my server (debian), I got 5 'asaadsasdasd' each time I pressed the button.(I used the port 8546 because it was already opened on my firewall).

Of course I added

<uses-permission android:name="android.permission.INTERNET" />

in the AndroidManifest.xml file.

Maybe your server code closes the socket after receiving a line.

Note also you don't need a Thread, an AsyncTask would be more appropriate, though for the example it works.

MainActivity.java:

package com.defranoux.testtcpsend;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MainActivity extends Activity {

private CClient mClient;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

mClient = new CClient();
Thread myThready = new Thread(mClient);
myThready.start();

Button button1 = (Button) findViewById(R.id.button1);
button1.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View arg0) {
proc_Login(arg0);
}
});
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}

public void proc_Login(View v)
{
for (int i=0; i<5; i++)
mClient.Send("asaadsasdasd");
}

}
进行了测试

CClient.java:

package com.defranoux.testtcpsend;

import java.io.*;
import java.net.Socket;
import java.net.UnknownHostException;

public class CClient
implements Runnable
{
private Socket socket;
private String ServerIP = "<my server ip goes here>";
private static final int ServerPort = 8546;

@Override
public void run()
{
try
{
socket = new Socket(ServerIP, ServerPort);
}
catch(Exception e)
{
System.out.print("Whoops! It didn't work!:");
System.out.print(e.getLocalizedMessage());
System.out.print("\n");
}
}

public void Send(String s)
{
try
{
PrintWriter outToServer = new PrintWriter(
new OutputStreamWriter(
socket.getOutputStream()));
outToServer.print(s + "\n");
outToServer.flush();


}
catch (UnknownHostException e) {
System.out.print(e.toString());
} catch (IOException e) {
System.out.print(e.toString());
}catch (Exception e) {
System.out.print(e.toString());
}

}
}

关于java - 从 Android 应用程序通过 TCP 发送数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22018305/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com