サーバーからファイルをダウンロードするための画面をスイングで開発しました。ダウンロードボタンを1回クリックすると、コンセプト全体が正常に機能します。しかし、ダウンロードボタンを2回クリックすると、入力ストリームの取得中にコードが一時停止することがわかりました.(これは、表示されているsysoutを使用して追跡しました.
以下に示すのは、2 つの異なるファイルに含まれる 2 つの別個のコード スニペットです。TCPClient には serverocket コーディングがありますが、clientUI には ui コンポーネントがあり、TCPSever メソッドを呼び出してソケットを受け入れ、目的を要求します。
TCP クライアント側で:
public TCPClient() throws Exception{
System.out.println("Inside TCPClient constructor---");
clientSocket = new Socket("localhost", 3500);
System.out.println("After creating socket instance---");
oos = new ObjectOutputStream(clientSocket.getOutputStream());
System.out.println("after getting the ouput stream---");
ois = new ObjectInputStream(clientSocket.getInputStream());
System.out.println("after getting the input stream.");
}
クライアント UI で:
private void downloadButton_actionPerformed(ActionEvent e) throws Exception
{
Object selectedItem = contentsList.getSelectedValue();
System.out.println("selectedItem---"+selectedItem);
new TCPClient().downloadContents(nodeName,selectedItem.toString());
}
}
これに対する解決策を教えてください...
Below is the server code:
public void listening() throws Exception{
ServerSocket ss = new ServerSocket(3500);
System.out.println( "DataServer Is Listening..." );
while( true )
{
Socket soc = ss.accept();
ObjectInputStream ois = new ObjectInputStream(soc.getInputStream());
ObjectOutputStream oos = new ObjectOutputStream( soc.getOutputStream() );
String input = ( String ) ois.readObject( );
if(input.startsWith("downloadContents")){
String nodeName = ois.readObject().toString();
String contentName = ois.readObject().toString();
List contentsForNode = DBServer.getContentsForNode(nodeName);
for(Object obj : contentsForNode){
if(obj.toString().contains(contentName)){
new FileServer().send(obj.toString());
break;
}
}
}
}
}
public static void main( String[] args )
{
TCPServer obDataServer = new TCPServer();
try
{
obDataServer.listening();
}
catch ( Exception ioe )
{
ioe.printStackTrace();
}
}