客户端向服务器发送文件的新套接字
我有一个客户端,它将文件发送到服务器并向服务器发送其他值。我设法传输文件,但直到我关闭套接字后它才会打开。所以我在客户端创建了另一个套接字只是为了发送文件,但是服务器读取它就像它是另一个客户端一样,并增加了客户端编号,并给了我一个例外,并说>套接字异常:软件导致连接中止:套接字写入错误。这是带有新套接字的客户端代码,仅用于发送,任何人都可以帮助我吗?提前致谢。
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);
}
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 the 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);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
tl;dr - 不要创建新的套接字。只需包装输出流并将对象发送过来即可。
假设 outC 也是到您的服务器的连接,执行此操作
将向底层流写入一个对象。
现在在服务器端,你可以这样做
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
This will write an object to the underlying stream.
Now on the server side, you can do this
支持多个连接的问题出在服务器端。我不知道您将如何从客户端解决服务器行为。它不支持一个会话中的多个连接。
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.