java与vc进行socket通信(传图片文件的问题)
java服务器端接受的最后一个包始终要查几十个字节,不知道怎么回事?
vc:
BYTE d[1024];
int iWrote=0;
while(!feof(fp))
{
int num=fread((void*)d,1,1024,fp);
printf( "have read file %d byte\n ",num);
iWrote=send(connectSock,(const char* )d,num,MSG_OOB); printf( "have send %d byte\n ",iWrote);
}
java:
DataInputStream r=new DataInputStream(socket.getInputStream());
byte[] data=new byte[1024];
File f2=new File( "1.bmp ");
FileOutputStream out=new FileOutputStream(f2);
int length=0;int a=0;
while((a=r.read(data))!=-1)
{
try{
out.write(data,0,a);
}
catch(Exception e)
{
e.printStackTrace();
}
}
------解决方案--------------------Socket client = server.accept();
BufferedReader reader = new BufferedReader(new InputStreamReader(client.getInputStream()));
int buffer_size = 1024, n;
char[] data = new char[buffer_size];
while((n = reader.read(data)) != -1){
//data
}
reader.close();
……
------解决方案--------------------看不出什么特别的,就是那个 MSG_OOB 挺可疑的。