Java C++ Socket 的问题
悬赏分:25 - 提问时间2009-9-27 16:39
我想用C++作为服务器端,java作为客户端用Socket进行互联传输文件。虽然功能是实现了,但不知道为什么速度非常慢(传输一张800K的图片要2分钟O__O"…),请教大家有没有什么方法能加快一点速度的呢?
这是我的主要代码:
C++:
void Server::recvFile()
{
ofstream outs("C://tempC.bmp", ios::out | ios::binary);
while (true)
{
char buffer[50000];
char aWord;
char bWord[100];
int i;
bool eofile = false;
for (i=0; i<50000; i++)
{
recv(sockConn, &aWord, 1, 0);
if (aWord == '\n')
break;
buffer[i] = aWord;
if (aWord == 'e')
{
eofile = true;
break;
}
}
if (!eofile)
{
char aTemp[5];
for (int m=0; m<5; m++)
aTemp[m] = 0;
int count = 0;
int tempInt;
for (int j=0; j<i; j++)
{
if (buffer[j] == ' ')
{
stringstream ss(aTemp);
ss >> tempInt;
count = 0;
for (m=0; m<10; m++)
aTemp[m] = 0;
BYTE tempByte = (BYTE)tempInt;
outs.write((char*)&tempByte, sizeof(BYTE));
}
else
{
aTemp[count] = buffer[j];
count++;
}
}
}
else
break;
}
}
Java:
InputStream in = null;
OutputStream out = null;
DataOutputStream writeToSocket = null;
DataInputStream readFromSocket = null;
try {
Socket socket = new Socket("localhost", 2000);
writeToSocket = new DataOutputStream(socket.getOutputStream());
readFromSocket = new DataInputStream(socket.getInputStream());
} catch (UnknownHostException ex) {
Logger.getLogger(MyView.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(MyView.class.getName()).log(Level.SEVERE, null, ex);
}
System.out.println("Connection successful!\n");
try {
System.out.println("以字节为单位读取文件内容,一次读多个字节:");
byte[] tempbytes = new byte[10000];
int byteread = 0;
in = new FileInputStream("C://temp.bmp");
out = new FileOutputStream("C://temp1.bmp");
while ((byteread = in.read(tempbytes)) != -1)
{
String aString = "";
StringBuilder sb = new StringBuilder(aString);
for (byte element : tempbytes) {
sb.append(String.valueOf(element));
sb.append(" ");
}
if (byteread == -1)
sb.append("e");
else
sb.append("\n");
aString=sb.toString();
// System.out.println(aString.length());
out.write(tempbytes);
// System.out.println(aString);
writeToSocket.writeBytes(aString);
}
请大家指教!!
悬赏分:25 - 提问时间2009-9-27 16:39
我想用C++作为服务器端,java作为客户端用Socket进行互联传输文件。虽然功能是实现了,但不知道为什么速度非常慢(传输一张800K的图片要2分钟O__O"…),请教大家有没有什么方法能加快一点速度的呢?
这是我的主要代码:
C++:
void Server::recvFile()
{
ofstream outs("C://tempC.bmp", ios::out | ios::binary);
while (true)
{
char buffer[50000];
char aWord;
char bWord[100];
int i;
bool eofile = false;
for (i=0; i<50000; i++)
{
recv(sockConn, &aWord, 1, 0);
if (aWord == '\n')
break;
buffer[i] = aWord;
if (aWord == 'e')
{
eofile = true;
break;
}
}
if (!eofile)
{
char aTemp[5];
for (int m=0; m<5; m++)
aTemp[m] = 0;
int count = 0;
int tempInt;
for (int j=0; j<i; j++)
{
if (buffer[j] == ' ')
{
stringstream ss(aTemp);
ss >> tempInt;
count = 0;
for (m=0; m<10; m++)
aTemp[m] = 0;
BYTE tempByte = (BYTE)tempInt;
outs.write((char*)&tempByte, sizeof(BYTE));
}
else
{
aTemp[count] = buffer[j];
count++;
}
}
}
else
break;
}
}
Java:
InputStream in = null;
OutputStream out = null;
DataOutputStream writeToSocket = null;
DataInputStream readFromSocket = null;
try {
Socket socket = new Socket("localhost", 2000);
writeToSocket = new DataOutputStream(socket.getOutputStream());
readFromSocket = new DataInputStream(socket.getInputStream());
} catch (UnknownHostException ex) {
Logger.getLogger(MyView.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(MyView.class.getName()).log(Level.SEVERE, null, ex);
}
System.out.println("Connection successful!\n");
try {
System.out.println("以字节为单位读取文件内容,一次读多个字节:");
byte[] tempbytes = new byte[10000];
int byteread = 0;
in = new FileInputStream("C://temp.bmp");
out = new FileOutputStream("C://temp1.bmp");
while ((byteread = in.read(tempbytes)) != -1)
{
String aString = "";
StringBuilder sb = new StringBuilder(aString);
for (byte element : tempbytes) {
sb.append(String.valueOf(element));
sb.append(" ");
}
if (byteread == -1)
sb.append("e");
else
sb.append("\n");
aString=sb.toString();
// System.out.println(aString.length());
out.write(tempbytes);
// System.out.println(aString);
writeToSocket.writeBytes(aString);
}
请大家指教!!