简体   繁体   中英

Socket unable to receive data

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.

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.

如果要通过导线发送整数,则在原始套接字流之上分层放置DataOutputStream / DataInputStream infinitely容易,只需执行writeInt()和readInt()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM