Search...
FAQs
Subscribe
Pie
FAQs
Recent topics
Flagged topics
Hot topics
Best topics
Search...
Search within Sockets and Internet Protocols
Search Coderanch
Advance search
Google search
Register / Login
Win a copy of
TDD for a Shopping Website LiveProject
this week in the
Testing
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
Pie Elite
all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Campbell Ritchie
Paul Clapham
Ron McLeod
Jeanne Boyarsky
Tim Cooke
Sheriffs:
Liutauras Vilda
paul wheaton
Henry Wong
Saloon Keepers:
Tim Moores
Tim Holloway
Stephan van Hulst
Carey Brown
Frits Walraven
Bartenders:
Piet Souris
Himai Minh
Forum:
Sockets and Internet Protocols
java networking, hangs at readline()
Rob Brew
Ranch Hand
Posts: 101
posted 10 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
Hi all.
I'm playing with networking in
Java
, i can receive using a client fine, but can't send using the same topic, the server freezes on readline();
code below what is wrong?
BattleServer.java
public static void main(String[] args) { String s[] = init(); try { ServerSocket serverSock = new ServerSocket(4242); while (true) { Socket sock = serverSock.accept(); PrintWriter writer = new PrintWriter(sock.getOutputStream()); BufferedReader reader = new BufferedReader(new InputStreamReader(sock.getInputStream())); for (int i = 0; i < s.length; i++) { writer.println(s[i]); System.out.println("Written" + s[i]); } System.out.println("This far"); String result = reader.readLine(); System.out.println("but no further"); System.out.println("read " + result); writer.close(); } } catch (IOException ex) { ex.printStackTrace(); } }
NetLoad()
public static void NetLoad(String todo) { int x[] = new int[4]; int y[] = new int[4]; String s; Ships.clear(); try { Socket socket = new Socket("127.0.0.1", 4242); InputStreamReader streamReader = new InputStreamReader(socket.getInputStream()); BufferedReader reader = new BufferedReader(streamReader); BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())); while ((s = reader.readLine()) != null) { for (int i = 0; i < 4; i++) { x[i] = Character.digit(s.charAt(i), 10); } for (int i = 5; i < 9; i++) { int yMinus = (i - 5); y[yMinus] = Character.digit(s.charAt(i), 10); } Ships.add(new Ship(x, y)); x = new int[4]; y = new int[4]; } writer.write("hello"); writer.flush(); } catch (Exception ex) { ex.printStackTrace(); } for (Ship ship : Ships) { ship.printShip(); } }
Ulf Dittmer
Rancher
Posts: 43027
76
posted 10 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
Are those running on the same machine? Read
Don't println to a Socket
for a potential problem.
Rob Brew
Ranch Hand
Posts: 101
posted 10 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
On the same machine, BattleServer can send but locks on readline() to receive. Windows machines.
Acetylsalicylic acid is aspirin. This could be handy too:
free, earth-friendly heat - a kickstarter for putting coin in your pocket while saving the earth
https://coderanch.com/t/751654/free-earth-friendly-heat-kickstarter
reply
reply
Bookmark Topic
Watch Topic
New Topic
Boost this thread!
Similar Threads
UnknownHostException when trying to connect to localhost
Threaded Application won't run.
Problem with sockets example
Java Head First Client Server Example
Java networking, nothing happens
More...