Android 套接字客户端无法发送和接收消息

Android Socket client unable to send and receive messages

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

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();
        }
    });

}
}

这里是我的套接字服务器脚本,在扭曲的 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()

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

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