我已经在swings中开发了一个屏幕,用于从服务器下载文件。单击下载按钮一次,整个概念正常工作。但是当我第二次点击下载按钮时,我发现代码暂停以获取输入流。(我已经使用显示的sysouts跟踪它了。)
下面显示的是两个不同文件中的两个单独的代码段。 TCPClient具有serversocket编码,而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.");
}
在客户端用户界面中:
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();
}
}
答案 0 :(得分:3)
猜测,您的服务器是单线程的,并且仍在读取其输入流,因为您尚未关闭客户端套接字。但是在您发布相关的服务器代码之前,这是任何人的猜测。
答案 1 :(得分:1)
在下载文件(成功/失败)后,您是否负责关闭套接字?它在代码片段中看起来不像。
我不确定,但这可能是问题
答案 2 :(得分:0)
这是服务器类:
package client_to_server;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;`
import java.net.ServerSocket;
import java.net.Socket;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
public class Server {
public static void main(String args[])throws IOException
{
ServerSocket serverSocket=new ServerSocket(2222);
System.out.println("New Server is Waiting");
Socket socket=serverSocket.accept();
System.out.println("My Connection Established");
DateFormat df=new SimpleDateFormat("HH:mm:ss");
Calendar c=Calendar.getInstance();
String starttime=df.format(c.getTime());
System.out.println("Start time is : "+starttime);
InputStream inputStream=socket.getInputStream();
byte[] readbyte=new byte[(1024*20)*1024];
FileOutputStream fileOutputStream=new FileOutputStream("/home/Manoj/copybulkfile5.zip");
int writebyte;
int count=0;
while((writebyte=inputStream.read(readbyte))!=-1)
{
if(writebyte>0)
count+=writebyte;
fileOutputStream.write(readbyte, 0, writebyte);
}
DateFormat df1=new SimpleDateFormat("HH:mm:ss");
Calendar c1=Calendar.getInstance();
String endtime=df1.format(c1.getTime());
System.out.println("END TIME is "+endtime);
System.out.println("THE WRITEBYTE VALUE IS "+writebyte+"THE READ BYTE VALUE IS"+count);
inputStream.close();
}
}
这是客户Cass:
package client_to_server;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.Socket;
public class Client {
public static void main(String args[])throws IOException
{
//Socket socket=new Socket("localhost",2222);
Socket socket=new Socket("localhost",2222);
File file=new File("/home/Checking/Myfile.zip");
byte[] mybyte=new byte[(1024*20)*1024];
FileInputStream fileInputStream=new FileInputStream(file);
int count;
OutputStream outputStream=socket.getOutputStream();
while((count=fileInputStream.read(mybyte))!=-1)
{
outputStream.write(mybyte);
}
System.out.println("THIS FILE HAS BEEN SENT SUCCESSFULLY!!!");
//System.out.println("END TIME "+hr+"Hours"+min+"Minutes "+sec+"Seconds");
socket.close();
}
}