用socket在两台机子之间传文件,在JDK1.3运行正常,在1.4下却不行,大侠帮忙!

heliosky 2003-04-17 11:19:55
----SockServer----
package socket;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;

public class SockServer {
/** The TCP port for the object time service. */
public static final short LISTEN_PORT = 1951;
public static void main(String[] argv) {
ServerSocket sock;
Socket clientSock;
try {
sock = new ServerSocket(LISTEN_PORT);
while (true) {
clientSock = sock.accept( );
System.out.println("Accept from " + clientSock.getInetAddress( ));
DataOutputStream dos = new DataOutputStream(new FileOutputStream("graphInfobak.dat"));
DataInputStream dis = new DataInputStream(clientSock.getInputStream());
while(dis.available()!=0){
int i = dis.read();
dos.write(i);
}
dos.flush();
dis.close();
dos.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}

---SockClient---
package socket;

import java.io.*;

import java.net.Socket;

/**
* DaytimeObject - connect to the Daytime (ascii) service.
*/
public class SockClient {
/** The TCP port for the object time service. */
public static final short LISTEN_PORT = 1951;

public static void main(String[] argv) {
String hostName = null;
if (argv.length == 0) {
System.err.println("Usage: filename");
System.exit(1);
}
hostName = argv[0];
try {
Socket sock = new Socket(hostName, LISTEN_PORT);
DataOutputStream dos = new DataOutputStream(
new BufferedOutputStream(sock.getOutputStream()));
DataInputStream dis = new DataInputStream(
new BufferedInputStream(
new FileInputStream("graphInfo.dat")));

while (dis.available() != 0) {
dos.write(dis.read());
}
dos.flush();
dis.close();
dos.close();

} catch (IOException e) {
System.err.println(e);
}
}
}
...全文
29 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
oicu 2003-04-18
  • 打赏
  • 举报
回复
use inputstream.read(byte[]),
not readInt();
and use ByteArrayOutputStream.

Then you can make sure the whole data transferred ok.

(I can not input Chinese in redhat )
amjn 2003-04-18
  • 打赏
  • 举报
回复
监听端最好用多线程
heliosky 2003-04-18
  • 打赏
  • 举报
回复
to callzjy(callzjy)
程序虽能运行,但结果却不正确,传递的文件大小为0

anyway,感谢各位,我自己已经解决了,再经过测试, 其实这个程序在1.3下也是不正常的.
问题的关键在于,CLIENT端发送完以后,不管监听端收没收到就关闭了。

作相应改变,监听端收完文件后关闭,CLIENT端发现监听端闭后再关闭。
1.4提供了两个新函数。 socket.isClosed() socket.isConnected()

谢谢各位,来者有分!
DavidBone 2003-04-18
  • 打赏
  • 举报
回复
up
cloudtarget 2003-04-18
  • 打赏
  • 举报
回复
//server
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;

public class SockServer
{
public static final short LISTEN_PORT = 1951;
public static void main(String[] args)
{
ServerSocket sock;
Socket clientSock;
try
{
sock = new ServerSocket(LISTEN_PORT);
while (true)
{
clientSock = sock.accept( );
System.out.println("Accept from " + clientSock.getInetAddress( ));
DataOutputStream dos = new DataOutputStream(new FileOutputStream("graphInfobak.dat"));
DataInputStream dis = new DataInputStream(clientSock.getInputStream());
while(dis.available()!=0)
{
int i = dis.read();
dos.write(i);
}
dos.flush();
dis.close();
dos.close();
}
}
catch (IOException e)
{
e.printStackTrace();
}
}
}

//client
import java.io.*;
import java.net.Socket;

public class SockClient
{
public static final short LISTEN_PORT = 1951;
public static void main(String[] args)
{
String hostName = null;

try
{
Socket sock = new Socket("192.168.0.3", LISTEN_PORT);
DataOutputStream dos = new DataOutputStream(new BufferedOutputStream(sock.getOutputStream()));
DataInputStream dis = new DataInputStream(new BufferedInputStream(new FileInputStream("c:\\graphInfo.dat")));
while (dis.available() != 0)
{
dos.write(dis.read());
}
dos.flush();
dis.close();
dos.close();
}
catch (IOException e)
{
System.err.println(e);
}
}
}
成功!!!
qnzu 2003-04-17
  • 打赏
  • 举报
回复
重新编译看看

62,615

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧