Results 1 to 4 of 4
Thread: TCP Connection
- 06-04-2009, 05:49 AM #1
Member
- Join Date
- Feb 2009
- Posts
- 13
- Rep Power
- 0
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:
I've been messing with it for quite a while, and I can't seem to figure out how to do this.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(); */ } }
Any help would be appreciated :)
Thanks,
Blacknight
- 06-04-2009, 10:26 AM #2
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
- 06-04-2009, 11:42 AM #3
Member
- Join Date
- Feb 2009
- Posts
- 13
- Rep Power
- 0
Don't seem to be having any problems with the server not receiving the messages.
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 ?).then you can read from the Socket's input stream to get the reply.
Thanks for the reply
- 06-04-2009, 01:02 PM #4
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
-
URL connection problem
By rajeshgubba in forum New To JavaReplies: 8Last Post: 04-23-2009, 06:22 PM -
connection fails
By jrjan in forum Advanced JavaReplies: 3Last Post: 01-10-2009, 12:15 AM -
Connection Loss
By CrazyShells Slam in forum New To JavaReplies: 0Last Post: 05-15-2008, 02:56 PM -
no connection
By even in forum JDBCReplies: 15Last Post: 01-02-2008, 01:50 PM -
connection pool for db2
By paty in forum JDBCReplies: 1Last Post: 08-06-2007, 02:43 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks