Win a copy of Python Continuous Integration and Delivery this week in the Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Devaka Cooray
  • Junilu Lacar
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Ron McLeod
  • Tim Holloway
  • Claude Moore
  • Stephan van Hulst
Bartenders:
  • Winston Gutkowski
  • Carey Brown
  • Frits Walraven

Java, Client sending variable trough a thread to a server.  RSS feed

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, would appreciate if someone could take a look at my code. I've made a button which calls connect(); and then (new Thread(new client())).start();

What I want my class client to do is to keep sending the variable "iterationCounter" to the server class constantly trough the run method. the variable iterationCounter is changing value which then is printed out by the server. From my understanding, this should work?

Client Side:


Server side:


the error im getting is from the server side.
at java.util.Scanner.throwFor(Scanner.java:862)
at java.util.Scanner.next(Scanner.java:1485)
at java.util.Scanner.nextInt(Scanner.java:2117)
at java.util.Scanner.nextInt(Scanner.java:2076)
at TheServer.MainServer.main(MainServer.java:40)
 
Saloon Keeper
Posts: 2406
296
Android Angular Framework Eclipse IDE Java Linux MySQL Database Redhat TypeScript
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

ed jay wrote:the error im getting is from the server side.
at java.util.Scanner.throwFor(Scanner.java:862)
at java.util.Scanner.next(Scanner.java:1485)
at java.util.Scanner.nextInt(Scanner.java:2117)
at java.util.Scanner.nextInt(Scanner.java:2076)
at TheServer.MainServer.main(MainServer.java:40)


I ran the client (renamed client class to Client) and server code using this to get things started on the client side:
I also added a line to increment iterationCounter each time in the while loop and I saw this at the server:

ed jay wrote:I've made a button which calls connect(); and then (new Thread(new client())).start()


If I try the equivalent of that:
I see nothing at the server side (as expected) because when the new client is instantiated in this line: new Thread(new Client()).start(); the value of p is uninitialized and set to null (connect() has not been called for this this instance), so no attempt is made to write to the socket's output stream: if (p != null) p.println(iterationCounter);.
 
ed jay
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks alot Ron McLeod! I wont be able to test the code in a couple of days but looks like you got the result I was looking for, much appreciated.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!