开发者

New socket for client to send file to server

I have a client that sends a file to the server and sends other values to the server.I managed to transfer the file but it is not opened until i close the socket.So i made another socket in the client side just for sending t开发者_Go百科he file,but the server read it as if it is another client and incremented the clients number and gave me an exception as well saying> Socketexception:software caused connection abort: socket write error.Here is the code of client side with a new socket just for the sending,Can anyone help me about that?Thanks in advance.

try
{
    Socket sendSock=new Socket("localhost", 8080);
    outC.println("AcceptFile,");
    FileInputStream fis = new FileInputStream(p);
    byte[] buffer = new byte[fis.available()];
    fis.read(buffer);
    ObjectOutputStream oos = new ObjectOutputStream(sendSock.getOutputStream()) ;
    oos.writeObject(buffer);
    sendSock.close();
}
catch(Exception c)
{
    System.err.println("exc" + c);
}


tl;dr - Don't make a new socket. Just wrap the output stream and send the objects over.

Assuming outC is also a connection to your server, do this

outC.println("AcceptFile,");
FileInputStream fis = new FileInputStream(p);
byte[] buffer = new byte[fis.available()];
fis.read(buffer);
ObjectOutputStream oos = new ObjectOutputStream(originalSocket.getOutputStream());
oos.writeObject(buffer);

This will write an object to the underlying stream.

Now on the server side, you can do this

if(clientMessage.equals("AcceptFile,")) {
    ObjectInputStream ois = new ObjectInputStream(clientSocket.getInputStream());
    byte[] buffer = (byte[])ois.readObject();
    // do what you will with that
}


The problem in supporting multiple connections is on the server side. I don't see how you are going to solve that server behavior from the client side. It doesn't support multiple connections in one session.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜