0

I have a class implementing serversocket class and there is another class implementing the client 1. Socket class.

So what I am trying to do is that. After getting the streams I want client to send a number to server and server will in turn respond to client whether it's prime or not. Which is display in an awt.Label.

But I am not able to receive any response.

Here is the code for client's constructor:

public ClientIsPrime() { setLayout(new GridLayout(2,2)); add(new Label("Enter a number: ")); add(numEntry=new TextField(10)); add(checkPrime=new Button("Check if number is Prime")); add(result=new Label("Result is shown here")); try { Socket client = new Socket(InetAddress.getLocalHost(), 5959); in=client.getInputStream(); out=client.getOutputStream(); }catch(UnknownHostException e) { result.setText("Local Host cannot be resolved"); }catch(IOException e) { result.setText("IOException occured"); } checkPrime.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent ae) { try { int num; num=Integer.parseInt(numEntry.getText()); out.write(num); out.flush(); int c; result.setText(""); String s=""; while((c=in.read())!=-1) s+=((char)c); result.setText(s); }catch(IOException e) { result.setText("IOException occured"); }catch(NumberFormatException e) { result.setText("Please enter a valid number."); } } }); } 

Code for Server:

public static void main(String args[])throws IOException { server=new ServerSocket(5959); socket=server.accept(); System.out.println("connected"); InputStream in=socket.getInputStream(); OutputStream out=socket.getOutputStream(); int c; String numStr=""; while((c=in.read())!=-1) numStr+=((char)c); int num=Integer.parseInt(numStr); if(num==3) out.write("Number is Prime".getBytes()); else out.write("Number is not Prime".getBytes()); out.flush(); in.close(); out.close(); } 

It isn't a real app. I am learning.

6
  • The prime number implementation is not right..but dont need any stuff on that..just wanted to test if messages are being transferred or not..once that is done..i would very well write the prime num implementation.. Commented Jan 8, 2012 at 23:30
  • From what I know about java sockets you are doing it correctly. Are your ports forwarded and your firewall down? Commented Jan 8, 2012 at 23:42
  • So, to clarify: is the problem that the server receives the prime number, but that the client never receives the response? Commented Jan 8, 2012 at 23:45
  • @chradcliffe The server is unable to receive the num..i inserted a SOPln(num) statement just after parsing..but didnt get any output.. Commented Jan 8, 2012 at 23:53
  • Soo what is the final solution then..should i switch over to DataInputStream or ObjectInputStream.. Isnt there a way to do this with InputStream.. Commented Jan 9, 2012 at 0:35

2 Answers 2

1

A few problems.

First your server implementation will never exit the while loop. The API for InputStream.read() states that it will block until data is received or the stream is closed. The stream is never closed so the reading will block forever after reading the initial data.

To solve this problem you must decide what your protocol is. See below.

The other problem is that you are writing from the client as a parsed int of text. So say 13 (as an int). But you are then reading it as if it were a sequence of characters. 13 on the wire will be read as some control character. You need to be consistent with how you write data and read data.

My suggestion would be to have a basic protocol. Use DataOutputStream on the writing side and DataInputStream on the reading side and then match the read/write calls on both sides to ensure you are consistent.

Sign up to request clarification or add additional context in comments.

3 Comments

Hmm ok..and for read() case..what if i could use available() method in a IF condition and then enter the loop.. and what if i pass the num variable as a string..using toString()..
If you want to understand what is happening I would read the source code of DataInput/OutputStream and see what it doing. Understanding what/how/why it does what it does is good. Reinventing the wheel beyond that is not.
Hmm..ok..so i think finally it would be better to use the Data/Object Streams then..anyways thanks for your reply!
0

If you want to sent integers across the wire it is infinitely easier to layer a DataOutputStream/DataInputStream on top of the raw socket streams and just do writeInt() and readInt()

1 Comment

Hmm that is ok..i know that..i can even use ObjectInputStream.. But i want to try it out with Input and Output Streams..it should work that way too..! Learning..so just wanted to try that out..

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.