Guest User

Untitled

a guest
Aug 4th, 2016
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.60 KB | None | 0 0
  1. public class MainActivity extends AppCompatActivity
  2. {
  3. //TextView textView;
  4. Button sendButton;
  5. Button connect;
  6. EditText message;
  7. OutputStream outputStream;
  8. InputStream inputStream;
  9. Socket socket;
  10.  
  11. @Override
  12. protected void onCreate(Bundle savedInstanceState) {
  13. super.onCreate(savedInstanceState);
  14. setContentView(R.layout.activity_main);
  15. sendButton = (Button) findViewById(R.id.sendButton);
  16. connect = (Button) findViewById(R.id.button);
  17. message = (EditText) findViewById(R.id.message);
  18. connect.setOnClickListener(new View.OnClickListener()
  19. {
  20. @Override
  21. public void onClick(View view)
  22. {
  23. connect.setText("Disconnect");
  24. AsyncTask asyncTask = new AsyncTask() {
  25. @Override
  26. protected Object doInBackground(Object[] objects)
  27. {
  28. try {
  29. socket = new Socket("192.168.100.106",8888);
  30. try {
  31. outputStream = socket.getOutputStream();
  32. inputStream = new DataInputStream(socket.getInputStream());
  33. } catch (IOException e) {
  34. e.printStackTrace();
  35. }
  36. connect.setOnClickListener(new View.OnClickListener()
  37. {
  38. @Override
  39. public void onClick(View view)
  40. {
  41.  
  42. try {
  43. socket.close();
  44. } catch (IOException e) {
  45. e.printStackTrace();
  46. }
  47. }
  48. });
  49. sendButton.setOnClickListener( new View.OnClickListener() {
  50. @Override
  51. public void onClick(View view)
  52. {
  53. PrintWriter out = new PrintWriter(outputStream);
  54. String mes = message.getText().toString();
  55. out.print(mes);
  56. }
  57. });
  58. } catch (IOException e) {
  59. e.printStackTrace();
  60. }
  61. return null;
  62. }
  63. };
  64. asyncTask.execute();
  65. }
  66. });
  67.  
  68. }
  69. }
  70.  
  71. from twisted.internet.protocol import Protocol, Factory
  72. from twisted.internet import reactor
  73. import ctypes # An included library with Python install.
  74. class DataTransfer(Protocol):
  75. def connectionMade(self):
  76. #self.transport.write("""connected""")
  77. self.factory.clients.append(self)
  78. print "clients are ", self.factory.clients
  79. self.username = ""
  80. self.password = ""
  81. self.auth = False
  82. self.ipaddress = self.transport.getPeer()
  83. print self.ipaddress
  84.  
  85. def connectionLost(self, reason):
  86. self.factory.clients.remove(self)
  87. print reason
  88.  
  89. def dataReceived(self, data):
  90. print data
  91. a = data.split(':')
  92. if len(a) > 1:
  93. command = a[0]
  94. content = a[1]
  95.  
  96. msg = ""
  97.  
  98. self.message(msg)
  99.  
  100. def message(self, message):
  101. self.transport.write(message + 'n')
  102.  
  103. factory = Factory()
  104. factory.protocol = DataTransfer
  105. factory.clients = []
  106.  
  107. reactor.listenTCP(8888, factory)
  108. print "Server started"
  109. reactor.run()
Add Comment
Please, Sign In to add comment