-java chat client problem- threads

I actually tried to pretend it was a c++ problem but i couldnt post the code there, Im trying different kinds of synchronisation but just cant seem to get the chat running freely, i have to hit return for the next line :/

can some explain why calling threads chatthreadsend/receive crashes

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
public void begin (){
        System.out.println("Welcome to Iffy chat client, Enter target IP");

        theIP = name.nextLine();

        try{
        try{
            connect();
            setarp();
            runchatprogram();

        }catch(IOException ioexception){
         ioexception.printStackTrace();
        }finally{

            System.out.println("\nio-exception...sry\n");
            closethestuff();

        }
        }catch (IOException ioexcption){
            ioexcption.printStackTrace();
        }
    }

    private void runchatprogram() throws IOException{
        sendmeassage("iffy unstable chat client managed to send you this start message");
        System.out.println("*----READY----*");
        while(true){

              //this is where i tried chat threads send and recieve
                 //there were errors
            sendmeassage(typemessage());
            getmessage();

        }
    }





    private void closethestuff() throws IOException {
        System.out.println("Killing everything");
        connectify.close();
        soot.close();
        sin.close();

    }



    private synchronized void getmessage () throws IOException{
        gets++;
        try{
        System.out.printf("%s:%s", gets, sin.readObject());//be surprised if the syntax works

        }catch(ClassNotFoundException cnfe){
            System.out.println("been sent some odd ass class");
        }

    }

    private synchronized void sendmeassage(String s)throws IOException {
        soot.writeObject(s);
        soot.flush();

    }

    private String typemessage (){
        String sentence;
        sentence = name.nextLine();
        return sentence;
    }

    private void setarp() throws IOException{
        soot = new ObjectOutputStream(connectify.getOutputStream());
        soot.flush();
        sin = new ObjectInputStream(connectify.getInputStream());
    System.out.println("*-----TO------*\n*---STREAMS---*");

    }


    private void connect() throws IOException {

       connectify = new Socket (InetAddress.getByName(theIP),9000);
        System.out.println("*--CONNECTED--*");
    }




        class chatthreadrecieve extends Thread{


        public void run(){

            try{

                getmessage();
                try {
                    sleep(1);
                } catch (InterruptedException e) {
                    System.out.println(e);
                }

            }catch(IOException ioe){
                System.out.printf("in threads recieve: %d",ioe);

            }

        }

    }
    class chatthreadsend extends Thread{


        public void run(){

            try{

                sendmeassage(typemessage());
                try {
                    sleep(1);
                } catch (InterruptedException e) {
                    System.out.println(e);
                }

            }catch(IOException ioe){
                System.out.printf("in threads recieve: %d", ioe);

            }

        }

    }

}
Last edited on
*tumble weed*
Well, first of all, the compiler should reject that code you posted, as those methods exist outside of a class along with some undeclared variables. Any chance you can post the complete code?
Thanks for looking, I added the swing stuff but its still the same code, with the same-ish problem

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.InetAddress;
import java.net.Socket;
import java.util.Scanner;
import java.util.concurrent.locks.Lock;


public class connecty extends JFrame{

    private JTextField inputer,ipmonster;
    private JTextArea chatwindow;
    private Socket connectify;
    public String theIP;
    private ObjectOutputStream soot;
    private ObjectInputStream sin;
    Scanner name;
    static Integer gets;


     public connecty(){

         super ("I R CLIENT");
         inputer = new JTextField();
         ipmonster = new JTextField();
         ipmonster.setText("Enter target IP here");
         chatwindow = new JTextArea();
         inputer.setEditable(true);
         inputer.addActionListener(
                 new ActionListener() {
                     public void actionPerformed(ActionEvent event) {
                         try {
                             sendmeassage(event.getActionCommand());
                         } catch (IOException e) {
                             e.printStackTrace();
                         }
                         inputer.setText("");
                     }
                 }
         );
         ipmonster.addActionListener(new ActionListener() {
             public void actionPerformed(ActionEvent event) {
               theIP = event.getActionCommand();
                 ipmonster.setEditable(false);

                   begin();

             }
         });
         add(ipmonster, BorderLayout.SOUTH);
         add(inputer, BorderLayout.NORTH);
         chatwindow = new JTextArea();
         add(new JScrollPane(chatwindow));
         setSize(300,150);
         setVisible(true);
     }
    public void begin (){


        try{
        try{
            connect();
            setarp();
            runchatprogram();

        }catch(IOException ioexception){
         ioexception.printStackTrace();
        }

        finally{

            showmessage("\n***io-exception...sry***");
            closethestuff();

        }
        }catch (IOException ioexcption){
            ioexcption.printStackTrace();
        }
    }

    private void runchatprogram() throws IOException {
        abletotype(true,1);
        sendmeassage("--STREAM OBJECT RECEIVED FROM CLIENT--");
        showmessage("\n*----READY----*\n\n");
        while(true){

         getmessage();

        }
    }





    private void closethestuff() throws IOException {
      showmessage("\nKilling everything");
        connectify.close();
        soot.close();
        sin.close();

    }



    private synchronized void getmessage () throws IOException{

        String streamstring;
        try{
        streamstring = (String)sin.readObject();
        showmessage(streamstring);//be surprised if the syntax works

        }catch(ClassNotFoundException cnfe){
            showmessage("been sent some odd ass class");
        }

    }

    private synchronized void sendmeassage(String s)throws IOException {
        soot.writeObject(s);
        soot.flush();

    }

    private String typemessage (){
        String sentence;
        sentence = name.nextLine();
        return sentence;
    }

    public void setarp() throws IOException{


        soot = new ObjectOutputStream(connectify.getOutputStream());
        soot.flush();
        sin = new ObjectInputStream(connectify.getInputStream());
        showmessage("\n*-----TO------*\n*---STREAMS---*");

    }


    public void connect() throws IOException {

       connectify = new Socket (InetAddress.getByName(theIP),9000);
       showmessage("\n*--CONNECTED--*");

    }

    public void showmessage (final String text){  //why final??
        SwingUtilities.invokeLater(new Runnable() {

            public void run() {
                chatwindow.append(text);
            }
        });
    }



    class chatthreadrecieve extends Thread{


        public void run(){

            try{

                getmessage();
                try {
                    sleep(1);
                } catch (InterruptedException e) {
                    System.out.println(e);
                }

            }catch(IOException ioe){
                showmessage(ioe.toString());

            }

        }

    }
    class chatthreadsend extends Thread{


        public void run(){

            try{

                sendmeassage(typemessage());
                try {
                    sleep(1);
                } catch (InterruptedException e) {
                    showmessage(e.toString());
                }

            }catch(IOException ioe){
               showmessage(ioe.toString());

            }

        }

    }

    public void abletotype (final boolean tof,final Integer A){
        SwingUtilities.invokeLater(new Runnable() {

            public void run() {
                if(A == 1)
                inputer.setEditable(tof);
                if(A == 2)
                ipmonster.setEditable(tof);
            }
        });
    }

}


1
2
3
4
5
6
7
8
9
10
11
12
13
public class test {

    public static void main(String[] args) {

     connecty crapclient = new connecty();
        crapclient.setSize(200,200);
        crapclient.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        crapclent.setVisible(true);

        }


}
Last edited on
Still a compile-time error:

crapclent.setVisible(true); <- undeclared identifier

...will run it now.

java.net.ConnectException: Connection refused: connect

What exactly is the stack-trace that you're getting? "Crash" doesn't tell me much.
Last edited on
sry, I forgot to mention it only works if a server is listening on that port

i have a server that it connects to too, anything listening will do, i used this one

EDIT: messed with it beyond recognition, repairing it now
Topic archived. No new replies allowed.