I have been trying to write socket server client programs, I am having difficulty passing things through posts and bundles using them in the way I have been

  • Replies:0
Jethro Devøn
  • Forum posts: 1

Sep 24, 2013, 3:02:43 PM via Website

So here I have a really basic example of the sort of things I have been trying to get to work, I used to mess around with java in this way and found it much easier, I also tried packing things in bundles and the accessing them, even variables to be used in functions, that way I hoped I could get the variables from textboxes and then say use them in a thread to initialize ports and ip addresses in the functions themselves...this didnt work!

All in all I can see I'm barking up the wrong tree, what is it that I think I can do but I can't??

1public class MainActivity extends Activity {
2
3 Socket sock;
4 TextView txtvw;
5 String string;
6 Handler updatehandler;
7
8 @Override
9 protected void onCreate(Bundle savedInstanceState) {
10 super.onCreate(savedInstanceState);
11 setContentView(R.layout.activity_main);
12
13 updatehandler = new Handler();// <-- why did my tutorial put this here? I create it in the universal namespace above :/
14 txtvw = (TextView)findViewById(R.id.textView1);
15 new Thread(new connect()).start();//im not sure whats wrong maybe the threads are not running?
16 }
17
18 class connect implements Runnable{
19
20 @Override
21 public void run() {
22 try {
23 InetAddress ip = InetAddress.getByName("chat.freenode.net");
24 sock = new Socket(ip,6667);
25 OutputStream out = sock.getOutputStream();
26 PrintWriter output = new PrintWriter(out);
27 output.println("NICK devonrevenge\r\n USER DevonRevenge 0 * :Ortej\r\n JOIN :#chessplusplus");
28
29 while (!Thread.currentThread().isInterrupted()) {
30 BufferedReader input = new BufferedReader (new InputStreamReader(sock.getInputStream()));
31 string = input.readLine();
32 updatehandler.post(new updatui(string));//I even put actual strings in here nothing changed so I know this is not
33//working but cannot see whats wrong with it
34 }
35
36
37 } catch (UnknownHostException e) {
38 // TODO Auto-generated catch block
39 e.printStackTrace();
40 } catch (IOException e) {
41 // TODO Auto-generated catch block
42 e.printStackTrace();
43 }
44
45 }
46
47 }
48 //I hoped this would update textView1's text, I suspect that I set it up wrong
49 class updatui implements Runnable{
50 private String msg;
51 public updatui(String str){
52 msg = str;//here the tutorial i followed had a this. before msg....why?
53 }
54
55 @Override
56 public void run() {
57 txtvw.setText(txtvw.getText().toString());
58 }
59
60 }
61
62}

— modified on Sep 24, 2013, 3:24:25 PM

Reply