Results 1 to 4 of 4

Thread: TCP Connection

  1. #1
    Blacknight is offline Member
    Join Date
    Feb 2009
    Posts
    13
    Rep Power
    0

    Default TCP Connection

    Hello,

    What I'm trying to do is:
    Have a thread (the client) connect to a server. Then be constantly checking if a variable within the client source changes, if it does change, then send it to the server. That Part is done.

    Now, what I want to do now is. Check if the server sends the client anything, and then from there I will check what the server sent and depending on what it is, do certain things.

    This is what I have so far:

    Java Code:
    public class ServerConnection extends Thread 
    	{ 
    		public void run()
    		{
    			Socket echoSocket = null;
    			PrintWriter out = null;
    			BufferedReader in = null;
    		try {
                echoSocket = new Socket("localhost", 7); // try binding on localhost port 7
                out = new PrintWriter(echoSocket.getOutputStream(), true);
                in = new BufferedReader(new InputStreamReader(echoSocket.getInputStream()));
            } catch (UnknownHostException e) {
                System.out.println(e);
                System.exit(1);
            } catch (IOException e) {
    			System.out.println(e);
                System.exit(1);
            }
    			BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in));
    		
    		
    			while(true) // Never leave loop
    			{
    			if(NewMsg != LastMsg) // check if the variable has changed
    			{
    				if(NewMsg != "")
    					out.println(NewMsg); // send the variable to server
    				LastMsg = NewMsg; // new message, is now old
    			} else {
    				Wait(100); // (callls function that does thread.sleep)
    			}
    			}
    			/*
    			out.close();
    			in.close();
    			stdIn.close();
    			echoSocket.close();
    			*/
    		}
    }
    I've been messing with it for quite a while, and I can't seem to figure out how to do this.


    Any help would be appreciated :)

    Thanks,

    Blacknight

  2. #2
    OrangeDog's Avatar
    OrangeDog is offline Senior Member
    Join Date
    Jan 2009
    Location
    Cambridge, UK
    Posts
    838
    Rep Power
    6

    Default

    Better to use some kind of wait()/notify() scheme if possible rather than constant polling you have. You could code this manually, or use a blocking queue to handle your messages.

    Once you've sent the message you need to flush() the stream so it actually gets sent, then you can read from the Socket's input stream to get the reply.
    Don't forget to mark threads as [SOLVED] and give reps to helpful posts.
    How To Ask Questions The Smart Way

  3. #3
    Blacknight is offline Member
    Join Date
    Feb 2009
    Posts
    13
    Rep Power
    0

    Default

    Quote Originally Posted by OrangeDog View Post
    Once you've sent the message you need to flush() the stream so it actually gets sent
    Don't seem to be having any problems with the server not receiving the messages.

    then you can read from the Socket's input stream to get the reply.
    Not sure if it makes a difference or not, but once the server receives the message it doesn't "reply" to it. I just need it to always check if it receives any messages from the server, while checking if it has anything to send to the server(Would I need to make another thread for this, or ?).

    Thanks for the reply

  4. #4
    OrangeDog's Avatar
    OrangeDog is offline Senior Member
    Join Date
    Jan 2009
    Location
    Cambridge, UK
    Posts
    838
    Rep Power
    6

    Default

    Without the flush, there's no guarantee that everything will always get sent.

    Looks like you probably want two threads for your application, as receiving from the server will block. The threads can signal to each other via shared variables.
    Don't forget to mark threads as [SOLVED] and give reps to helpful posts.
    How To Ask Questions The Smart Way

Similar Threads

  1. URL connection problem
    By rajeshgubba in forum New To Java
    Replies: 8
    Last Post: 04-23-2009, 06:22 PM
  2. connection fails
    By jrjan in forum Advanced Java
    Replies: 3
    Last Post: 01-10-2009, 12:15 AM
  3. Connection Loss
    By CrazyShells Slam in forum New To Java
    Replies: 0
    Last Post: 05-15-2008, 02:56 PM
  4. no connection
    By even in forum JDBC
    Replies: 15
    Last Post: 01-02-2008, 01:50 PM
  5. connection pool for db2
    By paty in forum JDBC
    Replies: 1
    Last Post: 08-06-2007, 02:43 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •