Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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();
- }
- });
- }
- }
- 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()
Add Comment
Please, Sign In to add comment