A friendly place for programming greenhorns!
Big Moose Saloon
Search
|
Java FAQ
|
Recent Topics
Register / Login
Win a copy of
The Mikado Method
this week in the
Agile and other Processes
forum!
JavaRanch
»
Java Forums
»
Java
»
Beginning Java
Author
Problem with sockets example
Vladimir Vucetic
Greenhorn
Joined: Jul 09, 2010
Posts: 10
posted
Aug 14, 2010 05:32:32
0
I am trying to run example from "Head First Book Java" sockets chapter, but have no luck.
This should be server code project:
public class Main { String[] adviceList = {"Take smaller bites", "Go for the tight jeans. No they do NOT make you look fat", "One word: inappropriate", "Just for today, be honest. Tell your boss what you *really* think", "You might want to rethink that haircut"}; public void go() { try { ServerSocket serverSock = new ServerSocket(4242); while (true) { Socket sock = serverSock.accept(); PrintWriter writer = new PrintWriter(sock.getOutputStream()); String advice = getAdvice(); writer.println(advice); writer.close(); System.out.println(advice); } } catch (IOException ex) { ex.printStackTrace(); } } private String getAdvice() { int random = (int) (Math.random() * adviceList.length); return adviceList[random]; } public static void main(String[] args) { Main server = new Main(); server.go(); } }
This shoud be client code project.
public class Main { public void go() { try { Socket s = new Socket("127.0.0.1", 4242); InputStreamReader streamReader = new InputStreamReader(s.getInputStream()); BufferedReader reader = new BufferedReader(streamReader); String advice = reader.readLine(); System.out.println("Today you should: " + advice); reader.close(); } catch (IOException ex) { ex.printStackTrace(); } } public static void main(String[] args) { Main client = new Main(); client.go(); } }
I first run server project and then run client but have no luck.
When I run server project error text is:
java.net.SocketException: Unrecognized Windows Sockets error: 0: JVM_Bind at java.net.PlainSocketImpl.socketBind(Native Method) at java.net.PlainSocketImpl.bind(PlainSocketImpl.java:365) at java.net.ServerSocket.bind(ServerSocket.java:319) at java.net.ServerSocket.<init>(ServerSocket.java:185) at java.net.ServerSocket.<init>(ServerSocket.java:97) at Main.go(Main.java:14) at Main.main(Main.java:39)
What is the problem here?
Vladimir Vucetic
Greenhorn
Joined: Jul 09, 2010
Posts: 10
posted
Aug 14, 2010 06:16:17
0
I am sorry it from some unknown reason start working. But I am not sure why that error occurred before
David Newton
Author
Rancher
Joined: Sep 29, 2008
Posts: 12617
I like...
posted
Aug 14, 2010 06:18:19
0
This error is sometimes related to trying to open a port that's already open; it may just be that you already had an instance running.
Glad you got it working, though!
I agree. Here's the link:
http://aspose.com/file-tools
subject: Problem with sockets example
Similar Threads
client-server error
UnknownHostException when trying to connect to localhost
Socket Problems :(
Chapter 15 Head First - DailyAdviceClient and DailyAdviceService
Client/Server Communication Problem
All times are in JavaRanch time: GMT-6 in summer, GMT-7 in winter