'What could cause a freezing socket between C++ and Java?
I am trying to implement socket connection between C++ client and Java server. It must be cappable of sending files, and image file for every iteration as well as text. The C++ client is a dll. I am suffering to understand where is the problem. If both applications are sockets - locked by default, yet the client seems to "overtake" the server. For this reason server is sending request for data first, than just next - data should come back. I thought that should work, but not. Also setting some sleep between messages in the server improves functionality, but with or withouth sleep - Beep() server freezes, it seems it is working just slightly slower than client - it seems there is no synchronization. Plese help! Can you can give any valuable advice?
Below are important parts of both applications:
C++ client
while (userInput.size() > 0)
{
//some other lines
const int64_t fileSize = GetFileSize(fName);
int chunkSize = 1024
char buff;
RecvBuffer(sock, &buff, 5, 5); // 5 chars message length "A\r\n", another 5 to send at once ?
cout << "rcvd: " << buff<< endl; //Printing "rcvd: A" - it was sent from Java "A\r\n"
SendPackage(sock, fName, chunkSize, ks);
cout << "sent: " << endl; //This "sent: " is "walking back before rcvd: A" with different speed every time the program is started
//Beep(8000, 100);
}
int RecvBuffer(SOCKET s, char* buffer, int bufferSize, int chunkSize) {
int i = 0;
while (i < bufferSize) {
const int l = recv(s, &buffer[i], __min(chunkSize, bufferSize - i), 0);
if (l < 0) { return l; } // this is an error
i += l;
}
return i;
}
int64_t SendPackage(SOCKET s, const std::string fileName, int chunkSize,std::string header) {
int64_t fileSize = GetFileSize(fileName);
if (fileSize < 0) { return -1; }
std::ifstream file(fileName, std::ifstream::binary);
if (file.fail()) { return -1; }
if (send(s, header.c_str(), header.size(), 0) == SOCKET_ERROR) { return -2; }
std::vector<char> buffer(chunkSize);
bool errored = false;
int64_t bytes_sent = 0;
while (fileSize > 0) {
//Beep(0, 2000);//Any delay here also helps
int ssize = static_cast<int>(std::min<int64_t>(fileSize, chunkSize));
if (!file.read(buffer.data(), ssize)) { errored = true; std::cout << "err!!!" << std::endl;break; }
if (!SendBuffer(s, buffer.data(), ssize)) { errored = true; std::cout << "err!!!" << std::endl; break; }
fileSize -= ssize;
}
file.close();
if (errored) {
std::cout << "err" << std::endl;
return -3;
}
return bytes_sent;
}
BOOL SendBuffer(SOCKET s, const char* buffer, int bufferSize) {
if (bufferSize > 0)
{
int numSent = send(s, buffer, bufferSize, 0/*MSG_DONTROUTE*/);
if (numSent < 0) {
return false;
}
}
return true;
}
Java Server
while(!Message.equals("command_Exit"))
{
ps.println("A\r\n");
long filesize = 0;
String inputStr = br.readLine();
filesize = pD.getFileSize(inputStr);
dis = new DataInputStream(bis); //bis = new BufferedInputStream(is);//is = s.getInputStream();//s - socket
int pos = 0;
if (filesize > 0)
{
byte[] totalbytes = new byte[(int)filesize];
byte[] bytes = new byte[1024];
do
{
int chunkSize = (int) Math.min(filesize, (long) bytes.length);
dis.readFully(bytes, 0, chunkSize);
int x = 0;
while(x<chunkSize)
{
totalbytes[pos] = bytes[x];
x++;
pos++;
}
filesize -= chunkSize;
}
while (filesize > 0);
ByteArrayInputStream input_stream= new ByteArrayInputStream(totalbytes);
BufferedImage final_buffered_image = ImageIO.read(input_stream);
System.out.println(final_buffered_image);
}
dis=null;
}
I will appreciate any help. Thank you very much for the time spent!
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
