0

I'm not sure what's going on here. I've got a TCP java server that waits for incoming connections.

In my TCP java client, I'm trying to make it so when I press the "connect" button on my GUI form, (when connect() is called in TCPClient.java), it opens up a new socket to the server.

This works, the server correctly displays that a new player joins when I press "connect" on the client.

Now I need the client to be able to have an input stream and an output stream to send/receive data on. I will do this on a separate thread. However when I create these 2 streams, the program completely freezes.

I don't want my program to freeze when I create the stream, because then it can't get to the point where I make my ListenFromServer thread:

    package client;

    import java.io.BufferedReader;
    import java.io.DataOutputStream;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.io.ObjectInputStream;
    import java.io.ObjectOutputStream;
    import java.net.Socket;
    import java.net.UnknownHostException;


    public class TCPClient extends Thread {
        UIClient ui;
        public boolean connected = false;
        Socket socket;
        ObjectInputStream sInput;       // to read from the socket
        ObjectOutputStream sOutput;     // to write on the socket


        public TCPClient(UIClient ui) throws UnknownHostException, IOException{
            this.ui = ui;
            System.out.println("client start");
            ui.console.println("CLIENT TEST");
        }

        public void connect() throws UnknownHostException, IOException{
            try{
                socket = new Socket("localhost", 2232);
            }
            catch(Exception e){
                ui.console.println("Could not connect to server");
                return;
            }
            try{
            sInput = new ObjectInputStream(socket.getInputStream());
            sOutput = new ObjectOutputStream(socket.getOutputStream());
            }
            catch (IOException ioE){
                ui.console.println("Exception creating new input/output streams");
                return;
            }
            //Thread that listens for messages from the server
            new ListenFromServer(this).start();
        }
    }

It freezes at this try statement:

                try{
            sInput = new ObjectInputStream(socket.getInputStream());
            sOutput = new ObjectOutputStream(socket.getOutputStream());
            }
Joe Bid
  • 465
  • 8
  • 24
  • Its not freezing it's waiting for the connection; java sockets are asynchronous so it is waiting for the connection I would redo the way your starting your using thread... http://stackoverflow.com/questions/16489026/what-is-the-main-advantage-of-extending-thread-class-or-when-to-extend-thread-i How you might want to do it is: Thread tr = new Thread(new Runabble(){ void run(){ while( your socket is connected){ TCPClient.this.yourMehtod(); } } }); – TheBetaProgrammer May 15 '14 at 01:14

2 Answers2

0

Create the streams in the run() method of the thread, not in the connect() method that is probably run in the event thread. Creating object streams does I/O. And create the ObjectOutputStream first.

user207421
  • 305,947
  • 44
  • 307
  • 483
  • Something is still wrong. After it creates the output stream and outputs: "test 4", the program does not output anything else. All though this time it does not freeze, nothing happens after that. http://pastie.org/private/ouflcm0yzud4kuzuwfwna – Joe Bid May 14 '14 at 22:16
  • I won't follow links. Anything that forms part of a question should be posted here: otherwise the question has no permanent value and is liable for deletion. Edit it into question. – user207421 May 14 '14 at 22:25
0

The ObjectIputStream will attempt to read from the input stream on construction. As the input stream is blocking the program will wait until there is something to read.

To resolve this you need to open an ObjectOutputStream on the sockets server endpoint.

Socket socket = ss.accept(); // ss is the ServerSocket
ObjectOutputStream os = new ObjectOutputStream(socket.getOutputStream());
ObjectInputStream is = new ObjectInputStream(socket.getInputStream());
BevynQ
  • 8,089
  • 4
  • 25
  • 37
  • Thanks that works but now I can't seem to send data from the server to the newly made client. I've got a ServerPlayer.java thread which is spawned upon a new connection (socket.accept()), and I pass a new output, input stream and the socket. Upon the initial starting point of the thread, I write a byte (1): http://pastie.org/private/haiiqxxvsils7k6zffzmpw And to receive it, I use my ListenForServer.java class which is a thread on my client that waits for data from server: http://pastie.org/private/wytv67wivqus0zqz6gb84q But nothing prints on the client console (line 20 of the second file) – Joe Bid May 15 '14 at 01:58
  • I suggest you make another question for that. – BevynQ May 15 '14 at 21:33