私は、serversocketクラスを実装するクラスを持っています。そして、クライアント1を実装する別のクラスがあります。ソケットクラス。ソケットがデータを受け取ることができません
私がしようとしていることはそれです。ストリームを取得した後、私はクライアントがサーバーに番号を送信し、サーバーがプライムであるかどうかにかかわらずクライアントに応答します。 awt.Label
に表示されます。
しかし、私は何の応答も受け取ることができません。ここで
は、クライアントのコンストラクタのコードです:Serverの
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.");
}
}
});
}
コード:
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();
}
それは本当のアプリではありません。私は学んでいます。
素数実装は正しくはありませんが、メッセージは転送されているかどうかをテストしたいと思っています。完了しているかどうかは、素数実装を記述するのがよいでしょう。 – ritesht93
私はあなたが正しくそれをやっているJavaソケットについて知っているから。あなたのポートは転送され、ファイアウォールは停止していますか? – Axis
それでは、サーバーが素数を受け取るが、クライアントは応答を受け取ることがないという問題を明確にするために? – chradcliffe