【问题标题】:Android Socket client unable to send and receive messagesAndroid Socket客户端无法收发消息
【发布时间】:2015-08-08 06:58:11
【问题描述】:

我想从我的套接字服务器发送和接收消息,该服务器是在 windows 上的 python 中创建的,借助 twisted API。我的客户将成为我的安卓手机,我将发送我的字符串消息。这是我的代码。有人可以帮忙吗。

public class MainActivity extends AppCompatActivity
{
//TextView textView;
Button sendButton;
Button connect;
EditText message;
OutputStream outputStream;
InputStream inputStream;
Socket socket;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    sendButton = (Button) findViewById(R.id.sendButton);
    connect = (Button) findViewById(R.id.button);
    message = (EditText) findViewById(R.id.message);
    connect.setOnClickListener(new View.OnClickListener()
    {
        @Override
        public void onClick(View view)
        {
            connect.setText("Disconnect");
            AsyncTask asyncTask = new AsyncTask() {
                @Override
                protected Object doInBackground(Object[] objects)
                {
                    try {
                         socket = new Socket("192.168.100.106",8888);
                        try {
                            outputStream = socket.getOutputStream();
                            inputStream = new DataInputStream(socket.getInputStream());
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                            connect.setOnClickListener(new View.OnClickListener()
                            {
                                @Override
                                public void onClick(View view)
                                {

                                    try {
                                        socket.close();
                                    } catch (IOException e) {
                                        e.printStackTrace();
                                    }
                                }
                            });
                            sendButton.setOnClickListener( new View.OnClickListener() {
                                @Override
                                public void onClick(View view)
                                {
                                    PrintWriter out = new PrintWriter(outputStream);
                                    String mes = message.getText().toString();
                                    out.print(mes);
                                }
                            });
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                    return null;
                }
            };
            asyncTask.execute();
        }
    });

}
}

这是我的套接字服务器脚本,它借助 twisted API 用 python 编码。

    from twisted.internet.protocol import Protocol, Factory
from twisted.internet import reactor
import ctypes  # An included library with Python install.
class DataTransfer(Protocol):
    def connectionMade(self):
            #self.transport.write("""connected""")
            self.factory.clients.append(self)
            print "clients are ", self.factory.clients
            self.username = ""
            self.password = ""
            self.auth = False
            self.ipaddress = self.transport.getPeer()
            print self.ipaddress

    def connectionLost(self, reason):
        self.factory.clients.remove(self)
        print reason

    def dataReceived(self, data):
        print data
        a = data.split(':')
        if len(a) > 1:
                    command = a[0]
                    content = a[1]

                    msg = ""

                    self.message(msg)

    def message(self, message):
            self.transport.write(message + '\n')

factory = Factory()
factory.protocol = DataTransfer
factory.clients = []

reactor.listenTCP(8888, factory)
print "Server started"
reactor.run()

目前我能够通信(即与服务器连接和断开连接。)但只是我无法发送和接收消息。

【问题讨论】:

    标签: android python sockets twisted


    【解决方案1】:

    直接使用 outputStream 而不是PrintWriter out = new PrintWriter(outputStream);,它应该可以工作。 :)

    【讨论】:

      猜你喜欢
      • 2013-04-24
      • 1970-01-01
      • 1970-01-01
      • 2016-05-27
      • 2010-09-23
      • 2012-03-06
      • 1970-01-01
      • 2014-01-20
      • 2012-02-12
      相关资源
      最近更新 更多